Pip is a tool for installing Python packages. With pip, you can search, download, and install packages from Python Package Index (PyPI) and other package indexes.
This guide explains how to install pip for Python 3 and Python 2 on Ubuntu 20.04. We will also walk you through the basics of installing and otherwise managing Python packages with pip.
Before You Begin
Python comes in two flavors; Python 2 and Python 3. Starting from Ubuntu 20.04, Python 3 is included in the base system installation, and Python 2 is available for installation from the Universe repository. Users are encouraged to switch to Python 3.
When installing a Python module globally, it is highly recommended to install the module’s deb package with the apt
tool as they are tested to work properly on Ubuntu systems. Python 3 packages are prefixed with python3-
and Python 2 packages are prefixed with python2-
.
Use pip to install a module globally only if there is no deb package for that module.
Prefer using pip within a virtual environment only. Python Virtual Environments allows you to install Python modules in an isolated location for a specific project, rather than being installed globally. This way, you do not have to worry about affecting other Python projects.
Installing pip for Python 3
To install pip for Python 3 on Ubuntu 20.04 run the following commands as root or sudo user in your terminal:
sudo apt update
sudo apt install python3-pip
The command above will also install all the dependencies required for building Python modules.
When the installation is complete, verify the installation by checking the pip version:
pip3 --version
The version number may vary, but it will look something like this:
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
Installing pip for Python 2
Pip for Python 2 is not included in the Ubuntu 20.04 repositories. We’ll be installing pip for Python 2 using the get-pip.py
script.
Start by enabling the universe repository:
sudo add-apt-repository universe
Update the packages index and install Python 2:
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
Once the repository is enabled, run the script as sudo user with python2
to install pip for Python 2:
sudo python2 get-pip.py
Pip will be installed globally. If you want to install it only for your user, run the command without sudo
. The script will also install setuptools
and wheel
, which allow you to install source distributions.
Verify the installation by printing the pip version number:
pip2 --version
The output will look something like this:
pip 20.0.2 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
How to Use Pip
In this section, we show you a few useful basic pip commands. With pip, you can install packages from PyPI, version control, local projects, and from distribution files. Generally, you will install packages from PyPI.
To view the list of all pip commands and options, type:
pip3 --help
You can get more information about a specific command using pip <command> --help
. For example, to get more information about the install command, type:
pip3 install --help
Installing Packages with Pip
Let’s say you want to install a package called scrapy
which is used for scraping and extracting data from websites.
To install the latest version of the package you would run the following command:
pip3 install scrapy
To install a specific version of the package append ==
and the version number after the package name:
pip3 install scrapy==1.5
pip3
with pip2
if using Python 2.Installing Packages with Pip using the Requirements Files
requirement.txt
is a text file that contains a list of pip packages with their versions that are required to run a specific Python project.
Use the following command to install a list of requirements specified in a file:
pip3 install -r requirements.txt
Listing Installed Packages
To list all the installed pip packages use the command below:
pip3 list
Upgrade a Package With Pip
To upgrade an already installed package to the latest version, enter:
pip3 install --upgrade package_name
Uninstalling Packages With Pip
To uninstall a package run:
pip3 uninstall package_name
Conclusion
We have shown you how to install pip on your Ubuntu or Parrot machine and how to manage Python packages using pip.
┌─[✗]─[puck@parrot-lt]─[~/htb/swagshop] └──╼ $python2 puck.py WORKED Check http://swagshop.htb/index.php/admin with creds rick:rick ┌─[puck@parrot-lt]─[~/htb/swagshop]
.