How to Install Python 3.12 on Ubuntu (26.04, 24.04, 22.04)

Last updated Friday, February 27, 2026 11:53 am Joshua James 20 min read 2 comments

Python 3.12 brings improved error messages with fine-grained tracebacks, per-interpreter GIL groundwork, and a streamlined standard library that removed distutils. To install Python 3.12 on Ubuntu, you pick one of three paths depending on your LTS release, and the new interpreter runs alongside the system default without conflicts.

Ubuntu 24.04 LTS already ships Python 3.12 as its default python3, Ubuntu 22.04 LTS adds it through the Deadsnakes PPA, and Ubuntu 26.04 LTS needs a source build. Python 3.12 receives upstream security fixes through October 2028 per PEP 693, and each path includes pip setup, virtual environments, troubleshooting, and clean removal.

Install Python 3.12 on Ubuntu

Check the Default Python Version and Python 3.12 Availability by Ubuntu Release

Ubuntu 24.04 LTS uses Python 3.12 as the default python3 interpreter, Ubuntu 22.04 LTS uses Python 3.10 and needs the Deadsnakes PPA for python3.12, and Ubuntu 26.04 LTS uses Python 3.13 and does not ship Python 3.12 packages in the default repositories.

Ubuntu ReleaseDefault python3Python 3.12 AvailabilityRecommended Method
Ubuntu 26.04 LTSPython 3.13.xNot available in default repositoriesCompile Python 3.12 from source
Ubuntu 24.04 LTSPython 3.12.xAvailable in official Ubuntu repositoriesInstall from Ubuntu repositories
Ubuntu 22.04 LTSPython 3.10.xNot in default Jammy repositories; available from Deadsnakes PPAInstall via Deadsnakes PPA

The Deadsnakes PPA publishes Python 3.12 packages for Ubuntu 22.04 LTS (Jammy). For Ubuntu 24.04 LTS (Noble), Python 3.12 already comes from Ubuntu’s official repositories, and for Ubuntu 26.04 LTS (Resolute) you need a source build because no Python 3.12 package is published for that release.

Choose a Python 3.12 Installation Method on Ubuntu

Choose the installation path that fits your Ubuntu release and workflow.

MethodChannelVersion SourceUpdatesBest For
Ubuntu repositories (24.04 LTS)Ubuntu PackagesDistribution packageAutomatic via apt upgradeUbuntu 24.04 LTS systems that want Canonical security updates
Deadsnakes PPA (22.04 LTS)Deadsnakes PPACommunity-maintained packageManual monitoring with apt upgradeUbuntu 22.04 LTS systems that need Python 3.12 alongside Python 3.10
Source build (26.04 LTS or custom)Python.org sourceLatest upstream 3.12.xManual rebuildsUbuntu 26.04 LTS, custom prefixes, or pinned maintenance workflows

Recommended path: use Ubuntu repositories on 24.04 LTS, use Deadsnakes only on 22.04 LTS, and use the source build section on 26.04 LTS.

Related Python Version Guides for Ubuntu

If your application stack is pinned to a different runtime, use these Ubuntu-specific version guides instead of forcing Python 3.12:

Run Python 3.12 Pre-Installation Checks on Ubuntu

Before you install Python 3.12, confirm your Ubuntu release, check whether Python 3.12 is already installed, and verify you have enough free space if you plan to compile from source.

# Check whether Python 3.12 is already installed
python3.12 --version 2>/dev/null && echo "Python 3.12 already present" || echo "Python 3.12 not found, safe to install"

# Confirm your Ubuntu release
lsb_release -rs

# Check free space for source builds
df -h /usr/local

Example output on supported Ubuntu LTS releases:

Python 3.12 not found, safe to install
22.04
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       250G   40G  198G  17% /

Source builds can use significantly more temporary space than the final install size when --enable-optimizations and --with-lto are enabled. If your VM uses a small /tmp tmpfs, see the troubleshooting section for the Disk quota exceeded workaround.

Keep /usr/bin/python3 pointing at the release default. Ubuntu system tools like apt depend on the original interpreter (python3.10 on 22.04, python3.12 on 24.04, python3.13 on 26.04), so always call python3.12 explicitly instead.

Update Ubuntu Before Installing Python 3.12

Refresh package indexes before installing Python 3.12 so Ubuntu resolves the latest dependencies and repository metadata correctly.

sudo apt update

This guide uses sudo for commands that need root privileges. If your user is not in the sudoers file yet, run the commands as root or follow the guide on how to add and manage sudo users on Ubuntu.

Expected output (example):

Hit:1 http://archive.ubuntu.com/ubuntu noble InRelease
Hit:2 http://security.ubuntu.com/ubuntu noble-security InRelease
Reading package lists... Done

Optionally upgrade installed packages before adding a PPA or compiling from source:

sudo apt upgrade

Continue with the installation method that matches your Ubuntu release.

Install Python 3.12 from Ubuntu Repositories (24.04 LTS)

Use this method on Ubuntu 24.04 LTS to receive Canonical security updates without adding a third-party repository:

sudo apt install python3.12 python3.12-venv python3.12-dev

Verify the installation:

python3.12 --version
Python 3.12.3

Run a smoke test to ensure core modules loaded correctly:

python3.12 -c "import ssl, sqlite3, bz2; print('Python 3.12 ready on Ubuntu')"
Python 3.12 ready on Ubuntu

Install Python 3.12 via Deadsnakes PPA (Ubuntu 22.04 LTS)

Use this method only on Ubuntu 22.04 LTS. Ubuntu 24.04 LTS should use the official repositories, and Ubuntu 26.04 LTS requires the source build section because the PPA does not publish 26.04 packages.

Install the PPA helper if it is not already present:

sudo apt install software-properties-common

Add the Deadsnakes PPA and refresh package lists:

sudo add-apt-repository ppa:deadsnakes/ppa -y
sudo apt update

The -y flag auto-confirms the prompt so the repository is added without interruption.

apt-cache policy python3.12
python3.12:
  Installed: (none)
  Candidate: 3.12.12-1+jammy1
  Version table:
     3.12.12-1+jammy1 500
        500 https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu jammy/main amd64 Packages

Install Python 3.12 from the PPA:

sudo apt install python3.12 python3.12-venv python3.12-dev

The Deadsnakes PPA maintainer states there is “no guarantee of timely updates in case of security problems.” For production systems, prefer Ubuntu 24.04 LTS official repositories or a source build with your own CVE monitoring. Deadsnakes works well for development, testing, and non-critical workloads.

Verify the installation:

python3.12 --version
Python 3.12.12

Install Optional Python 3.12 Packages on Ubuntu

Install additional modules when your workflow demands debugging tools, GUI bindings, or compatibility layers. The core installation already includes python3.12-venv and python3.12-dev, so only add these if you need specific functionality. Package names differ between Ubuntu 24.04 LTS and Ubuntu 22.04 LTS.

python3.12-dbg provides debugging symbols for profiling, crash analysis, or gdb attachments:

sudo apt install python3.12-dbg

GNU dbm bindings (gdbm) install via versioned packages on 22.04 LTS and unversioned packages on 24.04 LTS:

Ubuntu 24.04 LTS:

sudo apt install python3-gdbm

Ubuntu 22.04 LTS (Deadsnakes PPA):

sudo apt install python3.12-gdbm

Tkinter GUI bindings follow the same naming pattern:

Ubuntu 24.04 LTS:

sudo apt install python3-tk

Ubuntu 22.04 LTS (Deadsnakes PPA):

sudo apt install python3.12-tk

Python 3.12 removed the legacy distutils module per PEP 632. The python3.12-distutils package does not exist on Ubuntu 24.04 or in the Deadsnakes PPA. Projects that still require distutils should migrate to setuptools (which includes a distutils compatibility layer) or use modern build tools like Poetry or Hatchling.

Combine multiple packages in one apt invocation to save time:

# Ubuntu 24.04 LTS
sudo apt install python3.12-dbg python3-gdbm python3-tk

# Ubuntu 22.04 LTS (Deadsnakes PPA)
sudo apt install python3.12-dbg python3.12-gdbm python3.12-tk

The python3.12-full meta-package is available on Ubuntu 24.04 and in the Deadsnakes PPA. Use python3.12-full if you want the full optional set for Python 3.12, while python3-full tracks the default interpreter on Ubuntu 24.04.

Compile Python 3.12 from Source on Ubuntu

Compile Python 3.12 manually on Ubuntu 26.04 LTS or when you need custom prefixes or independent upgrade cycles. Source builds install alongside Ubuntu’s system Python without overwriting /usr/bin/python3.

Download Python 3.12 Source Tarball

Download the latest 3.12.x release directly from python.org and keep it in a dedicated build directory in your home folder so updates are repeatable:

mkdir -p ~/python3.12-build
cd ~/python3.12-build
PY312_VERSION="$(curl -s https://www.python.org/ftp/python/ | grep -oE '3\.12\.[0-9]+' | sort -V | tail -1)"
if [ -z "$PY312_VERSION" ]; then
  echo "Error: could not determine the latest Python 3.12 release."
  exit 1
fi
echo "Downloading Python ${PY312_VERSION}..."
wget https://www.python.org/ftp/python/${PY312_VERSION}/Python-${PY312_VERSION}.tar.xz
wget https://www.python.org/ftp/python/${PY312_VERSION}/Python-${PY312_VERSION}.tar.xz.asc

Verify the Python 3.12 Source GPG Signature

Python source releases are signed by Thomas Wouters, the release manager for Python 3.12. Import his public key, verify the fingerprint, and then verify the tarball before extracting:

# Import the Python 3.12 release manager's key (Thomas Wouters) over HTTPS
curl -fsSL https://keys.openpgp.org/vks/v1/by-fingerprint/7169605F62C751356D054A26A821E680E5FA6305 | gpg --import

# Confirm the fingerprint matches before verifying the tarball
gpg --fingerprint A821E680E5FA6305

# Verify the signature
gpg --verify Python-${PY312_VERSION}.tar.xz.asc Python-${PY312_VERSION}.tar.xz

Expected output confirming the correct fingerprint and a valid signature (timestamps vary by release; any Thomas Wouters UID is acceptable if the fingerprint matches):

pub   rsa4096 ... [SC]
      7169 605F 62C7 5135 6D05  4A26 A821 E680 E5FA 6305
uid           [ unknown] Thomas Wouters <thomas@python.org>
gpg: Good signature from "Thomas Wouters <thomas@python.org>"

Extract the verified tarball:

tar -xf Python-${PY312_VERSION}.tar.xz
cd Python-${PY312_VERSION}

If the discovery command cannot reach python.org (for example, on air-gapped hosts), check the Python.org downloads page for the latest 3.12.x maintenance release and substitute that version manually in the URLs. If your network blocks keys.openpgp.org, import the key from https://github.com/Yhg1s.gpg as a fallback, then verify the fingerprint before proceeding. You can also verify the SHA256 checksum on the downloads page if GPG verification still fails.

Install Python 3.12 Source Build Dependencies on Ubuntu

Before compiling, install the libraries required for SSL, compression, readline, Tk, and UUID support:

sudo apt install build-essential zlib1g-dev libncurses-dev libgdbm-dev libnss3-dev \
libssl-dev libsqlite3-dev libreadline-dev libffi-dev libbz2-dev liblzma-dev uuid-dev \
tk-dev pkg-config -y

The -y flag automatically confirms the installation prompt, which is useful for scripted builds.

These package names exist on Ubuntu 22.04 LTS, 24.04 LTS, and 26.04 LTS, so the same command works regardless of which release you are compiling on.

Configure and Build Python 3.12

With dependencies installed, compile Python with optimization flags and a custom prefix so it installs alongside the system interpreter:

./configure --enable-optimizations --with-lto --with-ensurepip=install --prefix=/usr/local/python3.12

The configure script checks your system for required libraries and build tools. Watch for lines confirming optional modules will be built:

checking for openssl/ssl.h in /usr... yes
checking for X509_VERIFY_PARAM_set1_host in libssl... yes
checking for --with-ssl-default-suites... python
checking for sqlite3.h... yes
checking for SQLITE_OMIT_COMPLETE in sqlite3.h... no
checking for lzma.h... yes
configure: creating ./config.status
config.status: creating Makefile
config.status: creating Misc/python.pc

If configure reports missing headers (for example, checking for openssl/ssl.h... no), install the corresponding -dev package and rerun configure before proceeding.

Compile and Install Python 3.12

Compile Python using all available CPU cores, then install to the custom prefix:

make -j"$(nproc)"
sudo make altinstall

The --prefix flag installs Python under /usr/local/python3.12, --with-lto enables Link Time Optimization for slightly faster runtime performance, and --with-ensurepip=install bundles pip. Compilation takes 5-15 minutes depending on your hardware when --enable-optimizations is enabled because it runs the test suite to profile the build.

Using make altinstall instead of make install keeps /usr/bin/python3 untouched while adding /usr/local/python3.12/bin/python3.12.

If the build fails with Disk quota exceeded while writing to /tmp (common on VMs using a small tmpfs for /tmp), rerun make with a larger temp directory, for example TMPDIR=$HOME/tmp make -j"$(nproc)". The troubleshooting section below includes a tested recovery workflow.

Register Libraries and Test the Python 3.12 Build on Ubuntu

After compilation completes, point the dynamic linker at your custom installation, reload the cache, and create a convenience symlink:

echo '/usr/local/python3.12/lib' | sudo tee /etc/ld.so.conf.d/python3.12.conf
sudo ldconfig
sudo ln -sf /usr/local/python3.12/bin/python3.12 /usr/local/bin/python3.12

Verify the build and confirm critical modules compiled successfully:

python3.12 --version
python3.12 -c "import ssl, sqlite3, bz2; print('Source build is healthy')"
Python 3.12.12
Source build is healthy

If the second command fails, reinstall the missing -dev packages, rerun ./configure, and rebuild the installation.

Install Pip for Python 3.12 on Ubuntu

Pip availability depends on how you installed Python 3.12. Use the method below that matches your Ubuntu release and installation path.

Install Pip via APT (Ubuntu 24.04 LTS)

On Ubuntu 24.04 LTS, the python3-pip package targets Python 3.12 and keeps pip updated through APT:

sudo apt install python3-pip

Verify the installation:

python3.12 -m pip --version
pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12)

Install Pip with ensurepip (PPA or Source Builds)

If you installed Python 3.12 from the Deadsnakes PPA or from source, use ensurepip to bootstrap pip in the Python 3.12 site-packages directory:

python3.12 -m ensurepip --upgrade
python3.12 -m pip --version
pip 25.0.1 from /home/username/.local/lib/python3.12/site-packages/pip (python 3.12)

On Ubuntu 22.04 LTS Deadsnakes installs, ensurepip commonly seeds pip into your user site under ~/.local/. Source builds configured with --with-ensurepip=install usually already include pip under your custom prefix, and rerunning ensurepip upgrades that bundled pip.

Pip versions vary by Ubuntu release and installation method. The APT-installed pip version tracks Canonical’s packaging cycle, while ensurepip bundles the pip version available when your Python installation was built.

Bootstrap Pip for Python 3.12 with get-pip.py

Alternatively, for air-gapped systems or custom builds, download the latest bootstrap script and execute it with Python 3.12:

wget https://bootstrap.pypa.io/get-pip.py
python3.12 get-pip.py
rm get-pip.py

This approach installs pip, setuptools, and wheel side by side. Keep the script up to date whenever you rerun the procedure.

Manage Packages with python3.12 -m pip

Once pip is installed, always call pip through the desired interpreter to avoid cross-version confusion:

python3.12 -m pip install package_name
python3.12 -m pip install --upgrade package_name
python3.12 -m pip uninstall package_name

Replace package_name with libraries such as numpy, fastapi, or django. For a comprehensive guide to managing packages, see Install pip on Ubuntu. Using python3.12 -m pip ensures you target Python 3.12; use a virtual environment to keep packages isolated from the system interpreter.

Ubuntu 24.04 LTS and 26.04 LTS mark the system Python as externally managed (PEP 668). If you run python3.12 -m pip install <package> against the system interpreter on those releases, pip will error unless you work inside a virtual environment or pass --break-system-packages. Use --break-system-packages only when you understand the risks because it bypasses apt protections and can break system tools. The Deadsnakes Python 3.12 packages on Ubuntu 22.04 LTS do not ship an externally managed marker.

Use Python 3.12 Virtual Environments on Ubuntu

Virtual environments prevent package conflicts between projects by providing isolated site-packages directories. For comprehensive coverage of virtual environment workflows, see the dedicated Python virtual environment guide for Ubuntu.

Create a Python 3.12 Virtual Environment

First, create a dedicated directory for your environments (for example ~/venvs) and provision a new one with python3.12:

python3.12 -m venv ~/venvs/py312

Replace ~/venvs/py312 with any path meaningful to your workflow.

Activate the Python 3.12 Virtual Environment

Next, activate the environment so your shell points python and pip to the local installation:

source ~/venvs/py312/bin/activate

The prompt changes to include the environment name, indicating successful activation. Install packages as needed while the environment remains active.

Deactivate the Python 3.12 Virtual Environment

When finished, exit the virtual environment by running:

deactivate

The shell returns to the system Python context, ensuring new sessions do not inherit the environment accidentally.

Troubleshooting Python 3.12 Installation Issues on Ubuntu

Missing Development Headers During Python 3.12 Compilation on Ubuntu

If development headers are missing, Python builds but some standard library modules fail to import.

A common error looks like:

ModuleNotFoundError: No module named '_ssl'

Confirm which modules are failing:

python3.12 -c "import ssl, sqlite3, bz2"
ModuleNotFoundError: No module named '_ssl'

Install the missing headers and rebuild:

sudo apt install libssl-dev libbz2-dev libsqlite3-dev
./configure --enable-optimizations --with-lto --with-ensurepip=install --prefix=/usr/local/python3.12
make -j"$(nproc)"
sudo make altinstall

Verify the modules load correctly:

python3.12 -c "import ssl, sqlite3, bz2; print('Modules loaded')"
Modules loaded

Source Build Fails with Disk Quota Exceeded in /tmp on Ubuntu

On some Ubuntu VMs, /tmp is mounted as a small tmpfs. Python 3.12 source builds with --enable-optimizations and --with-lto can exhaust that temporary space during the compile stage even when your root filesystem has plenty of free space.

lto1: fatal error: error writing to /tmp/cc*.s: Disk quota exceeded
compilation terminated.

Create a writable temp directory in your home folder and rerun the build using TMPDIR:

mkdir -p ~/tmp
TMPDIR=$HOME/tmp make -j"$(nproc)"
sudo make altinstall

Verify that the rebuilt interpreter starts correctly:

python3.12 --version
Python 3.12.12

Python 3.12 Shared Library Linking Errors on Ubuntu

When running python3.12 after source compilation, you may encounter an error stating that the shared library cannot be found.

python3.12: error while loading shared libraries: libpython3.12.so.1.0: cannot open shared object file: No such file or directory

First verify the library exists and note its path:

find /usr/local -name "libpython3.12.so*"

Example output:

/usr/local/python3.12/lib/libpython3.12.so.1.0

Register the library path and refresh the linker cache:

echo "/usr/local/python3.12/lib" | sudo tee /etc/ld.so.conf.d/python3.12.conf
sudo ldconfig

Verify the interpreter starts correctly:

python3.12 --version
Python 3.12.12

PIP Externally Managed Environment Errors on Ubuntu

On Ubuntu 24.04 LTS and 26.04 LTS, pip blocks installs to the system interpreter (python3) without a virtual environment. This does not affect Python 3.12 installed from the Deadsnakes PPA or source.

error: externally-managed-environment

X This environment is externally managed
-> To install Python packages system-wide, try apt install
   xyz, where xyz is the package you are trying to
   install.

If you wish to install a Python package using pip, you must
create a virtual environment first.

Create a venv instead of bypassing protections:

python3 -m venv myproject_env
source myproject_env/bin/activate
pip install package-name

Only use --break-system-packages in disposable environments:

python3 -m pip install --break-system-packages package-name

APT Breaking After Installing Python 3.12 from Deadsnakes PPA on Ubuntu

If apt update or apt install fails with Python-related errors after adding the Deadsnakes PPA, the /usr/bin/python3 symlink may have been changed. A typical error looks like:

ModuleNotFoundError: No module named 'apt_pkg'

Check the /usr/bin/python3 symlink:

ls -la /usr/bin/python3

Expected output by release:

22.04 LTS (Jammy):

lrwxrwxrwx 1 root root 10 Aug  8  2024 /usr/bin/python3 -> python3.10

24.04 LTS (Noble):

lrwxrwxrwx 1 root root 10 Nov 12 12:15 /usr/bin/python3 -> python3.12

26.04 LTS (Resolute):

lrwxrwxrwx 1 root root 10 Aug 21 15:18 /usr/bin/python3 -> python3.13

If the symlink points elsewhere, restore the original target for your release:

# Ubuntu 24.04 LTS
sudo ln -sf /usr/bin/python3.12 /usr/bin/python3

# Ubuntu 22.04 LTS
sudo ln -sf /usr/bin/python3.10 /usr/bin/python3

# Ubuntu 26.04 LTS
sudo ln -sf /usr/bin/python3.13 /usr/bin/python3

Always match the symlink to your release’s default interpreter and invoke Python 3.12 explicitly using python3.12 rather than modifying the python3 symlink on production systems.

Python 3.12 Virtual Environment Creation Fails on Ubuntu

When python3.12 -m venv fails with an ensurepip error, the python3.12-venv package is missing.

The virtual environment was not created successfully because ensurepip is not available. On Debian/Ubuntu systems, you need to install the python3.12-venv package.

Install the missing package:

sudo apt install python3.12-venv

Verify that venv creation succeeds:

python3.12 -m venv ~/venvs/py312
source ~/venvs/py312/bin/activate
python --version
Python 3.12.12

Patch versions differ between Ubuntu 22.04 LTS and 24.04 LTS, so your output may show a different 3.12.x value.

For source-compiled installations, ensure you configured with --with-ensurepip=install before running make. If you skipped this flag, bootstrap pip manually using the get-pip.py script as described in the pip installation section.

GPG Signature Verification Fails for Python 3.12 Source Builds

If signature verification fails with “No public key” or “Can’t check signature” errors, the release manager’s key was not imported correctly.

gpg: Can't check signature: No public key

Re-import the Python 3.12 release manager’s key and confirm the fingerprint:

curl -fsSL https://keys.openpgp.org/vks/v1/by-fingerprint/7169605F62C751356D054A26A821E680E5FA6305 | gpg --import
gpg --fingerprint A821E680E5FA6305

Expected fingerprint (must match exactly):

7169 605F 62C7 5135 6D05  4A26 A821 E680 E5FA 6305

If your network blocks keys.openpgp.org, fall back to curl -fsSL https://github.com/Yhg1s.gpg | gpg --import, then verify the fingerprint above. As a last resort, use a traditional keyserver command such as gpg --keyserver hkps://keys.openpgp.org --recv-keys A821E680E5FA6305. You can also compare your checksum output with the hash on the official downloads page:

sha256sum Python-*.tar.xz

Update or Remove Python 3.12 on Ubuntu

Update Python 3.12 Packages on Ubuntu

sudo apt update
sudo apt install --only-upgrade python3.12 python3.12-venv python3.12-dev

Verify the updated version:

python3.12 --version
Python 3.12.12

Update Python 3.12 Source Builds on Ubuntu (Manual Script)

For source-compiled installations, use a dedicated update script to rebuild cleanly. Save the following script to your build directory:

cat <<'EOF' > ~/python3.12-build/update-python3.12.sh
#!/usr/bin/env bash
# Python 3.12 Source Update Script
# This script downloads and compiles the latest Python 3.12 release
set -euo pipefail  # Exit on any error, undefined variable, or pipe failure

# Check for required build tools
for cmd in curl tar make gcc; do
  if ! command -v "$cmd" > /dev/null 2>&1; then
    echo "Error: $cmd is required but not installed."
    echo "Run: sudo apt install build-essential curl"
    exit 1
  fi
done

BUILD_DIR="$HOME/python3.12-build"
PREFIX="/usr/local/python3.12"

# Confirm build directory exists
if [ ! -d "$BUILD_DIR" ]; then
  echo "Error: $BUILD_DIR does not exist."
  echo "Create it with: mkdir -p $BUILD_DIR"
  exit 1
fi

# Avoid /tmp tmpfs quota failures on small VMs by using a build-local temp directory
TMPDIR="${TMPDIR:-$BUILD_DIR/tmp}"
mkdir -p "$TMPDIR"
export TMPDIR

# Fetch the latest 3.12.x version number from python.org
echo "Checking for latest Python 3.12 version..."
PY312_VERSION="$(curl -s https://www.python.org/ftp/python/ | grep -oE '3\\.12\\.[0-9]+' | sort -V | tail -1)"
if [ -z "$PY312_VERSION" ]; then
  echo "Error: could not determine the latest Python 3.12 release."
  exit 1
fi
echo "Latest available: Python ${PY312_VERSION}"

# Get currently installed version (strip "Python " prefix for comparison)
INSTALLED_VERSION="$(python3.12 --version 2>/dev/null | cut -d' ' -f2 || echo "none")"
echo "Currently installed: Python ${INSTALLED_VERSION}"

# Skip rebuild if already up to date
if [ "$INSTALLED_VERSION" = "$PY312_VERSION" ]; then
  echo ""
  echo "Already up to date - no rebuild needed."
  exit 0
fi
echo ""
echo "Update available: ${INSTALLED_VERSION} -> ${PY312_VERSION}"
echo ""

# Download and extract source
cd "$BUILD_DIR"
rm -rf "Python-${PY312_VERSION}"
echo "Downloading Python ${PY312_VERSION}..."
curl -fsSLO "https://www.python.org/ftp/python/${PY312_VERSION}/Python-${PY312_VERSION}.tar.xz"
echo "Extracting source..."
tar -xf "Python-${PY312_VERSION}.tar.xz"
cd "Python-${PY312_VERSION}"

# Configure, compile, and install
echo "Configuring build (this takes a few minutes)..."
./configure --enable-optimizations --with-lto --with-ensurepip=install --prefix="$PREFIX"
echo "Compiling Python (5-15 minutes depending on hardware)..."
make -j"$(nproc)"
echo "Installing (requires sudo)..."
sudo make altinstall
sudo ln -sf "$PREFIX/bin/python3.12" /usr/local/bin/python3.12
sudo ldconfig

echo ""
echo "Update complete!"
python3.12 --version
EOF
chmod +x ~/python3.12-build/update-python3.12.sh

Run the script when you want to update to the latest 3.12.x release:

~/python3.12-build/update-python3.12.sh

Expected output when an update is available:

Checking for latest Python 3.12 version...
Latest available: Python 3.12.12
Currently installed: Python 3.12.11

Update available: 3.12.11 -> 3.12.12

Downloading Python 3.12.12...
Extracting source...
Configuring build (this takes a few minutes)...
Compiling Python (5-15 minutes depending on hardware)...
Installing (requires sudo)...

Update complete!
Python 3.12.12

If already on the latest version, the script exits early without recompiling:

Checking for latest Python 3.12 version...
Latest available: Python 3.12.12
Currently installed: Python 3.12.12

Already up to date - no rebuild needed.

Avoid automating source builds with cron. Compilation can fail due to missing dependencies or network issues, so run the script manually and review the output each time.

Remove Python 3.12 Packages on Ubuntu

Python 3.12 is the system interpreter on Ubuntu 24.04 LTS. Removing python3.12 will break apt, software-properties-common, cloud-init, and essential system utilities. Never run the uninstall commands below on Ubuntu 24.04 LTS. This section applies only to Ubuntu 22.04 LTS users who installed Python 3.12 via Deadsnakes PPA or users who compiled from source on Ubuntu 26.04 LTS.

For Ubuntu 22.04 LTS users who installed Python 3.12 via Deadsnakes PPA and no longer need it, uninstall the packages:

sudo apt remove --purge python3.12 python3.12-venv python3.12-dev
sudo apt autoremove

This command is safe on Ubuntu 22.04 LTS because the system interpreter remains Python 3.10. Verify your Ubuntu version before proceeding:

lsb_release -rs
22.04

Remove the Deadsnakes PPA

Additionally, remove the Deadsnakes PPA if your system no longer requires Python 3.12 packages:

sudo add-apt-repository --remove ppa:deadsnakes/ppa -y
sudo apt update

Verify the Deadsnakes PPA entry is removed:

ls /etc/apt/sources.list.d | grep deadsnakes || echo "Deadsnakes PPA entry removed"
Deadsnakes PPA entry removed

Clean Up Source-Compiled Python 3.12 Installations

For source-compiled installations, delete the installation directory, symlink, and ld.so entry.

The following commands permanently delete the source-compiled Python 3.12 installation and the build directory. Back up the installation first if you want to keep locally installed modules, for example: cp -a /usr/local/python3.12 ~/python3.12-backup.

sudo rm -rf /usr/local/python3.12
sudo rm -f /usr/local/bin/python3.12
sudo rm -f /etc/ld.so.conf.d/python3.12.conf
rm -rf ~/python3.12-build
sudo ldconfig

If you used a custom prefix like --prefix=/opt/python3.12, remove that directory instead:

sudo rm -rf /opt/python3.12
sudo ldconfig

Frequently Asked Questions About Python 3.12 on Ubuntu

Does Ubuntu 24.04 use Python 3.12 by default?

Yes. Ubuntu 24.04 LTS ships Python 3.12 as the default python3 interpreter, so /usr/bin/python3 points to python3.12. Confirm with python3 --version or ls -la /usr/bin/python3.

Can I install Python 3.12 on Ubuntu 22.04 without compiling from source?

Yes. Ubuntu 22.04 LTS can install Python 3.12 from the Deadsnakes PPA. The default Jammy repositories do not provide a python3.12 package, so add ppa:deadsnakes/ppa and then install python3.12 with APT.

Can I install Python 3.12 on Ubuntu 20.04?

This article focuses on Ubuntu 22.04, 24.04, and 26.04. If you must stay on Ubuntu 20.04, use a source build and do not replace /usr/bin/python3. Upgrading to a newer Ubuntu LTS is usually the cleaner option.

Why is python3.12-distutils missing on Ubuntu?

Python 3.12 removed distutils from the standard library (PEP 632), so neither Ubuntu nor Deadsnakes ship a python3.12-distutils or python3-distutils package on 24.04. Run pip install setuptools inside a virtual environment to get a distutils compatibility layer that older packages require.

Why does pip show “externally-managed-environment” on Ubuntu 24.04 or 26.04?

Ubuntu 24.04 and 26.04 implement PEP 668 for the system Python, so pip blocks global installs to protect APT-managed files. Use python3.12 -m venv for project environments, or use --break-system-packages only when you accept the risk.

How do I upgrade from Python 3.10 to Python 3.12 on Ubuntu?

On Ubuntu 22.04 LTS, add the Deadsnakes PPA and install python3.12 alongside the existing Python 3.10. Do not remove or replace /usr/bin/python3. On Ubuntu 24.04 LTS, Python 3.12 is already the default interpreter so no upgrade is needed. Use python3.12 explicitly or activate a virtual environment to switch your projects.

Does this guide work for Python 3.12 on WSL (Windows Subsystem for Linux)?

Yes. WSL runs a full Ubuntu userspace, so every method in this guide applies directly. Open your WSL terminal, check your Ubuntu version with lsb_release -cs, and follow the matching instructions.

How do I install the python3.12-venv package on Ubuntu?

On Ubuntu 24.04 LTS run sudo apt install python3.12-venv since the package is in the default repositories. On Ubuntu 22.04 LTS, add the Deadsnakes PPA first and then run the same APT command. The package provides the venv module that python3.12 -m venv needs.

Useful Python 3.12 and Ubuntu References

Use these upstream references when you need release schedules, changelogs, or the current Python 3.12 source tarball.

Conclusion

Python 3.12 is running alongside Ubuntu’s system interpreter with pip available and virtual environments ready for project isolation. Keep /usr/bin/python3 pointed at the release default, and use the update script or apt upgrade to stay current on security patches. For related workflows, see how to install pip on Ubuntu or create Python virtual environments on Ubuntu.

Found this guide useful?

Support LinuxCapable to keep tutorials free and up to date.

Buy me a coffee Buy me a coffee

2 thoughts on “How to Install Python 3.12 on Ubuntu (26.04, 24.04, 22.04)”

    • Thanks for sharing this, Pasha. Python 3.12 isn’t available in Ubuntu’s universe repository for 22.04 LTS or 20.04 LTS – only Ubuntu 24.04 LTS ships it by default in the main repository. For Ubuntu 22.04 LTS, you’ll need the Deadsnakes PPA instead:

      sudo add-apt-repository ppa:deadsnakes/ppa -y
      sudo apt update
      sudo apt install python3.12 python3.12-venv python3.12-dev

      The guide now covers this installation method in detail under the “Install Python 3.12 via Deadsnakes PPA” section. You can verify package availability for different Ubuntu versions at packages.ubuntu.com.

      Reply
Before commenting, please review our Comments Policy.
Formatting tips for your comment

You can use basic HTML to format your comment. Useful tags currently allowed:

You type Result
<code>command</code> command
<strong>bold</strong> bold
<em>italic</em> italic
<blockquote>quote</blockquote> quote block

Leave a Comment

We read and reply to every comment - let us know how we can help or improve this guide.

Let us know you are human: