Pip serves as a Python package manager, facilitating the installation, upgrading, configuration, and management of project dependencies. By utilizing pip, users gain the ability to search, download, and install packages from various indexes, including the Python Package Index (PyPI). The subsequent instructions elucidate the process of installing pip for both Python 3 and Python 2 on Ubuntu 22.04. Additionally, we will provide an overview of the fundamentals pertaining to the installation and administration of Python packages through pip.

Preparation

There are two variants of Python available: Python 2 and Python 3. Python 3 is already included in the initial installation of the system, while Python 2 can be installed from the standard Ubuntu repositories. It is recommended that users utilize Python 3.

Typically, when installing a Python module globally, it is advisable to use the apt tool to install the module’s deb package, as these packages are thoroughly tested to ensure compatibility with Ubuntu systems. Only when a deb package is unavailable for a particular module should you resort to using pip to install it globally.

Python 3 packages are distinguished by the prefix “python3-“, while Python 2 packages bear the prefix “python2-“.

It is highly recommended to utilize pip exclusively within a virtual environment. Python Virtual Environments enable you to install Python modules in a segregated location specific to a particular project, rather than installing them globally. By doing so, you can ensure that modifications or installations do not impact other Python projects, providing a more isolated and controlled environment.

Installing pip for Python 3


To install pip for Python 3 on Ubuntu 22.04, the process is simple. Follow these steps by executing the given commands in your terminal as a root or sudo user:

$ sudo apt update
$ sudo apt install python3-pip

The command above also installs all the dependencies required for building Python modules.

Once the installation is complete, verify it by checking the pip version:

$ pip3 --version

The version number may differ, but it will resemble something similar to the following format:

Output
pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)

Installing pip for Python 2

Pip for Python 2 is not available in the Ubuntu 22.04 repositories. Therefore, we will install pip for Python 2 using the get-pip.py script.

If Python 2 is not already installed on your system, you can install it by executing the following command:

$ sudo apt update 
$ sudo apt install python2

Use curl to download the get-pip.py script:

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

To install pip for Python 2, run the script as a sudo user using the python2 binary. Execute the following command in your terminal:

$ sudo python2 get-pip.py

The aforementioned command installs pip globally. If you prefer to install it only for your user, execute the command without sudo. The script also installs the setup tools and wheel packages, which enable the installation of source distributions.

To confirm the installation, print the pip version number by running the following command:

$ pip2 --version

The output will look something like this:

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

How to Use Pip

In this section, we will present a few helpful basic pip commands. To obtain a list of all pip commands and options, type the following:

$ pip3 --help

To acquire additional information about a specific command, you can use the following syntax: pip <command> --help. For instance, to obtain more details about the install command, type the following:

$ pip3 install --help

Installing Packages with Pip


The primary purpose of the pip tool is to install packages. Suppose you want to install numpy.

To install the latest version of a package, execute the following command:

$ pip3 install <package_name>

For example, to install the NumPy package, you would type:

$ pip3 install numpy

If you wish to install a specific version of a package, you can append == followed by the version number after the package name. For example:

$ pip3 install numpy==1.18.5

Replace pip3 with pip2 if using Python 2.

Installing Packages with Pip using the Requirements Files

A “requirements.txt” file is a text file that comprises a list of pip packages along with their required versions to run a particular Python project.

To install the packages listed in a requirements file, use the following command:

$ pip3 install -r requirements.txt

Listing Installed Packages

To list all the installed pip packages, use the “list” subcommand:

$ pip3 list

Upgrade a Package With Pip 

To upgrade an already installed package to the latest version, enter the following command:

$ pip3 install --upgrade package_name

Uninstalling Packages With Pip

To uninstall a package, run:

$ pip3 uninstall package_name

Conclusion


We have provided instructions on how to install pip on your Ubuntu machine and manage Python packages using pip. For further information about Pip, please visit the Pip user guide page.

If you have any questions or feedback, please feel free to comment below.

Comments to: How to Install Python Pip on Ubuntu 22.04

    Your email address will not be published. Required fields are marked *