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
- Through Ubuntu Default Package Installer
- Through Python Script
- Through Pipx Package Installer
- How to Use Pip on Ubuntu 24.04
- Listing all the Installed Packages
- Searching for Packages
- Creating a Python Virtual Environment
- Installing Packages via pip
- Updating Python Packages via pip
- Listing User installed pip Packages
- Installing Multiple Packages with pip
- Checking Installed Package Info
- Conclusion
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:
To confirm the installation verify the version by executing the version command:
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:
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:
Here, the m flag specifies the given directory as the virtual environment:
Now activate the virtual environment directory by using the source command:
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:
To confirm the installation, verify the version by executing the version command:
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:
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:
To confirm the installation, verify the version by executing the version command:
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:
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:
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:
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:
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:
Now create the virtual environment in that newly created directory using the virtaulenv command:
Now active the virtual environment using the source command along with its path for bin folder:
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:
Alternatively, you can also use the Python3 interpreter to invoke the pip module for installing Python packages:
If you want to install any specific version of Python application then use the below syntax:
Here, i have installed the older version of numpy:
Similarly, to remove any Python package using pip execute:
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:
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:
6: Listing User installed pip Packages
To filter out the system package and the pre-installed package, use the freeze command:
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:
Next, enter the names of the packages correctly and then save the file:
Now execute the requirements file to install all the packages:
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:
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:
Further, if you need extensive help regarding the use of pip package manager, consult its 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.
I’m Aaliyan javaid, an electrical engineer and a passionate tech enthusiast. I have been working with embedded systems, programming, and Linux operating systems for over 2 years. I love to explore new technologies and share my knowledge and experience with others.