Jul 1, 2023 4 min read

How to Install PIP on Ubuntu 20.04

Install PIP on Ubuntu 20.04 with our step-by-step tutorial. It is a command-line tool for installing and managing Python software packages.

Install PIP on Ubuntu 20.04
Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to install PIP on Ubuntu 20.04, let's briefly understand - What is PIP?

PIP Package Manager is a popular command-line tool for installing and managing Python software packages. It streamlines the process of integrating external libraries into Python projects, allowing developers to easily enhance their code with pre-existing functionality.

With a vast repository of packages, PIP makes it effortless to search, install, and update libraries, ensuring efficient and hassle-free development. Whether you're a beginner or an experienced Pythonista, PIP is an indispensable tool in your programming toolkit.

In this tutorial, you will install Python pip on Ubuntu 20.04 using apt package manager. Additionally, we will cover how to install and manage Python packages using pip.

Advantages of PIP

  1. Streamlined Installation: PIP simplifies the process of installing Python packages, eliminating the need for manual downloads and configuration.
  2. Vast Package Repository: PIP provides access to a vast collection of libraries, enabling developers to easily incorporate pre-existing functionality into their projects.
  3. Dependency Management: PIP automatically resolves and installs dependencies, saving time and ensuring compatibility between packages.
  4. Easy Updates: PIP makes it effortless to update installed packages to the latest versions, ensuring access to new features and bug fixes.
  5. Virtual Environments: PIP supports the creation of isolated virtual environments, allowing for project-specific package installations and avoiding conflicts between packages.

Prerequisites to Install PIP on Ubuntu 20.04

  • Make sure you are logged in as a sudo user

Install pip for Python 3

By default, Ubuntu 20.04 comes with Python 3. Follow these instructions to install pip3 for Python 3:

1) The following command should be used to update the package list:

sudo apt update

2) Now, install pip for python 3 using the following command:

sudo apt install python3-pip

As part of the above command, all the dependencies for creating Python modules will be installed.

3) If you have successfully installed pip, you can verify the installation by looking at the pip version:

pip3 --version

The version number will likely vary, but it will look like this:

Output

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

Install pip for Python 2

The Python 2 library is not installed by default in Ubuntu 20.04. The steps for installing Python 2 and pip for Python 2 are as follows:

1) Use the following command to update the package index and install python:

sudo apt update
sudo apt install python2

2) You can install pip for Python 2 with:

curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
sudo python2 get-pip.py

Python2, Pip, and all Python modules' dependencies will be installed by running the above command.

3) Print out the pip version number and verify the installation:

pip --version

There may be variations in version numbers, but they will look something like this:

Output

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

How to Use PIP

Using the apt package manager to install Python modules globally is highly recommended because they are tested and verified to work properly on Ubuntu systems.

When the package manager does not provide Python modules, you should install them via pip instead.

The majority of the time, pip should only be used within a virtual environment. Rather than installing Python modules globally, Python Virtual Environments allow you to install them in an isolated environment for a given project. You won't have to worry about interfering with other Python projects this way.

We'll go over a few fundamental pip commands in this section. We can install packages from PyPI, version control, local projects, and distribution files with pip, although in most circumstances, PyPI will be used.

Check the list of all pip commands and options using:

pip3 --help

Use pip <command> --help to get more information about a specific command. For example, to get more information about the install command, run the following command:

pip3 install --help

Install Packages with PIP

Let's imagine you wish to install Scrapy, a tool that allows you to scrape and extract data from websites.

Run the following command to install the most recent version of the package:

pip3 install scrapy

You would use the following command to install a specific version of the package:

pip3 install scrapy==1.5
Note: Replace pip3 with pip if using Python 2.

Install Packages with PIP using the Requirements Files

A text file called requirement.txt includes a list of pip packages and their versions that are necessary to run a Python project.

Install the requirements list specified in a file, using the following command:

pip3 install -r requirements.txt

List Installed Packages

Use the following command to see a list of all installed pip packages:

pip3 list

Upgrade a Package With PIP

Run the following command to upgrade an installed package:

pip3 install --upgrade package_name

Uninstall Packages with PIP

Use the following command to uninstall the packages:

pip3 uninstall package_name

FAQs to Install PIP on Ubuntu 20.04

How can I upgrade PIP to the latest version on Ubuntu 20.04?

Use the command pip install --upgrade pip to upgrade PIP to the latest version.

What if I encounter permission errors when installing packages with PIP?

Add sudo at the beginning of the command to run it with administrative privileges, or consider using virtual environments to avoid system-wide installations.

Can I install packages globally with PIP on Ubuntu 20.04?

Yes, PIP can install packages globally by using the sudo command before the installation command. However, it's recommended to use virtual environments for better isolation.

How can I uninstall a package installed with PIP?

Execute pip uninstall package_name in the terminal. Replace package_name with the name of the package you want to uninstall.

Can I install packages from the Python Package Index (PyPI) using PIP?

Absolutely! PIP allows you to install packages from PyPI by running pip install package_name. Replace package_name with the name of the desired package.

How do I list all the packages installed with PIP?

Type pip list in the terminal to get a list of all the installed packages along with their versions.

Conclusion

We hope this detailed tutorial helped you understand how to install PIP.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.