Pip is a tool for installing Python packages. With pip, you can search, download, and install packages from the Python Package Index (PyPI) and other package indexes.
This guide explains how to install pip for Python 3 and Python 2 on Ubuntu 20.04. We will also guide you through the basics of installing and managing Python packages with pip.
Before you begin
Python comes in two flavors; Python 2 and Python 3. Starting from Ubuntu 20.04, Python 3 is included in the basic system installation, and Python 2 is available for installation from the Universe repository. Users are available to switch to Python 3.
When installing Python modules globally, it is strongly recommended to install the deb module package with apt tools because they are tested to work well on Ubuntu systems. Python 3 packages start with the python3– package and Python 2 starts with python2-.
Use pip to install modules globally only if there is no deb package for that module.
Prefer to use pip in a virtual environment only. The Virtual Python environment allows you to install Python modules in isolated locations for specific projects, rather than installed globally. In this way, you don’t need to worry about affecting other Python projects.
Install pip for Python 3
To install pip for Python 3 on Ubuntu 20.04, run the following command as a root or sudo user on your terminal:
sudo apt update
sudo apt install python3-pip
The above command will also install all the dependencies needed to build a Python module.
When the installation is complete, verify the installation by checking the pip version:
The version number can vary, but it will look like this:
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
Install pip for Python 2
Pip for Python 2 is not included in the Ubuntu 20.04 repository. We will install pip for Python 2 using the get-pip.py script.
Start by activating the universe repository:
sudo add-apt-repository universe
Update the package index and install Python 2:
sudo apt update
sudo apt install python2
Use curl to download the get-pip.py script:
curl https://bootstrap.pypa.io/get-pip.py --output get-pip.py
After the repository is activated, run the script as a sudo user in python2 to install pip for Python 2:
sudo python2 get-pip.py
Pip will be installed globally. If you want to install it only for your users, run the command without sudo. The script will also install tools and settings wheel, which allows you to install the source distribution.
Verify the installation by printing the pip version number:
The output will look like this:
pip 20.0.2 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
How to use Pip
In this section, we show you some basic pip commands that are useful. With pip, you can install packages from PyPI, version control, local projects, and from distribution files. In general, you will install packages from PyPI.
To see a list of all pip commands and options, type:
You can get more information about certain commands using pip <command> –help. For example, to get more information about the install command, type:
pip3 install --help
Installing Packages with Pip
Say you want to install a package called memo which is used to scrape and extract data from the website.
To install the latest version of the package you will run the following command:
pip3 install scrapy
To install a specific version of the package, add == and the version number after the package name:
pip3 install scrapy==1.5
pip2 if using Python 2.
Install the Package with Pip using the Requirements File
requirement.txt is a text file that lists pip packages with the version needed to run certain Python projects.
Use the following command to install the list of requirements specified in the file:
pip3 install -r requirements.txt
List of Packages Installed
To register all installed pip packages, use the command below:
Upgrade Packages with Pip
To upgrade to a package that is already installed to the latest version, enter:
pip3 install --upgrade package_name
Uninstalling Packages with Pip
To uninstall the package:
pip3 uninstall package_name
We have shown you how to install pip on your Ubuntu machine and how to manage Python packages using pip.
For more information about pip, visit the pip user guide page.
If you have questions or feedback, feel free to comment below.