How To

Install Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from the Python Package Index (PyPI). PyPI hosts hundreds of thousands of packages covering web development, data science, machine learning, automation, and more. Pip is not installed by default on Ubuntu 18.04, but the setup is quick.

This guide shows you how to install pip on Ubuntu 18.04 for both Python 3 and Python 2, and walks you through the most useful pip commands for day-to-day package management.

<strong>Prerequisite:</strong>&nbsp;You need sudo access.

Install Pip on Ubuntu: Python 3 and Python 2

Python 3 (recommended). Ubuntu 18.04 ships with Python 3 as the default. Install pip for Python 3 with:

bashsudo apt updatesudo apt install python3-pip

This also installs the header files and build tools required to compile Python extensions. Verify the installation:

bashpip3 --version

The output shows the pip version and the Python version it is linked to, for example: pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6).

Python 2. Python 2 is not installed by default in Ubuntu 18.04. To install both Python 2 and its pip package in one command:

bashsudo apt updatesudo apt install python-pip

Verify with:

bashpip --version

When to Use Pip and When to Use Apt

Before installing Python packages globally with pip, it is worth knowing when to reach for apt instead.

If a Python package is available through the Ubuntu repositories, install it with apt. Those packages are tested for compatibility with Ubuntu’s system libraries and receive security updates through the normal Ubuntu update process.

Use pip for packages that apt does not offer, or when you need a newer version than what Ubuntu provides.

The safest approach for most projects is to use pip inside a virtual environment. Create one with python3 -m venv myenv and activate it with source myenv/bin/activate. Once inside, pip installs packages into an isolated environment for that specific project only. This way, upgrading a package for one project will not break another.

How to Use Pip: Install, Upgrade, and Uninstall Packages

Install a package (latest version):

bashpip3 install scrapy

Install a specific version:

bashpip3 install scrapy==1.5

Install from a requirements file. A requirements.txt file lists the packages a project needs, with optional version pins. It is the standard way to share and reproduce a Python environment:

bashpip3 install -r requirements.txt

Generate a requirements file from your current environment with pip3 freeze > requirements.txt. This captures all installed packages and their exact versions.

List installed packages:

bashpip3 list

Upgrade a package:

bashpip3 install --upgrade package_name

Uninstall a package:

bashpip3 uninstall package_name

Replace pip3 with pip in any of these commands when working with Python 2. To get help for any subcommand, run pip3 <command> --help.

Pip is now installed on your Ubuntu 18.04 system. Visit the pip user guide to learn about dependency resolution, trusted hosts, and configuration files. Leave a comment below if you run into any issues.

Cyber Defence

Recent Posts

Install R on Ubuntu 18.04 from CRAN: Statistical Computing Setup

R is an open-source programming language and environment built for statistical computing and data visualization. It…

18 minutes ago

Install Jenkins on Ubuntu 18.04: CI/CD Server Setup Guide

Jenkins is an open-source automation server that makes it easy to build CI/CD pipelines. Continuous integration…

23 minutes ago

Install Android Studio on Ubuntu 18.04 with Snap and OpenJDK 8

Android Studio is the official IDE for Android development, built on JetBrains' IntelliJ IDEA platform. It…

28 minutes ago

Install and Configure GitLab on Ubuntu 18.04 with Omnibus

GitLab is a web-based, open-source Git repository manager written in Ruby. It includes built-in tools for…

32 minutes ago

Install Anaconda on Ubuntu 18.04: Python Data Science Setup Guide

Anaconda is the most widely used Python distribution for data science and machine learning. It bundles…

23 hours ago

Install WordPress on Ubuntu 18.04 with Nginx and PHP 7.2

WordPress is the most popular open-source CMS in the world, powering over 40% of all websites…

23 hours ago