Ubuntu

How to install Pip and Use on Ubuntu 24.04

pip in ubuntu 24.04

To install the application on any of the Linux distributions like Ubuntu 24.04 its setup file is required which can be a compressed or deb file. These files are used with package managers to complete the installation and there are several package managers divided based on their functionality. Pip is a package manager for Python programming language, responsible for installing and managing the applications written in Python.

Outline:

How to Install Pip on Ubuntu 24.04

The preferred installer program or pip is primarily used for libraries and packages that don’t come with the standard Python library. Furthermore, pip enables the users to incorporate external modules in their Python code resulting in adding some extra functionality. To install pip on Ubuntu 24.04 there are three ways which will explained briefly:

1: Through Ubuntu Default Package Installer

Most of the packages on Ubuntu can be installed via apt so to install pip on Ubuntu by using its default package installer execute:

sudo apt install python3-pip

To confirm the installation verify the version by executing the version command:

pip3 --version

2:Through Python Script

Another way to install pip for Python is by using its script file which can be downloaded from its official site. To download the script file, click on the bootstrap link:

Alternatively, you can use the terminal to download the file by using its download link along with wget utility:

wget https://bootstrap.pypa.io/get-pip.py

Now if you directly install pip using Python and the script file you will get an error of eternally-managed-environment. This error arises to avoid conflict between the system package manager and the Python package manager.

To fix that issue a Python virtual environment is to be created as it will no longer be affected by the system’s default package installer. Here for illustration, I have created my home directory as a virtual environment, you can change it as per your preference:

python3 -m venv /home/linux

Here, the m flag specifies the given directory as the virtual environment:

Now activate the virtual environment directory by using the source command:

source /home/linux/activate

If the downloaded file is in another directory then move the file to the virtual environment directory and then execute the below command to install pip:

python get-pip.py

To confirm the installation, verify the version by executing the version command:

pip --version

3: Through Pipx Package Installer

The pipx is also a package manager for Python that uses the isolated environment for installing Python non-Debian packages, unlike pip. So to install pip via pipx you do not need to create a virtual environment just execute:

pipx install pip

While installing you will get a warning for the PATH environment variable which means that your current directory is not in the PATH variable. By adding the directory you can launch the application without specifying its path, to add the directory use the ensurepath command:

pipx ensurepath

To confirm the installation, verify the version by executing the version command:

pip --version

Note: There was another method for installing pip on Ubuntu which used the ensurepip module. However, the ensirepip module is now disabled for the Debian/Ubuntu systems:

python -m ensurepip

How to Use Pip on Ubuntu 24.04

The pip package installer simplifies the process of installing, updating, and removing Python applications. It also ensures smooth workflow by handling the version management and dependencies, further to use it effectively here are some basics for using pip on Ubuntu 24.04:

1: Listing all the Installed Packages

By installing pip, several different packages come pre-installed which can be checked by listing all the Python packages installed via pip:

pip3 list

This list includes both user-installed and pre-installed packages by pip:

2: Searching for Packages

If you are looking to install any specific Python package, then you can search for it by manually visiting the pip official site. Previously, the packages were allowed to be searched by using the search command along with pip:

pip3 search enter_search_term

Here for illustration, I have searched for Python blinker package:

3: Creating a Python Virtual Environment

To use pip on Ubuntu, it is necessary to have a virtual environment, as it can cause aunty issues with the system default package installer. If you want to install any Python package without creating a virtual environment the pipx would be the preferable choice. To create the Python virtual environment, first create a directory using mkdir command:

sudo mkdir <diretory-name>

Here I have created a directory named venv_pip :

Now to create the virtual environment you have to install virtualenv utility on Ubuntu first using the apt package manager:

sudo apt install python3-virtualenv

Now create the virtual environment in that newly created directory using the virtaulenv command:

sudo virtualenv <directory-name>

Now active the virtual environment using the source command along with its path for bin folder:

source <directroy-name>/bin/activate

To leave the virtual environment, use the deactivate command

4: Installing Packages via pip

The installation of any Python package or library is similar to that of using another package manager. Use the below syntax for installing the Python packages via pip:

pip3 install <Name-of-python-package>

Alternatively, you can also use the Python3 interpreter to invoke the pip module for installing Python packages:

python3 -m pip uninstall matplotlib

If you want to install any specific version of Python application then use the below syntax:

pip3 install Enter_Package_Name==<version-number>

Here, i have installed the older version of numpy:

Similarly, to remove any Python package using pip execute:

pip3 uninstall Enter_Package_Name

5: Updating Python Packages via pip

Sometimes you might experience performance or compatibility issues with any of the installed Python packages, so in that case try updating the package by executing:

pip3 install --upgrade Enter_Package_Name

Here, as in the image below, I had an older version of numpy installed which I updated using the above syntax:

Note: If you are using pip in the virtual environment for the first time, then you might encounter the permission error named could not install packages due to an OSError:

To resolve this error simply change the ownership of the following directory to the current user and while doing that do not add the last given folder as it is not created yet which in my case is numpy:

sudo chown -R $USER <directory-path>

6: Listing User installed pip Packages

To filter out the system package and the pre-installed package, use the freeze command:

pip3 freeze

This command will list the packages along with their versions

7: Installing Multiple Packages with pip

In Python-based projects there is a file named requirements.txt which is used to keep the record of user-installed packages via pip. Furthermore, this file can also be used to install multiple Python packages in one go for that first open up the file in the editor:

sudo nano requirements.txt

Next, enter the names of the packages correctly and then save the file:

Now execute the requirements file to install all the packages:

pip3 install -r requirements.txt

Another purpose of the requirements file is that it can be used to save the data for user-installed packages which can be utilized in any other project. So to add the user-installed Python packages to the requirements file execute:

pip3 freeze > requirements.txt

8: Checking Installed Package Info

To access the information about the installed package use the show command which can be useful for greeting the information about its version, author, and installed directory:

pip3 show Enter_Package_Name

Further, if you need extensive help regarding the use of pip package manager, consult its help:

pip3 --help

Conclusion

Pip is a package manager for Python which is primarily used in Python-based projects installing libraries or packages. To install pip on Ubuntu 24.04 there are three ways which include using the default package installer which is one of the preferred methods.

The two other include using pipx package manager and Python script file. To use pip for package installation, you have to create a virtual environment so that there are no issues with the system package manager.

Similar Posts