How to Install Python and pip on Linux

How to Install Python and pip on Linux

Reading time1 min
#Python#Linux#Ubuntu#pip#Pyenv

How to Install Python and pip on Linux

Almost every Linux distribution already ships Python 3, because system tools depend on it. Usually the job is not installing Python but adding pip, venv support or a newer version next to the system one.

One rule covers most of the trouble: leave the system python3 alone. Do not repoint /usr/bin/python3 to another version, and do not install packages into it with sudo pip. apt, dnf, cloud-init and other tools are built against that exact interpreter.

Check what you have

python3 --version
ls /usr/bin/python3*
python3 -m pip --version

Default versions on current releases:

  • Ubuntu 24.04: 3.12. Ubuntu 26.04: 3.14.
  • Debian 12: 3.11. Debian 13: 3.13.
  • RHEL 9 and clones: 3.9, with 3.11, 3.12 and (9.8+) 3.14 as separate packages. RHEL 10: 3.12, with 3.14 as a separate package.
  • Amazon Linux 2023: 3.9 as python3, with 3.11 to 3.14 as separate packages.

On Ubuntu and Debian only python3 exists. If scripts expect a python command, install python-is-python3.

Install Python, pip and venv

Ubuntu and Debian:

sudo apt update
sudo apt install python3 python3-pip python3-venv

python3-venv is a separate package on Debian-based systems. Without it, python3 -m venv fails with "ensurepip is not available". Add python3-dev and build-essential if you install packages that compile C extensions.

Fedora, RHEL, Rocky, AlmaLinux, Amazon Linux 2023:

sudo dnf install python3 python3-pip
sudo dnf install python3.12 python3.12-pip     # newer version next to 3.9 on RHEL 9 or AL2023

The versioned interpreter is then available as python3.12, while python3 stays 3.9.

Arch:

sudo pacman -S python python-pip

pip and "externally-managed-environment"

On Debian 12+, Ubuntu 23.04+ and Arch, this fails:

pip install requests
# error: externally-managed-environment

That is deliberate (PEP 668). Packages installed with pip into the system interpreter can overwrite or conflict with the ones apt installed and break system tools. Use one of these instead:

A virtual environment per project. This is the normal way to work:

python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
pip install requests
deactivate

pipx for command-line tools written in Python. Each tool gets its own environment, and its command goes into ~/.local/bin:

sudo apt install pipx        # Fedora: sudo dnf install pipx
pipx ensurepath
pipx install httpie

The distribution package when one exists, for example sudo apt install python3-requests.

pip has a --break-system-packages flag. The name is accurate, so do not use it on a system you care about.

Add a newer Python version

Install the new version alongside the system one and call it by its full name, or through a virtual environment. Do not use update-alternatives or ln -sf to make python3 point to it: that is the classic way to break apt on Ubuntu, and AWS explicitly warns against it on Amazon Linux 2023.

deadsnakes PPA (Ubuntu LTS)

The deadsnakes PPA is a community repository with other Python versions for Ubuntu LTS releases. It skips the version the release already ships.

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.13 python3.13-venv
python3.13 -m venv .venv

Create a venv with the new interpreter and pip comes with it. Do not try to install pip system-wide for it.

uv

uv downloads prebuilt Python builds into your home directory, so there is no compiling and no root access:

curl -LsSf https://astral.sh/uv/install.sh | sh
uv python install 3.13
uv venv --python 3.13
source .venv/bin/activate

It works the same on every distribution, which makes it handy on servers and in CI.

pyenv

pyenv compiles Python from source and switches versions per directory. Install the build dependencies first, otherwise modules such as ssl or sqlite3 come out missing:

sudo apt install build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev \
  libsqlite3-dev libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev \
  libffi-dev liblzma-dev libzstd-dev curl git
curl -fsSL https://pyenv.run | bash

Add the lines the installer prints to ~/.bashrc, open a new shell, then:

pyenv install 3.13          # latest 3.13.x
pyenv local 3.13            # writes .python-version in the current directory
python --version

Build from source

If none of the above fits, download the source tarball from python.org and build it:

tar -xf Python-3.13.*.tar.xz
cd Python-3.13.*/
./configure --enable-optimizations --prefix=/usr/local
make -j"$(nproc)"
sudo make altinstall

Use altinstall, not install. It installs python3.13 without creating a python3 link that would shadow the system interpreter. The same build dependencies as for pyenv apply.

Verify

python3 --version
python3 -c 'import ssl, sqlite3; print(ssl.OPENSSL_VERSION)'
python3 -m venv /tmp/test-venv && /tmp/test-venv/bin/pip --version

If the import fails on a self-built Python, a -dev package was missing during the build. Install it and rebuild.

Quick reference

sudo apt install python3 python3-pip python3-venv   # Ubuntu, Debian
sudo dnf install python3 python3-pip                # Fedora, RHEL, AL2023
sudo apt install python-is-python3                  # provide a `python` command
python3 -m venv .venv && source .venv/bin/activate  # per-project packages
pipx install tool-name                              # Python CLI tools
sudo apt install python3.13 python3.13-venv         # newer version via deadsnakes
uv python install 3.13                              # newer version, no root
pyenv install 3.13 && pyenv local 3.13              # per-directory versions
sudo make altinstall                                # source build, keeps system python3