How to Install Python Pip on Ubuntu 18.04 and 20.04

How to Install Python Pip on Ubuntu 18.04 and 20.04

Download Complete Linux Commands Cheat Sheet

Whether you’re a Virtual Private Server (VPS) administrator, a software developer, or a general user, you might need to install pip on Ubuntu. As a package management system, pip lets you install packages of software written in the Python programming language.

In this tutorial, we will explain what pip is, how it works, and the ways to install pip on Ubuntu using Python 2 and Python 3. Finally, we’ll also go over how to install a pip package.

What Is Pip and What Does It Do?

Before we get to the pip installation tutorial, it’s important to understand what pip is and how it works.

Pip, which stands for “pip installs packages”, is a command-line package management system for software written in the Python programming language. Once installed, pip allows you to install and manage Python packages from the command line or a terminal program.

A Python package is a collection of Python modules. They contain Python definitions and statements, usually including runnable code.

Pip eases the process of installing and managing Python modules and packages because it connects to the Python Package Index (PyPI) – the official third-party software repository for Python.

When a user installs Python packages using pip, the package manager will resolve all the dependencies and check whether the chosen software packages are already installed. If pip finds that the software hasn’t been installed, it will install them on the machine.

How to Install Pip and Pip Packages

This section will explain how to install Pip on Ubuntu using Python 2 and Python 3. We will also go over basic pip commands, including how to install a pip package.

Installing Pip for Python 2

First, let’s go over how to install pip on the Ubuntu operating system for a machine that runs Python 2. However, pip for Python 2 uses different commands depending on which Ubuntu version you’re running.

In the following sections, you will find step-by-step instructions on how to install pip for Python on Ubuntu 18.04 and Ubuntu 20.04.

Ubuntu 18.04

On Ubuntu 18.04, Python 2 and pip are not installed by default. Follow these steps to install them:

  1. Open the terminal. To quickly do it, press Ctrl+Alt+T.
  2. Make sure that your Ubuntu 18.04 system is running Python 2 by entering the following command to the terminal:
python2 --version
  1. Update the repository package list by inputting this command:
sudo apt update
  1. Begin installing Python 2 and pip by running the following command:
sudo apt install python-/pip
command line output from running sudo apt install python-/pip

Note that all the dependencies required for building Python modules will be installed too.

You might need to press Y or Enter to complete the installation.

  1. Verify whether pip has been installed correctly by running this command:
pip --version

If the installation is successful, you will see your pip version number. The output should look similar to this:

command line output showing the installation was successful

The installed version may differ depending on the version of pip you’ve installed on your Ubuntu system.

  1. You have the option to install a file – requirements.txt – which contains a list of all the packages that can be installed using pip. To install the requirements, enter the following pip command:
sudo pip install -r requirements.txt
  1. Finally, make sure that you have the latest version of pip for Python. The latest iteration of pip that supports Python 2 is the 20.3.4 version. To upgrade pip to the latest version, run the following pip command:
sudo pip install --upgrade pip
command line output showing the latest version of pip for python

Ubuntu 20.04

pip for Python 2 is not included with Ubuntu 20.04. To install pip for Python 2 on Ubuntu 20.04, you will have to use the get-pip script.

  1. Open the terminal window. For a shortcut, press Ctrl+Alt+T.
  2. To update the repository package list, run the following command:
sudo apt update
  1. If you haven’t installed Python 2, do so first by entering the following command:
sudo apt install python2
  1. Download the get-pip.py script using curl:
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
  1. Once the script is downloaded, run it as a Python 2 sudo user:
sudo python2 get-pip.py
command line output after running python 2 sudo user

With the command above, pip will be installed at the system level. However, if you want to install pip on your user account only, run the script without sudo.

  1. Make sure that pip has been installed successfully by printing its version number with this command:
pip2 --version

Depending on which version of pip you have installed on the Ubuntu system, the output will be similar to the following:

pip 20.3.4 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
command line output checking the version number

Installing Pip3 in Ubuntu

Pip for Python 3 is commonly referred to as pip3. Fortunately, Ubuntu 18.04 has Python 3 installed by default. However, you will still need to install pip3 manually. Follow these steps:

  1. Open the terminal.
  2. To update the repository package list, run the following command:
sudo apt update
  1. Install pip for Python 3 and all the dependencies by entering this command:
sudo apt install python3-pip
installing pip for python 3

You might be asked to type Y or press Enter to confirm the installation.

  1. Verify the installation by printing your version number of pip:
pip3 --version

The output will depend on which version of pip you have installed. In general, it should resemble the following:

command line output verifying the installation
  1. To upgrade pip to the newest version, run the following command:
sudo pip3 install --upgrade pip
upgrading pip to the newest version

Installing a Pip Package

Now that you have installed pip on your Ubuntu machine, you can start installing Python packages found in the Python Package Index (PyPI) with pip and manage them using various commands.

Keep in mind that the commands you need to use will depend on the version of Python installed. The following commands are applicable for Python 3. If you use Python 2, simply change pip3 to pip in each command.

To install a package, run:

pip3 install <package name>

For example, here’s how to install Scrapy – the web-crawling framework tool – on a machine running Python 3:

pip3 install scrapy

To install a specific version of a package with pip, specify the version number in the command. For example, run this command if you want to install Scrapy 1.5:

pip3 install scrapy==1.5

To update an installed package to the newest version that’s available on the Python Package Index (PyPI), enter this command:

pip3 install <package_name> --upgrade

For example, enter this command to update Scrapy on a machine that’s running Python 3:

pip3 install scrapy --upgrade
command line output after running command to update scrapy on a machine running python 3

Sometimes, you will want to remove installed pip packages. To uninstall a package on Python 3, run the following command:

pip3 uninstall <package_name>

Conclusion

Pip is a helpful command line package manager and installer for Ubuntu. Using various commands, pip allows you to manage Python software packages from the Ubuntu terminal.

In this tutorial, you have learned how to install pip on Ubuntu machines running both Python 2 and Python 3. We’ve also gone over how to use pip to install a Python package and upgrade it to the latest version.

We hope you’ve found this article helpful and wish you the best of luck in your future projects with pip on Ubuntu.

Was this tutorial helpful?

Author
The author

Hasna Avni

Hasna is a Content Writer at Hostinger. With passion and knowledge in tech and digital marketing, she hopes to help people solve their problems and succeed. When not writing, she enjoys live music and overanalyzing movies.