How to install Python Pip on Ubuntu 20.04

Published on March 14, 2021 · Updated on March 14, 2021
How to install Python Pip on Ubuntu 20.04

Python-Pip is a package manager for Python. With PIP you can simply search, download and install Python packages. In this guide we will show you how you can install pip for both Python3 and Python2 on Ubuntu 20.04. In addtion to that we will show you basic commands, so you can easily upadate and install your python packages on your Ubuntu machine.

There are a number of ways to install Software on Ubuntu. The most popular method is using the system package mananger called apt, another newer but also very popular choice is using the Snap-Packagemananger. Soon there will be another way for you. It’s called pip and it can be used to install Python-based applications like Ansible.

Prerequisits

Although Python2 is deprecated, we will still include this in our guide, since its up to today still the most used python version. From Ubuntu 20.04 Python3 is now the standard included python. Is is also encouraged to use Python3 when possible.

You should always try to install a package with apt first, because they are well tested and most likely to function properly with Ubuntu 20.04. The python packages are prefixed with ‘python3-’ or ‘python2-’. e.g if you want to install pythons library requests for example then install it via apt: apt install python3-requests first, before you are trying to install it with pip.

You should use pip install only if there is no deb package in the apt package manager available.

Install pip for python3

You can install pip3 for Python3 with the following commands:

sudo apt update && sudo apt install python3-pip

To ensure that pip has been intalled you can type:

pip3 --version

Install pip for Python2

Unfortunately Pip for Python2 is not in the default Ubuntu 20.04 repositories. But we can use pypas get-pip.py Python script to install pip on Ubuntu 20.04.

First we need to add the universe repository to the package manager by entering:

sudo add-apt-repository universe

After the package is added, make sure to update the repository list and then install Python2.

sudo apt update
sudo apt install python2

In the next step you need to download the get-pip.py script from the following website in this example we will be using wget:

wget https://bootstrap.paypa.io/get-pip.py

Once you have downloaded the script successfully you can install pip by using python2:

sudo python2 get-pip.py

This command will install pip globally, that means any user on the machine can use it. If you want to install it for just a single user make sure to be that user and then run the script without sudo. The script does not only install python-pip for Python2, but it will also install setuptools and wheel. These packages will allow you to install and source more distributions.

Install your favorite pip package

First you can check the version of pip with

pip3 --version

or

pip2 --version

The output should be something like this:

pip 9.0.1 from /usr/lib/python3/dist-package (python 3.6)

To install a package lets say for example you want to install the python package requests, which is a very popular http client, you can type:

pip3 install requests 

This command will intall the latest version of requests. If you want however intall a specific version you can specify this by appending ‘==’ and the version number you which to install after the package name. Like so:

pip3 install requests=1.0

Install Pip Packages using a requirements.txt file

You can also install multiple packages with the exact version by using the requirements.txt text file. This is quite common used in bigger software projects to ensure everyone is on the same page and everyone can install the same packages and versions on their system.

To install packages from a ‘requirements.txt’ file simply append the ‘-r’ parameter

pip3 install -r requirements.txt

Please note that you have to be in the folder where the requirements.txt file is located. Otherwise specify the relativ path to the requirements file.

Listing installed Packages

To get a list of all pip packages currently installed onto your system you can use the ’list’ parameter:

pip3 list

Uninstall Packages

Uninstalling packages is as simple as installing them. Simply run:

pip3 uninstall PACKAGE_NAME

Conclusion

In this guide you have learned how to install pip on you Ubuntu machine, as well as using same basic commands with pip. For more information you can check out the official pip-docs.

As always if you have questions, feedback or corrections to make, please feel free to use the comment section down below.

Load Comments