How to Install Python 3.12 on Ubuntu 22.04 or 20.04 (Without the PIP Chaos)
If your latest project demands the newest language features, you’ll need Python 3.12 before you can get started. The stock Ubuntu repositories still ship 3.10, so you have to bring in an extra source. This guide shows how to do that cleanly and why each step matters.
Why the Official Repos Lag Behind
Ubuntu focuses on stability, not cutting‑edge releases. That means the default package manager (apt) pulls Python 3.10 from 22.04’s “jammy” archive or 20.04’s “focal” archive. If you’re used to pip installing packages that expect a newer runtime, you’ll hit compatibility headaches.
I once had an automated data‑pipeline that started crashing after the latest security update: it was still using Python 3.10, but one of its dependencies required the new match statement only available in 3.12. The fix was to install 3.12 separately—no upgrade of the entire OS.
Prepping Your System
1. Update your package list – this ensures you’re pulling the newest meta‑information for the base system.
sudo apt update && sudo apt upgrade -y
2. Install build essentials – Python 3.12 will be compiled from source if you don’t use a PPA, and those tools make that painless.
sudo apt install build-essential libssl-dev zlib1g-dev \
libncurses5-dev libncursesw5-dev libreadline-dev \
libsqlite3-dev libffi-dev wget
Without these libraries, you’ll see “undefined reference” errors when compiling.
Adding the deadsnakes PPA
The deadsnakes Personal Package Archive maintains newer Python releases for Ubuntu. It’s a well‑maintained, community source that ships .deb packages you can install with apt.
sudo add-apt-repository ppa:deadsnakes/ppa sudo apt update
If the PPA isn’t recognized, make sure you’ve got software-properties-common installed:
sudo apt install software-properties-common
Installing Python 3.12
Now that the repository is in place, grab the binary:
sudo apt install python3.12 python3.12-venv python3.12-dev
Why pull python3.12-venv and python3.12-dev?
The former lets you create isolated environments (python -m venv) without touching the system Python. The latter is required for building packages with native extensions (think pandas, numpy).
Switching Default python(3) to 3.12
Ubuntu still sets /usr/bin/python to a legacy version (often 2.7 or 3.10). If you want python3 to point to 3.12:
sudo update-alternatives --install /usr/bin/python3 python3 \
/usr/bin/python3.12 1
Verify:
python3 --version # should print Python 3.12.x
Heads‑up: Changing the default python3 can break system scripts that rely on features only present in earlier releases. I’d keep both versions side by side and use virtual environments for projects.
Verifying Everything Works
1. Check the interpreter
python3 -c "import sys; print(sys.version)"
2. Test a simple match statement (only in 3.12)
python3 <<'EOF'
match 3:
case 1: print('one')
case 2: print('two')
case _: print('other')
EOF
You should see “other” printed.
3. Install a package with C extensions – if that installs without errors, you’re good to go.
python3 -m pip install numpy
Cleaning Up (Optional)
If you ever decide 3.12 isn’t worth the extra disk space:
sudo apt purge python3.12*
Then remove the PPA if you don’t plan to use it again:
sudo add-apt-repository --remove ppa:deadsnakes/ppa
Give it a whirl; if you hit any snags, drop me a note and we’ll troubleshoot together.