install and use pip on ubuntu 14.04

How-to Install and Use pip on Ubuntu 14.04

What is pip?

Pip is package management system for Python programming languages. We can use pip to manage software packages written in python. We can search, install, remove many packages that can be found in Python Package Index (PyPI).

In this tutorial we’ll learn how to install and use pip on Ubuntu 14.04.

Python 2.7.9 or later and Python 3.4 and later already include pip by default. The name pip itself is recursive acronym of pip installs packages or pip install Python.

There are two installation method that we can use, we can install from Ubuntu repository or from PyPI.

After installing pip, we’ll 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 pip From Ubuntu Repository

Pip is available from Ubuntu repository. To install pip we can use command below.


sudo apt-get -y install python-pip

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


sudo pip install --upgrade pip

The output should be similar to below :


Downloading/unpacking pip from https://pypi.python.org/packages/py2.py3/p/pip/pip-8.0.3-py2.py3-none-any.whl#md5=b234250205337ff67967dff300001e3d
Downloading pip-8.0.3-py2.py3-none-any.whl (1.2MB): 1.2MB downloaded
Installing collected packages: pip
Found existing installation: pip 1.5.4
Not uninstalling pip at /usr/lib/python2.7/dist-packages, owned by OS
Successfully installed pip
Cleaning up...

As you can see from output above the pip version installed from Ubuntu repository is version 1.5.4 and when we’re upgrading pip we get version 8.0.3.

Install pip From PyPI

This is alternative way of installing pip, 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

Install pip


$ sudo python get-pip.py

Remove pip installer


$ rm -f ~/get-pip.py

Check where pip installed


$ which pip
/usr/local/bin/pip

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

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


sudo pip install fortune

Since fortune package does not come with fortune database, we will download Brian Clapper fortune database using command below :


wget https://github.com/bmc/fortunes/blob/master/fortunes
wget https://github.com/bmc/fortunes/blob/master/fortunes.dat

Now we are ready to use fortune


fortune fortunes

To uninstall

fortune

package you can use command below:


sudo pip uninstall fortune

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


$ pip list
apt-xapian-index (0.45)
chardet (2.0.1)
Cheetah (2.4.4)
cloud-init (0.7.5)
configobj (4.7.2)
enum34 (1.1.2)
grizzled-python (1.0.7)
httplib2 (0.8)
jsonpatch (1.3)
jsonpointer (1.0)
keyring (3.5)
...

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
apt-xapian-index==0.45
chardet==2.0.1
Cheetah==2.4.4
cloud-init==0.7.5
configobj==4.7.2
httplib2==0.8
jsonpatch==1.3
jsonpointer==1.0
keyring==3.5
Landscape-Client==14.12
launchpadlib==1.10.2
lazr.restfulclient==0.13.3
lazr.uri==1.0.3
oauth==1.0.1
PAM==0.4.2
pep8==1.7.0
prettytable==0.7.2
pycrypto==2.6.1
pycurl==7.19.3
pygobject==3.12.0
pyOpenSSL==0.13
pyserial==2.6
python-apt===0.9.3.5ubuntu2
python-debian===0.1.21-nmu2ubuntu2
PyYAML==3.10
requests==2.2.1
SecretStorage==2.0.0
simplejson==3.3.1
six==1.5.2
ssh-import-id==3.21
Twisted-Core==13.2.0
Twisted-Names==13.2.0
Twisted-Web==13.2.0
urllib3==1.7.1
wadllib==1.3.2
zope.interface==4.0.5

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

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

Let’s install pep8 package and see the detail info about pep8 package


$ sudo pip install pep8
$ pip show pep8
---
Metadata-Version: 2.0
Name: pep8
Version: 1.7.0
Summary: Python style guide checker
Home-page: http://pep8.readthedocs.org/
Author: Johann C. Rocholl
Author-email: [email protected]
Installer: pip
License: Expat license
Location: /usr/local/lib/python2.7/dist-packages
Requires:
Classifiers:
Development Status :: 5 - Production/Stable
Environment :: Console
Intended Audience :: Developers
License :: OSI Approved :: MIT License
Operating System :: OS Independent
Programming Language :: Python
Programming Language :: Python :: 2
Programming Language :: Python :: 3
Topic :: Software Development :: Libraries :: Python Modules
Entry-points:
[console_scripts]
pep8 = pep8:_main

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

<

pre>
pep8==1.7.0

This is basically a 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.0-py2.py3-none-any.whl file on directory where you run pip wheel.

Summary

In this tutorial learned how to install pip for Python on Ubuntu 14.04 from Ubuntu repository and from 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!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.