How-to Install pip on Ubuntu 16.04 LTS (Xenial Xerus)

In this tutorial we’ll learn how to install pip for python3 on Ubuntu 16.04. We will learn how to install pip from Ubuntu repository and PyPA.

Introduction

In this tutorial, we learn how to install pip on Ubuntu 16.04. There are two installation methods that we can use. We can install pip from the Ubuntu repository or PyPI.

pip is a package management system for Python programming languages. we use pip to install python application or libraries. We can search, install, remove many packages that can be found in Python Package Index (PyPI)

Python 2.7.9 or later and Python 3.4 and newer already include pip by default.

The name pip itself is recursive acronym of pip installs packages or pip install Python.

After installing pip, we learn how to use pip. By the end of this tutorial, you should be able to use pip as part of your daily tools.

Install pip3 on Ubuntu 16.04 from The Ubuntu Repository

Pip is available from the Ubuntu repository. Ubuntu 16.04 shipped with Python 3 by default so that we will install pip for Python 3. To install pip, we can use the command below.

sudo apt-get -y --no-install-recommends install python3-pip python3-setuptools

If we are installing pip from the Ubuntu repository, we need to update our pip installation using the command below.

pip3 --version
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)

This is a pretty old version of pip. At the time of this writing, the latest version of pip is version 18.1.

We can check latest version of pip by running pip3 with one of the command, for example list.


$ pip3 list
blinker (1.3)
...
urllib3 (1.13.1)
You are using pip version 8.1.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

The output above is truncated for clarity. We see above that the latest stable version of pip is version 18.1.

To upgrade pip3 we can use command below


$ sudo pip3 install --upgrade pip

The output should be similar to below :


Collecting pip
  Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl (1.3MB)
    100% |████████████████████████████████| 1.3MB 934kB/s 
Installing collected packages: pip
  Found existing installation: pip 8.1.1
    Not uninstalling pip at /usr/lib/python3/dist-packages, outside environment /usr
Successfully installed pip-18.1

The upgrade step above will note remove pip installed by ubuntu package on /usr/bin/pip but install new pip on /usr/local/bin/pip

If we start to use pip after upgrading it will throw error. We need to logout and login again to our system.


pip3 --version
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ImportError: cannot import name main
</module>

After log in again we can check installed version of pip. It already upgraded to version 18.1.


$ pip3 --version
pip 18.1 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)

Install pip on Ubuntu 16.04 from PyPA

This is the alternative way of installing pip. Instead of installing pip from the Ubuntu repository and then upgrade it we can install pip from PyPI.

If you already follow the steps above, you don’t need to follow the steps below.

Download pip installer script.


$ cd ~
$ wget -c https://bootstrap.pypa.io/get-pip.py

Run the installer script


$ sudo python3 get-pip.py

Remove pip installer script


$ rm -f ~/get-pip.py

Check where pip installed. If you’re using this method pip will be installed as pip instead of pip3


$ which pip
/usr/local/bin/pip

Check installed version of pip


$ pip --version
pip 18.1 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)

Using pip

Now pip is ready and we can start. First thing first, how to get help. You can use help option to get list of pip options and parameter:


$ pip help

Usage:   
  pip <command /> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible dependencies.
  config                      Manage local and global configuration.
  search                      Search PyPI for packages.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  help                        Show help for commands.
...

We can install Python package using pip. In this tutorial we’ll start by installing pep8 program as sample.


sudo pip install pep8

We can see detail information for a specific package using show option.


$ pip show pep8
Name: pep8
Version: 1.7.1
Summary: Python style guide checker
Home-page: http://pep8.readthedocs.org/
Author: Ian Lee
Author-email: IanLee1521@gmail.com
License: Expat license
Location: /usr/local/lib/python3.5/dist-packages
Requires: 
Required-by: 

To uninstall pep8 package you can use command below:


sudo pip uninstall pep8

To list all installed Python package you can use command below:


$ pip list
Package             Version               
------------------- ----------------------
backports.tempfile  1.0                   
...
wheel               0.32.3

I truncated the output above since the list of packages is quite long.

To get list of installed package in requirements format you can use freeze options.


$ pip freeze
backports.tempfile==1.0
...
urllib3==1.13.1

Yo can also save the freeze to a file and use it to install packages on another computer. For example :


pip freeze > requirements.txt

Copy the requirements.txt on another server and you can install all packages simply by running


pip install -r requirements.txt

Like any other package management system you can also search pip package using search option. For example let’s search for awscli package. awscli is command line interface for Amazon Web Services.


$ pip search awscli
awsclpy (0.4)           - Chain AWSCLI commands in Python.
awscli-keyring (0.1.2)  - AWS command line credentials from OS X Keychain and other keyrings.
awscli-cwlogs (1.0.0)   - AWSCLI CloudWatch Logs plugin
awscli (1.10.1)         - Universal Command Line Environment for AWS.

If freeze option above only list package with its version on a text file. wheel can create wheel package for us (.whl).

Quoting [http://wheel.readthedocs.org/en/latest/](Wheel Documentation), A Wheel is :

… a ZIP-format archive with a specially formatted filename and the .whl extension. It is designed to contain all the files for a PEP 376 compatible install in a way that is very close to the on-disk format. Many packages will be properly installed with only the “Unpack” step (simply extracting the file onto sys.path), and the unpacked archive preserves enough information to “Spread” (copy data and scripts to their final locations) at any later time.

Let’s create file wheel-input.txt with contents below:


pep8==1.7.1

The file we created above is a python requirement file that we can create with freeze option.

Let’s create the wheel package.


$ pip wheel -r wheel-input.txt

After the command above run successfully you will get pep8-1.7.1-py2.py3-none-any.whl file on directory where you run pip wheel.

Enable bash Completion

To enable bash completion for pip create new file /etc/profile.d/pip_completion.sh with contents below:

# pip bash completion start
_pip_completion()
{
    COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \
                   COMP_CWORD=$COMP_CWORD \
                   PIP_AUTO_COMPLETE=1 $1 ) )
}
complete -o default -F _pip_completion pip
# pip bash completion end

Summary

In this tutorial learned how-to install pip on Ubuntu 16.04 from Ubuntu repository and PyPA. We also learn how to use pip on Ubuntu. Now you can search for Python package or application that might be able to help your job and install it. Have Fun!