Cybersecurity Updates & Tools

Install pip on Ubuntu 20.04: Python 3, Python 2, and Usage Guide

pip is Python’s package manager. It lets you search, download, and install packages from the Python Package Index (PyPI) and other sources. If you work with Python, pip is one of the first tools you will set up.

This guide shows you how to install pip on Ubuntu 20.04 for both Python 3 and Python 2, and covers the most useful pip commands for managing packages.

A few things to know before you start:

  • Ubuntu 20.04 ships with Python 3 by default. Python 2 reached end-of-life in 2020, so use Python 3 for all new projects.
  • When installing Python packages globally, prefer apt packages (prefixed with python3-) over pip when they are available. They are tested for Ubuntu compatibility.
  • The safest way to use pip is inside a virtual environment. This keeps each project’s packages separate and avoids version conflicts across projects.

Install pip on Ubuntu for Python 3

pip for Python 3 is the version you will use most often. Python 3 is the default on Ubuntu 20.04 and should be your first choice for any new Python work.

Install pip for Python 3:

bashsudo apt updatesudo apt install python3-pip

This also installs the dependencies needed to build Python modules from source.

Verify the installation:

bashpip3 --version

Output:

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 available in Ubuntu 20.04’s repositories. You install it using the get-pip.py script instead.

First, enable the Universe repository and install Python 2:

bashsudo add-apt-repository universesudo apt updatesudo apt install python2

Download the get-pip.py installer:

bashcurl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py

Run the script with Python 2:

bashsudo python2 get-pip.py

This installs pip globally. The script also installs setuptools and wheel, which let you install packages from source distributions. To install pip only for your current user, run the command without sudo.

Verify the installation:

bashpip2 --version

Output:

pip 20.0.2 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)

Manage Packages with pip

Here are the most common pip commands you will use on a regular basis.

Install the latest version of a package:

bashpip3 install scrapy

Install a specific version:

bashpip3 install scrapy==1.5

Install from a requirements file: A requirements.txt file lists all the packages a project depends on. This approach is very useful for team projects everyone runs the same command and gets the exact same packages.

bashpip3 install -r requirements.txt

List all installed packages:

bashpip3 list

Upgrade a package to the latest version:

bashpip3 install --upgrade package_name

Remove a package:

bashpip3 uninstall package_name
<strong>Note:</strong>&nbsp;Replace&nbsp;<code>pip3</code>&nbsp;with&nbsp;<code>pip2</code>&nbsp;in any command above if you are working with Python 2.

To see all options for any pip command, use --help:

bashpip3 install --help

This shows every available flag and option for that specific command.

pip is now installed and ready on your Ubuntu machine. For everyday Python work, the best habit is to use pip inside a virtual environment. Create one with python3 -m venv myenv in your project folder, activate it, and install packages from there. This keeps your global Python install clean and your projects independent from each other. Got questions? Leave a comment below.