Newsletter Subscribe
Enter your email address below and subscribe to our newsletter
You will find multiple tools which depend on pip packages and that’s where the confusion starts as you can not install those packages via your traditional package manager like apt.
Pip (Pip Installs Packages) is a command line package manager to install Python packages which are listed in the Python Package Index (PyPI).
So in this tutorial, I will walk you through the following:
Let’s start with the first one.
Table of Contents
As pip is available in the default repository of Ubuntu, you can install pip using the apt packager. To use the apt package manager for installing pip, use the following:
sudo apt update && sudo apt install python3-pip python3-venv
Once done, you can check the installed version of pip using the following command:
pip3 --version
Now, let’s take a look at how to use a pip package manager in the virtual environment.
The reason why you must install Python packages in the virtual environment is pip won’t let you install them system-wide.
For example, earlier, we used to install packages using the pip install
command and now, if I try using that command, it will give me an error saying “externally-managed-environment”:
This error suggests that you can no longer install Python packages without isolation. So, let’s take a look at how to do it.
To create a virtual environment, you need to use the venv
flag with the python3
command and replace <env-name>
with the actual name of the virtual environment:
python3 -m venv <env-name>
For example, if I want to name my virtual environment env
, then I will use the following command:
python3 -m venv env
Once you create the virtual environment, you can activate it using the source
command and by entering the name of your virtual environment:
source <env-name>/bin/activate
As I named my virtual environment env
, I will use the given command to activate my virtual environment:
source env/bin/activate
Once you get into the virtual environment, you can use the pip package manager to perform various tasks. Here’s a table suggesting the basic use of the pip package manager:
Command | Description |
---|---|
pip3 install –upgrade <package-name> | Upgrades a software package to the latest version. |
pip list | Lists installed packages. |
pip list –outdated | Lists outdated packages and shows the latest versions available. |
pip3 search <package-name> | Searches for a particular package. |
pip3 install <package-name> | Installs the latest version of a software package. |
pip3 uninstall <package-name> | Removes a Python package. |
pip3 show <package-name> | Prints additional package details. |
pip download <package-name> | Downloads a package without installing it. |
Now, let’s take a look at some basic use cases of pip.
To install a package using pip, you need to use the pip3 install
command along with the package name as shown here:
pip3 install <package_name>
For example, if I want to install a library called requests
, then I will use the following:
pip3 install requests
To upgrade a package using pip, you need to use the pip install
with the --upgrade
flag as shown here:
pip install --upgrade <package-name>
For example, if I want to upgrade the requests
package, then I will use the following:
pip install --upgrade requests
To uninstall the package that you installed using pip, use the pip3 uninstall
command as shown here:
pip3 uninstall <package-name>
So let’s say I want to uninstall a package named requests
, then I will use the following:
pip3 uninstall requests
I find creating and managing virtual environments a tedious task and that’s when I came across a tool called pipx which is similar to pip but takes care of the virtualisation of packages automatically and you don’t have to switch to the environment either.
To install pipx on Ubuntu, you can use the following command:
sudo apt install pipx
Once done, add it to the $PATH so it can be used from anywhere:
pipx ensurepath
Now, you can use the pipx as the pip command and flags will remain the same. For example, if I want to install a package using pipx, I’d use the following:
pipx install <package-name>
In this tutorial, I went through how you can install pip on Ubuntu and how you can create a virtual environment to install and manage pip packages.
I hope you would have found my bonus tip helpful.