Skip to content

Install Python 3.11.x on Ubuntu 22.04 (keep system 3.10 intact)

Install 3.11 side-by-side 3.10 and use it via python3.11 or better yet throught a venv.

Why this way?

On Ubuntu 22.04 the default python3 is 3.10 and many system tools rely on it.

deadsnakes PPA

Using deadsnakes PPA allows you to install specific Python versions without interfering with the system's default Python installation, which is crucial for system stability.

The deadsnakes PPA documentation

The deadsnakes PPA documentation explicitly states that : There's no guarantee of timely updates in case of security problems or other issues.

Current Versions

Python

python3 --version

Ubuntu

lsb_release -a

Install Python 3.11.x from Deadsnakes PPA

Dependencies / tools / Updates

software-properties-common allows you to easily manage your distribution and independent software vendor, like the ppa:deadsnakes/ppa.

sudo apt update && 
sudo apt install -y software-properties-common && 
sudo add-apt-repository -y ppa:deadsnakes/ppa && 
sudo apt update;

Python 3.11

python3, venv and some necessary development files for Python 3.11.

sudo apt install -y python3.11 python3.11-venv python3.11-dev

Optional

These packages are essential development libraries and tools required to compile software from source code on a Ubuntu system, particularly for languages like Python. They provide the necessary headers and files for your programs to interact with various system-level features.

sudo apt install -y build-essential libssl-dev zlib1g-dev libffi-dev libsqlite3-dev

Pip for 3.11

Install/Bootstrap pip for a specific Python interpreter

python3.11 -m ensurepip --upgrade

Upgrade pip to the latest version available from PyPI and also upgrades two other key build-related packages, setuptools and wheel

python3.11 -m pip install --upgrade pip setuptools wheel
python3.11 -m venv ~/.venvs/py311
source ~/.venvs/py311/bin/activate
python -V    # should show 3.11.x
# …install your packages…
deactivate

Make python/python3 point to 3.11 for your shell only

echo 'alias python=python3.11' >> ~/.bashrc
echo 'alias pip="python3.11 -m pip"' >> ~/.bashrc
source ~/.bashrc

Quick sanity checks

python3.11 -V
python3.11 -c "import sys; print(sys.executable)"
which python3.11