Time to host your applications on cloud. Join Now
Installing software packages written in Python via the terminal is done with Pip, a nimble and comprehensive package manager. It saves you a lot of time in installing, upgrading and uninstalling Python and its dependencies. This will assist in downloading and installing the packages via pypi.org website.
This very fact that pip is needed means that you can tap into Python's blossoming ecosystem with over 200,000 apps ranging from web frameworks to the now science computing.
In this multi-procedural guide, we'll be looking at few different ways to get this tool installed on Ubuntu 16.04 systems, utilizing it to manage virtual environments and install packages via it in Python.
Here's a quick overview of pip before we get into the installation process:
Pip is the standard package manager for Python. It is installed by default with Python 3.4 and above.
For earlier versions of Python, pip needs to be installed separately.
Pip allows installing packages from the Python Package Index (PyPI) repository as well as other indexes.
Packages can be installed for the system Python or isolated environments called virtualenvs.
Pip manages dependencies automatically, enabling complex Python apps to be built easily.
First of all, it’s our turn to proceed through the different ways of pip installation on Ubuntu 16.04 LTS.
Before installing pip, your Ubuntu 16.04 system should have:Before installing pip, your Ubuntu 16.04 system should have:
Python 2.7 or Python 3.x available via the default Ubuntu repositories.
Up-to-date apt package index: sudo apt update
curl installed for some installation methods: sudo apt install curl
Unzip utility if using get-pip.py: sudo apt install unzip
Active internet connection for downloads.
The easiest way to install pip is by using Ubuntu's package manager apt:
sudo apt update
sudo apt install python3-pip
This will install the python3-pip package that contains pip for Python 3.x.
To also have pip2 for Python 2.7 available:
sudo apt install python-pip
Now both pip and pip2 commands can be used to install Python 2.x and Python 3.x packages respectively.
This method installs pip at the system level. The next methods will cover installing pip locally.
Python includes the ensurepip module that can bootstrap pip installation:
python3 -m ensurepip --default-pip
For Python 2.7 use:
python2 -m ensurepip --default-pip
This installs pip locally in the current user directory under ~/.local/. Add ~/.local/bin to your PATH to run it.
Advantages of this method include not requiring internet access or external dependencies.
Another option is using the get-pip.py installer script:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
This downloads the latest get-pip.py, executes it, and installs pip locally.
To install for Python 2.7, pass the Python binary:
python2 get-pip.py
The script also installs the setuptools and wheel packages needed for compiling certain Python packages.
An alternative approach is using an existing pip install to upgrade itself:
sudo apt install python3-pip
pip3 install --upgrade pip
This leverages system pip, upgrades it to latest version, then overwrites itself.
In summary, there are four primary ways to install pip on Ubuntu 16.04:In summary, there are four primary ways to install pip on Ubuntu 16.04:
Using apt to install Python pip packages
Leveraging Python's ensurepip module
Downloading and executing get-pip.py
Using existing pip to upgrade itself
The selected method will depend on your specific environment and needs.
A key benefit of pip is managing virtual environments to isolate project dependencies from each other and the system Python.
First, install the virtualenv package globally:
sudo pip3 install virtualenv
Then create environments and activate them:
virtualenv myprojectenv
source myprojectenv/bin/activate
Anything installed via pip now will be contained within the virtual environment.
Deactivate the environment when done:
deactivate
Pip virtual environments are a best practice for Python dependency and package management.
Once pip is available, you can start installing useful Python packages.
Packages are installed using the pip install command. For example to install the requests module:
pip3 install requests
Some packages have additional dependencies that pip will automatically install.
You can also specify package versions:
pip3 install requests==2.20.0
And upgrade packages:
pip3 install --upgrade requests
Use pip list to see installed packages and versions.
And pip uninstall to remove packages.
There are over 200,000 packages available via PyPI and other indexes that can now be leveraged on your Ubuntu system with pip!
Here are some additional pointers for using pip effectively:
Use virtual environments to avoid cluttering the global Python space.
Run pip freeze > requirements.txt to save installed packages for reproducibility.
Use -e when installing from GitHub for editable installs.
The -U flag will upgrade packages if newer versions are available.
pip show displays info about an installed package.
pip cache manages the pip download cache to save bandwidth.
Utilize a requirements.txt file for specifying all dependencies.
Pip opens up Python's extensive ecosystem of packages for you on Ubuntu. It manages dependencies, versions, and virtual environments - enabling hassle-free Python development.
Whether you need packages for web development, scientific Cloud computing, data analysis, machine learning, or anything else - pip has you covered.
Follow this guide to get pip installed on Ubuntu 16.04 via multiple methods. Then take full advantage of Python's power and flexibility with pip!
Let’s talk about the future, and make it happen!