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

git fetch vs git pull: How They Work and When to Use Each

Both git fetch and git pull talk to a remote repository, but they do very different things to your…

1 day ago

git cherry-pick Command: Apply Commits from Another Branch

Sometimes the change you need already exists, just on the wrong branch. A hotfix lands…

1 day ago

Best Email APIs for Secure Business Email: Why Developers Are Moving Beyond SMTP

Email is still one of the most important communication channels inside modern applications. Password resets,…

2 days ago

Nginx Commands in Linux: Start, Stop, Reload, Test, and Log

Nginx is a high-performance web server and reverse proxy trusted by some of the largest…

5 days ago

ufw Command in Linux: Manage Firewall Rules with Examples

ufw (Uncomplicated Firewall) sits on top of iptables (or nftables on newer systems) and replaces…

5 days ago

who Command in Linux: Show All Logged-In Users and Sessions

When you share a server with a team or investigate unexpected activity, the first question…

5 days ago