How to Install Pip on Ubuntu 21.04

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

Introduction

Pip is a package management system that simplifies installation and management of software packages written in Python such as those found in the Python Package Index (PyPI).

In this tutorial, we will show you how to install Python Pip on Ubuntu 18.04 using the apt package manager.We will also walk you through the basics of installing and managing Python packages with pip.

Installing pip for Python3

Install pip for Python 3 on Ubuntu 21.04 run the following commands:

$sudo apt update
$sudo apt install python3-pip -y

When the installation is complete, verify the installation by checking the pip version:

$pip3 --version

Note: The version number may vary.

Installing pip for Python2

So Pip for Python 2 is not included in the Ubuntu 21.04 repositories. We’ll install pip for Python 2 using the get-pip.py script.

Start by enabling the universe repository:

$sudo add-apt-repository universe

Then press ENTER to continue…

Update the packages index and install Python 2:

$sudo apt update
$sudo apt install python2 -y

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

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

If the command not found you can install it with:

$sudo snap install curl 

# version 7.76.1

or

$sudo apt install curl 


# version 7.74.0-1ubuntu2

Once the repository is enabled, run the script as sudo user with python2  to install pip for Python2:

$sudo python2 get-pip.py

So 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

How to Use Pip

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:

$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:

$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

Note: Replace 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:

$pip3 install --upgrade package_name

Uninstalling Packages With Pip

To uninstall a package run:

$pip3 uninstall package_name

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
MQ-Jr
MQ-Jr
unixcop Admin

2 COMMENTS

  1. I recently upgraded from 20.10 to 21.04
    After the upgrade, the installed python modules are able to be found, and neither can pip be found
    After installing pip:
    Reading package lists… Done
    Building dependency tree… Done
    Reading state information… Done
    python3-pip is already the newest version (20.3.4-1ubuntu2).
    0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

    Checking pip3 –version gives:
    Traceback (most recent call last):
    File “/home/john/.pyenv/versions/oanda/bin/pip3”, line 5, in
    from pip._internal.cli.main import main
    ModuleNotFoundError: No module named ‘pip’

    Still looking for a fix. Any suggestions?
    Thanks.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook