How to Install Python 3.12 on Ubuntu 26.04, 24.04 and 22.04

Install Python 3.12 on Ubuntu 26.04, 24.04 and 22.04 via official repos, Deadsnakes PPA or source. Covers pip, venv, security updates.

Last updatedAuthorJoshua JamesRead time13 minGuide typeUbuntuDiscussion3 comments

To install Python 3.12 on Ubuntu, match the method to your LTS release. Ubuntu 24.04 already ships Python 3.12, Ubuntu 22.04 uses the Deadsnakes PPA for python3.12, and Ubuntu 26.04 needs a source build because its default interpreter is Python 3.14 and no python3.12 archive package is published.

Each path installs Python 3.12 without replacing the release-owned /usr/bin/python3. Python 3.12 receives upstream source-only security fixes until October 2028 per PEP 693, so the guide also covers pip, virtual environments, updates, removal, and troubleshooting for the supported Ubuntu paths.

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.14 and does not ship Python 3.12 packages in the default repositories.

Ubuntu ReleaseDefault python3Python 3.12 AvailabilityRecommended Method
Ubuntu 26.04 LTSPython 3.14.xNo Ubuntu archive packageCompile Python 3.12 from source
Ubuntu 24.04 LTSPython 3.12.xAvailable in Ubuntu repositoriesInstall from Ubuntu repositories
Ubuntu 22.04 LTSPython 3.10.xAvailable from Deadsnakes PPA, not Jammy default repositoriesInstall 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 SourceUpdate BehaviorBest For
Ubuntu repositories (24.04 LTS)Ubuntu PackagesUbuntu 24.04 package branchUpdated with normal apt upgrade security and maintenance updatesUbuntu 24.04 LTS systems that want distro-managed Python 3.12
Deadsnakes PPA (22.04 LTS)Deadsnakes PPACommunity-maintained Jammy packageAPT-managed updates from the PPA, with community security timingUbuntu 22.04 LTS systems that need Python 3.12 alongside Python 3.10
Source build (26.04 LTS or custom)Python.org sourceLatest stable upstream 3.12.x sourceManual rebuilds when a new 3.12.x source release appearsUbuntu 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.

Protect Ubuntu’s Default Python Before Installing Python 3.12

Ubuntu packages and management tools expect the package-owned /usr/bin/python3 interpreter for that release. Replacing that symlink, using update-alternatives against /usr/bin/python3, or copying a custom interpreter over it can break APT-related helpers, add-apt-repository, command-not-found, desktop update utilities, cloud-init, and Python modules compiled for Ubuntu’s default ABI.

  • Use versioned commands: Run python3.12 directly, or point a project virtual environment, service file, cron job, or shebang at the versioned interpreter.
  • Keep project packages isolated: Create environments with python3.12 -m venv and install dependencies inside the activated environment. Use pipx for standalone Python CLI tools.
  • Keep source builds separate: Use make altinstall with a dedicated prefix such as /usr/local/python3.12. Do not install a custom build over /usr.
  • Do not remove the system runtime: On Ubuntu 24.04 LTS, python3.12 is the system runtime. On Ubuntu 22.04 LTS, Python 3.10 is the system runtime, and on Ubuntu 26.04 LTS, Python 3.14 is the system runtime. The release-owned python3 must stay in place.

Ubuntu 20.04 and WSL Notes for Python 3.12

This article focuses on Ubuntu 26.04, 24.04, and 22.04. Ubuntu 20.04 is outside this command set; if you must keep a 20.04 host running, use a source build and keep /usr/bin/python3 pointed at the original Python 3.8 interpreter. WSL follows the same release logic as a normal Ubuntu install, so check the Ubuntu version inside WSL before choosing a method.

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
. /etc/os-release
printf '%s\n' "$VERSION_ID"

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

On an Ubuntu 22.04 system before adding the PPA, relevant output looks like this:

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. If your VM uses a small /tmp tmpfs, see the troubleshooting section for the temporary-space 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.14 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

Package installation and files under /usr/local require 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.

Relevant output includes:

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.13-1+jammy1
  Version table:
     3.12.13-1+jammy1 500
        500 https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu jammy/main amd64 Packages

Install Python 3.12 from the PPA. This adds a versioned python3.12 command beside Ubuntu 22.04’s original Python 3.10 interpreter.

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.13

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.

Install Python 3.12 Source Build Dependencies on Ubuntu

Install the compiler, development headers, download tools, and GPG utilities required for a complete Python source build:

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 curl gpg

These package names resolve on Ubuntu 22.04 LTS, 24.04 LTS, and 26.04 LTS. Source builds are the normal path on Ubuntu 26.04, but the same dependency set also works when you deliberately choose a custom Python 3.12 build on an older LTS release.

Download the Latest Stable Python 3.12 Source Tarball

Download the latest stable 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 -fsSL https://www.python.org/ftp/python/ |
  grep -oE 'href="3\.12\.[0-9]+/"' |
  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}..."
curl -fSLO "https://www.python.org/ftp/python/${PY312_VERSION}/Python-${PY312_VERSION}.tar.xz"
curl -fSLO "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

Relevant output confirming the correct fingerprint and a valid signature includes these lines. Timestamps vary by release, and 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.

Configure and Build Python 3.12

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

./configure --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, and --with-ensurepip=install bundles pip for the custom interpreter. A standard build is faster and less failure-prone than a profile-guided build, especially on smaller VMs.

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 while writing temporary compiler files under /tmp, rerun make with a larger temp directory, for example TMPDIR=$HOME/tmp make -j"$(nproc)". The troubleshooting section includes a 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, lzma, zlib, readline, uuid, ctypes, dbm.gnu, tkinter; print('Source build is healthy')"
Python 3.12.13
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:

curl -fSLO 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 marks its system Python 3.12 as externally managed (PEP 668), and Ubuntu 26.04 LTS marks its system Python 3.14 the same way. A source-built /usr/local/python3.12 interpreter is separate from APT, but you should still use virtual environments for project 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, lzma, zlib, readline, uuid, ctypes, dbm.gnu, tkinter"
ModuleNotFoundError: No module named '_ssl'

Install the missing headers and rebuild:

sudo apt install libssl-dev libbz2-dev libsqlite3-dev
./configure --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, lzma, zlib, readline, uuid, ctypes, dbm.gnu, tkinter; print('Modules loaded')"
Modules loaded

Source Build Fails Because /tmp Has No Space on Ubuntu

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

cc1: fatal error: error writing to /tmp/cc*.s: No space left on device
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.13

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.13

PIP Externally Managed Environment Errors on Ubuntu

On Ubuntu 24.04 LTS and 26.04 LTS, pip blocks installs to the release-owned system interpreter (python3) without a virtual environment. Ubuntu 24.04’s system interpreter is Python 3.12, while Ubuntu 26.04’s system interpreter is Python 3.14. This does not affect Python 3.12 installed from the Deadsnakes PPA or from a custom source prefix.

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 and System Tool Errors After Replacing Ubuntu’s Default Python

If add-apt-repository, command-not-found, desktop update tools, cloud-init, or other Python-based Ubuntu utilities fail after Python changes, check whether the system /usr/bin/python3 symlink was modified. It should point to Ubuntu’s package-owned default interpreter, not a manually installed Python 3.12 binary on Ubuntu 22.04 or 26.04.

ModuleNotFoundError: No module named 'apt_pkg'

Check the /usr/bin/python3 symlink target:

readlink -f /usr/bin/python3

The release-owned targets are:

22.04 LTS (Jammy):

/usr/bin/python3.10

24.04 LTS (Noble):

/usr/bin/python3.12

26.04 LTS (Resolute):

/usr/bin/python3.14

Restore the package-owned symlink by reinstalling the core Python packages:

sudo apt install --reinstall python3-minimal python3-apt python3-distro-info
readlink -f /usr/bin/python3

Always call Python 3.12 explicitly with python3.12 instead of altering system links. Do not use update-alternatives to manage /usr/bin/python3. On Ubuntu 24.04, python3 already points to Python 3.12 through Ubuntu’s packaged default.

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.13

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.13

Ubuntu 24.04 LTS remains on its distro-managed 3.12.3 package branch, while the Deadsnakes PPA and source builds can show a newer upstream 3.12.x patch release.

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 gpg; 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 gpg"
    exit 1
  fi
done

BUILD_DIR="$HOME/python3.12-build"
PREFIX="/usr/local/python3.12"
KEY_FINGERPRINT="7169605F62C751356D054A26A821E680E5FA6305"
KEY_FINGERPRINT_SPACED="7169 605F 62C7 5135 6D05  4A26 A821 E680 E5FA 6305"
KEY_ID="A821E680E5FA6305"

# 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 stable 3.12.x version number from python.org
echo "Checking for latest Python 3.12 version..."
PY312_VERSION="$(
  curl -fsSL https://www.python.org/ftp/python/ |
  grep -oE 'href="3\.12\.[0-9]+/"' |
  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 "Checking Python 3.12 release signing key..."
if ! gpg --list-keys "$KEY_ID" > /dev/null 2>&1; then
  echo "Importing Python 3.12 release signing key..."
  curl -fsSL "https://keys.openpgp.org/vks/v1/by-fingerprint/${KEY_FINGERPRINT}" | gpg --import
fi
if ! gpg --fingerprint "$KEY_ID" | grep -Fq "$KEY_FINGERPRINT_SPACED"; then
  echo "Error: Python 3.12 release signing key fingerprint did not match."
  exit 1
fi
echo "Python release signing key fingerprint verified."
echo "Downloading Python ${PY312_VERSION}..."
curl -fSLO "https://www.python.org/ftp/python/${PY312_VERSION}/Python-${PY312_VERSION}.tar.xz"
curl -fSLO "https://www.python.org/ftp/python/${PY312_VERSION}/Python-${PY312_VERSION}.tar.xz.asc"
echo "Verifying source signature..."
gpg --verify "Python-${PY312_VERSION}.tar.xz.asc" "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 --with-ensurepip=install --prefix="$PREFIX"
echo "Compiling Python..."
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

Relevant output when an update is available includes:

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

Update available: 3.12.12 -> 3.12.13

Checking Python 3.12 release signing key...
Python release signing key fingerprint verified.
Downloading Python 3.12.13...
Verifying source signature...
Extracting source...
Configuring build (this takes a few minutes)...
Compiling Python...
Installing (requires sudo)...

Update complete!
Python 3.12.13

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

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

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 can break APT-related Python tooling, software-properties-common, cloud-init, desktop update utilities, and essential system workflows. 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.

Before removing Deadsnakes packages, confirm the host is Ubuntu 22.04 LTS:

. /etc/os-release
printf '%s\n' "$VERSION_ID"
22.04

For Ubuntu 22.04 LTS users who installed Python 3.12 via Deadsnakes PPA and no longer need it, remove the versioned packages while keeping Ubuntu’s Python 3.10 system interpreter intact:

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

If you installed optional Deadsnakes packages from the earlier section, remove those versioned packages too:

sudo apt remove python3.12-full python3.12-dbg python3.12-gdbm python3.12-tk

Confirm the versioned packages are no longer installed:

dpkg-query -W -f='${db:Status-Abbrev} ${binary:Package}\n' \
python3.12 python3.12-venv python3.12-dev python3.12-full \
python3.12-dbg python3.12-gdbm python3.12-tk 2>/dev/null | \
grep '^ii' || echo "Python 3.12 packages removed"
Python 3.12 packages removed

Review optional dependency cleanup before running it, especially on reused desktops where APT may list packages unrelated to Python 3.12:

sudo apt autoremove --dry-run

If the preview lists only packages you no longer need, run the cleanup interactively:

sudo apt autoremove

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:

grep -R "deadsnakes" /etc/apt/sources.list /etc/apt/sources.list.d 2>/dev/null || echo "Deadsnakes PPA entry removed"
apt-cache policy python3.12 | grep deadsnakes || echo "Deadsnakes no longer provides python3.12"
Deadsnakes PPA entry removed
Deadsnakes no longer provides python3.12

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

Verify the source prefix, symlink, linker configuration, and build directory are gone:

for path in /usr/local/python3.12 /usr/local/bin/python3.12 /etc/ld.so.conf.d/python3.12.conf "$HOME/python3.12-build"; do
  display_path="${path/#$HOME/\$HOME}"
  if [ -e "$path" ]; then
    printf 'Still present: %s\n' "$display_path"
  else
    printf 'Removed: %s\n' "$display_path"
  fi
done
Removed: /usr/local/python3.12
Removed: /usr/local/bin/python3.12
Removed: /etc/ld.so.conf.d/python3.12.conf
Removed: $HOME/python3.12-build

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

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

Confirm the custom prefix was removed:

test ! -e /opt/python3.12 && echo "/opt/python3.12 removed"
/opt/python3.12 removed

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

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.

Share this guide

Help another Linux user troubleshoot faster

Share this guide with someone troubleshooting Linux systems or saving it for later.

Follow LinuxCapable

Want more LinuxCapable guides in Google?

Add LinuxCapable as a preferred source so Google can show more of our fresh Linux tutorials in Top Stories and From your sources when relevant.

Add LinuxCapable as a preferred source on Google
Search LinuxCapable

Need another guide?

Search LinuxCapable for package installs, commands, troubleshooting, and follow-up guides related to what you just read.

Found this guide useful?

Support LinuxCapable to keep tutorials free and up to date.

Buy me a coffeeBuy me a coffee

3 thoughts on “How to Install Python 3.12 on Ubuntu 26.04, 24.04 and 22.04”

  1. It’s for reasons like this that Linux struggles to find itself a bigger share of the market. Nobody should have to dive down a rabbit hole just to fix a dependency on a minor release of something so critical.

    Reply
    • 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 in published comments:

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

Got a Question or Feedback?

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

Verify before posting: