Python Installation β Linux#
Debian / Ubuntu#
sudo apt update
sudo apt install python3 python3-pip python3-venv python3-dev -y
Output:
Reading package lists... Done
The following NEW packages will be installed:
python3 python3-dev python3-pip python3-venv
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
For a newer version than the distro default, add the deadsnakes PPA:
sudo add-apt-repository ppa:deadsnakes/ppa -y
sudo apt update
sudo apt install python3.12 python3.12-venv python3.12-dev -y
Fedora / RHEL / Rocky / AlmaLinux#
# Fedora
sudo dnf install python3 python3-pip -y
# RHEL / Rocky / AlmaLinux β enable EPEL first
sudo dnf install epel-release -y
sudo dnf install python3.12 -y
Output:
Installed:
python3.12-3.12.3-1.fc40.x86_64
python3.12-libs-3.12.3-1.fc40.x86_64
Complete!
Arch Linux / Manjaro#
sudo pacman -Syu python python-pip
Output:
resolving dependencies...
Packages (2) python-3.12.3-1 python-pip-24.0-1
Total Installed Size: 74.32 MiB
:: Proceed with installation? [Y/n] Y
[!NOTE] Archβs
pythonpackage always tracks the latest stable Python, so it updates frequently. This is excellent for getting new versions but can occasionally break packages that havenβt updated yet.
pyenv β version-independent approach#
Works on any Linux distro. Installs Python in your home directory, no root required.
# Install build dependencies (Debian/Ubuntu)
sudo apt install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev \
libffi-dev liblzma-dev
# Install pyenv
curl https://pyenv.run | bash
# Append to ~/.bashrc / ~/.zshrc
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
source ~/.bashrc
# Install and set default
pyenv install 3.12.3
pyenv global 3.12.3
Output:
Downloading Python-3.12.3.tar.xz...
-> https://www.python.org/ftp/python/3.12.3/Python-3.12.3.tar.xz
Installing Python-3.12.3...
Installed Python-3.12.3 to /home/user/.pyenv/versions/3.12.3
Building from source (unsupported distros)#
Use this when no package manager provides your target version:
# Install build dependencies first (Debian/Ubuntu)
sudo apt install -y build-essential libssl-dev zlib1g-dev libffi-dev libsqlite3-dev
# Download and build
wget https://www.python.org/ftp/python/3.12.3/Python-3.12.3.tgz
tar xf Python-3.12.3.tgz
cd Python-3.12.3
./configure --enable-optimizations --with-ensurepip=install
make -j$(nproc)
sudo make altinstall # altinstall avoids overwriting the system python3
Output:
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-24.0 setuptools-69.5.1
[!TIP] Use
make altinstall(notmake install) to avoid replacing the systempython3symlink, which could break OS tools.
Verify#
python3 --version
python3 -m pip --version
python3 -c "import venv; print('venv OK')"
Output:
Python 3.12.3
pip 24.0 from /home/user/.pyenv/versions/3.12.3/lib/python3.12/site-packages/pip (python 3.12)
venv OK
Common pitfalls#
[!WARNING] Externally managed environments β Debian 12+ and Ubuntu 23.04+ enforce PEP 668, blocking
pip installoutside a venv with the errorerror: externally-managed-environment. Always activate a virtual environment first. See venv.
[!WARNING] Missing
python3-venvβ On Ubuntu,python3 -m venvfails until you installpython3-venv(orpython3.12-venvfor a specific version):sudo apt install python3-venv.
Next steps#
python3 -m venv .venv
source .venv/bin/activate
See Virtual Environments.