Python 3.12 is the current default interpreter on Ubuntu 24.04 LTS and provides modern language features, improved performance, and enhanced f-string support. With security support guaranteed through October 2028, Python 3.12 is a stable, production-ready runtime for new projects and long-term LTS deployments.
This guide covers installing Python 3.12 on Ubuntu from official repositories (Ubuntu 24.04 LTS), the Deadsnakes PPA for older releases (Ubuntu 22.04 LTS), and compiling from source for custom optimization. You’ll install development headers, configure pip within externally-managed environment constraints, create and manage virtual environments, switch between interpreters, and troubleshoot common issues.
When to Choose Python 3.12 Over Newer Versions
Compare Python Releases for Ubuntu
Ubuntu 24.04 LTS ships Python 3.12 by default, making it the natural choice for new projects on the latest LTS release. Use the following comparison to determine when Python 3.12 provides the right balance of stability, support, and feature coverage across different deployment scenarios.
| Python Version | Availability on Ubuntu | Choose It When | Trade-offs |
|---|---|---|---|
| Python 3.8 | Deadsnakes PPA or source build only | Legacy applications pinned to EOL runtimes, transitional migrations, compatibility testing | End-of-life October 2024, no upstream security patches, container isolation recommended |
| Python 3.10 | Ubuntu 22.04 LTS default, Deadsnakes PPA for 24.04 LTS+ | Production fleets requiring LTS security coverage through October 2026, tools certified against 3.10 binaries, Canonical Extended Security Maintenance support | Fewer new features, Ubuntu 24.04+ users must import PPA, some newer libraries target 3.11+ |
| Python 3.11 | Deadsnakes PPA for all releases | CPU-bound workloads seeking 10-60% performance improvements, exception group handling, TOML parsing, fine-grained error locations | Not default on any Ubuntu LTS, community-maintained PPA, some vendor SDKs still validating against 3.10 |
| Python 3.12 | Ubuntu 24.04 LTS default, Deadsnakes PPA for 22.04 LTS | New projects, development workstations, f-string upgrades (PEP 701), per-interpreter GIL experimental feature, continued security support through October 2028 | Ubuntu 22.04 LTS users must import PPA, some older third-party modules may need updates, newer features land first in 3.13 |
| Python 3.13 | Deadsnakes PPA for Ubuntu 24.04 LTS and 22.04 LTS; source builds for custom prefixes | Testing the free-threaded build preview, adopting the latest language and typing changes, planning ahead for support through October 2029 | Not in Ubuntu repositories, community packages lack guaranteed security SLAs, some third-party wheels may lag early releases |
| Python 3.14 | Deadsnakes PPA for Ubuntu 24.04 LTS and 22.04 LTS as published; source builds available immediately | Early adopters validating continued free-threaded work, benchmarking the newest runtime, and planning for support through October 2030 | Not in Ubuntu repositories, PPA refresh cadence may lag security fixes, third-party wheels can trail initial releases |
Ubuntu 24.04 LTS users should install from official repositories for best compatibility and automatic security updates. Ubuntu 22.04 LTS users can import the Deadsnakes PPA to run Python 3.12 alongside the system’s default Python 3.10. Choose Python 3.10 for conservative production deployments that prioritize extended security support; choose Python 3.11 for CPU-bound workloads; choose Python 3.12 for new projects leveraging modern language features with security support through October 2028. Choose Python 3.13 or Python 3.14 when you want the latest interpreter features, free-threaded preview builds, and longer upstream support via the Deadsnakes PPA or a source build.
Key Reasons to Choose Python 3.12
Python 3.12 remains the best choice for:
- Ubuntu 24.04 LTS default interpreter: Ships with the latest LTS release, receiving automatic security updates from Canonical for the extended support window through October 2028.
- Modern language features: PEP 701 f-strings, improved error messages with exact column positions, per-interpreter GIL (experimental), per-item type hint configuration.
- Performance improvements: Comprehension inlining, optimized bytecode execution, and reduced memory overhead compared to Python 3.10 and 3.11.
- Security support through October 2028: Five additional years of upstream security patches guarantee production-grade stability without forcing major version upgrades.
- Vendor SDK certification: Major frameworks (Django, FastAPI, NumPy) fully support Python 3.12 with optimized wheels and active development.
Choose Your Python 3.12 Installation Method
Select the installation method that matches your Ubuntu release and deployment requirements. Ubuntu 24.04 LTS users should install from official repositories; Ubuntu 22.04 LTS users must import the Deadsnakes PPA; all users can compile from source for custom optimization.
| Installation Option | Best For | Trade-offs |
|---|---|---|
| Ubuntu 24.04 LTS Official Repositories | Ubuntu 24.04 LTS systems benefiting from automatic security updates managed by Canonical, best long-term stability | Not available on Ubuntu 22.04 LTS or 20.04 LTS; use Deadsnakes PPA for older releases |
| Deadsnakes PPA | Ubuntu 22.04 LTS systems needing Python 3.12 alongside the default Python 3.10, development machines testing against 3.12 | Community-maintained packages, no guaranteed security update timeline; production users must monitor CVEs independently |
| Compile from Source | Custom optimization requirements, air-gapped environments, installations needing specific build flags like --enable-optimizations or alternate prefixes such as /opt/python3.12 | Takes longer to build, you manage security updates manually, requires build dependencies and periodic recompilation |
| Stick with Ubuntu Default Python | Users who do not specifically need Python 3.12; Ubuntu 24.04 LTS ships it by default, no additional setup needed | If you need features only available in Python 3.13+, follow the upstream release schedule |
Whichever method you choose, never replace the default /usr/bin/python3 interpreter. Ubuntu’s package manager, cloud-init, and system utilities depend on the version that ships with the distribution.
Understand Support and Compatibility Limits
- Ubuntu 24.04 LTS ships Python 3.12 natively: The official repositories include python3.12, python3.12-dev, and all optional packages. No PPA import required. Ubuntu 22.04 LTS users must import the Deadsnakes PPA; Ubuntu 20.04 LTS reaches end-of-standard-support April 2025, making Python 3.12 deployment increasingly difficult on that release.
- Deadsnakes PPA carries a “no guaranteed updates” disclaimer: The maintainer states explicitly that production teams using the Deadsnakes PPA must monitor CVEs themselves and be ready to recompile or sandbox workloads until patched packages land.
- Ecosystem support is strong: Django 5.0+, FastAPI, SQLAlchemy 2.0, NumPy 2.0, and modern frameworks all fully support Python 3.12 with optimized wheels. Legacy libraries targeting only Python 3.10 are increasingly rare.
Python 3.12 reaches upstream end-of-life on 31 October 2028, per the official release schedule (PEP 693). For systems requiring guaranteed security support beyond that date, plan migrations to Python 3.13 (EOL October 2029) or Python 3.14 (EOL October 2030). Ubuntu 24.04 LTS receives Canonical’s Extended Security Maintenance through April 2034, extending system-level support even after Python 3.12 upstream EOL.
Python 3.12 Pre-Installation Steps
Update System Before Proceeding
Before installing Python 3.12, update your system’s package lists to ensure all dependencies resolve correctly and you receive the latest security patches. This step is particularly important when adding third-party repositories like Deadsnakes or compiling software from source, as build tools and libraries frequently receive compatibility updates.
Open a terminal window with Ctrl+Alt+T and run:
sudo apt update
Optionally, upgrade installed packages to prevent conflicts during Python 3.12 installation:
sudo apt upgrade
Install Python 3.12 on Ubuntu
The exact installation method depends on whether your system ships Python 3.12 through official repositories (Ubuntu 24.04 LTS) or requires the Deadsnakes PPA (Ubuntu 22.04 LTS). Start by refreshing the package index.
Install Python 3.12 from Ubuntu Repositories
Ubuntu 24.04 LTS users can install Python 3.12 directly from official repositories without importing a PPA:
sudo apt install python3.12 python3.12-venv python3.12-dev
Verify the installation:
python3.12 --version
Python 3.12.x
Run a smoke test to ensure core modules loaded correctly:
python3.12 -c "import ssl, sqlite3, bz2; print('Python 3.12 ready on Ubuntu')"
Install Python 3.12 via Deadsnakes PPA
Ubuntu 22.04 LTS users must import the Deadsnakes PPA to access Python 3.12 packages. Ubuntu 24.04 LTS users should stick with the official repositories for security updates and only use the PPA for short-term testing or if you deliberately prefer community packages:
sudo add-apt-repository ppa:deadsnakes/ppa -y
sudo apt update
sudo apt install python3.12 python3.12-venv python3.12-dev
Critical Deadsnakes PPA Disclaimer: The Deadsnakes PPA explicitly disclaims timely security updates. The maintainer states: “there’s no guarantee of timely updates in case of security problems or other issues.” Production systems that demand guaranteed security patching should prefer Ubuntu 24.04 LTS official repositories (with Canonical Extended Security Maintenance through April 2034) or compile from source with your own security monitoring workflow. Use Deadsnakes only for development machines, testing environments, or non-critical workloads where you accept responsibility for tracking CVEs independently.
Verify the installation:
python3.12 --version
Python 3.12.x
Install Additional Python 3.12 Packages (Optional)
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:
python3.12-dbg provides debugging symbols for profiling, crash analysis, or gdb attachments:
sudo apt install python3.12-dbg
python3.12-gdbm provides GNU dbm bindings for lightweight key-value storage:
sudo apt install python3.12-gdbm
python3.12-tk installs Tkinter for GUI applications:
sudo apt install python3.12-tk
Python 3.12 removed the legacy distutils module per PEP 632. The
python3.12-distutilspackage 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:
sudo apt install python3.12-dbg python3.12-gdbm python3.12-tk
There is no
python3.12-fullmeta-package in Ubuntu 24.04 or the Deadsnakes PPA. To install every recommended component for the system default interpreter, usepython3-fullinstead—on Ubuntu 24.04 that targets Python 3.12. PPA users should install only the specific 3.12 packages they need as shown above.
Compile Python 3.12 from Source on Ubuntu
Compile Python 3.12 manually when you need Link Time Optimization, 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 using wget and extract the archive. The snippet below discovers the newest 3.12 patch automatically:
PY312_VERSION="$(curl -s https://www.python.org/ftp/python/ | grep -oE '3\\.12\\.[0-9]+' | sort -V | tail -1)"
wget https://www.python.org/ftp/python/${PY312_VERSION}/Python-${PY312_VERSION}.tar.xz
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 and filenames.
Install Build Dependencies
Before compiling, install the libraries required for SSL, compression, readline, Tk, and UUID support:
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev \
libssl-dev libsqlite3-dev libreadline-dev libffi-dev libbz2-dev liblzma-dev uuid-dev \
tk-dev pkg-config make -y
These package names exist on both Ubuntu 22.04 LTS and 24.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. The following example installs Python under /usr/local/python3.12 to avoid overwriting your system interpreter. The --enable-optimizations flag enables Profile Guided Optimization (PGO), which typically adds 10-20% performance improvement but increases compile time significantly:
./configure --enable-optimizations --with-lto --with-ensurepip=install --prefix=/usr/local/python3.12
make -j"$(nproc)"
sudo make altinstall
Using make altinstall instead of make install keeps /usr/bin/python3 untouched while adding /usr/local/python3.12/bin/python3.12.
Register Libraries and Test the Build
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 -s /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')"
If the second command fails, reinstall the missing -dev packages, rerun ./configure, and rebuild the installation.
Install Pip for Python 3.12
Pip usually installs with the interpreter, but verifying its presence ensures you can manage packages without touching Ubuntu’s system directories.
Install Pip via APT
Install the system-wide pip package so Ubuntu manages updates automatically:
sudo apt install python3-pip
While this command wires pip to /usr/bin/python3, you can still bootstrap pip specifically for Python 3.12:
python3.12 -m ensurepip --upgrade
python3.12 -m pip --version
The ensurepip module installs pip in the Python 3.12 site-packages directory even when the system default interpreter differs.
Bootstrap Pip 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 the Python pip installation guide. Using python3.12 -m pip keeps dependency management isolated from the system interpreter.
Ubuntu 23.04 and newer mark the system Python as “externally managed” (PEP 668). If you try to run
python3.12 -m pip install <package>against the base interpreter on Ubuntu 24.04 or newer, pip prints an error unless you work inside a virtual environment or pass--break-system-packages. Use –break-system-packages only when you fully understand the risks: this flag allows pip to write directly to system directories, bypassing apt’s conflict detection. This can break system tools, interfere with apt upgrades, and create difficult-to-diagnose dependency conflicts. Only use--break-system-packagesin isolated development VMs or throwaway containers, never on production servers or shared systems. The recommended approach is to create a venv per project as shown in the virtual environments section below.
Work in Python 3.12 Virtual Environments
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.
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 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 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.
Switch Between Python Versions on Ubuntu
Additionally, you can use the update-alternatives framework when you need to change which interpreter launches when you type python. This approach keeps /usr/bin/python3 untouched, preserving system scripts that depend on Ubuntu’s default version.
Register Python Versions
The priority numbers determine automatic selection when in auto mode; higher numbers take precedence. Only register versions that already exist on your system.
# Ubuntu 24.04 LTS (system Python 3.12)
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.12 6
On Ubuntu 22.04 LTS with the Deadsnakes PPA installed, register both the system interpreter (3.10) and Python 3.12 so you can switch between them:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 4
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.12 6
Stock Ubuntu installations only ship their default interpreter: Python 3.10 on Ubuntu 22.04 LTS and Python 3.12 on Ubuntu 24.04 LTS. Register additional entries only after you have installed those binaries via Deadsnakes or source compilation. Attempting to register non-existent paths will fail.
Configure the Default Version
After registering each interpreter, select the default:
sudo update-alternatives --config python
Only adjust
/usr/bin/python(the unversioned python command, originally a Python 2 shim for legacy scripts). Leave/usr/bin/python3at the system default to avoid breaking apt, cloud-init, or other distribution utilities that depend on Ubuntu’s shipped interpreter.
Troubleshooting Common Python 3.12 Installation Issues
Missing Development Headers During Compilation
The configure script may report warnings about missing optional libraries when development headers are not installed. While Python will compile successfully, certain standard library modules like _ssl, _bz2, or _sqlite3 will be unavailable. To identify which modules failed to build, review the configure output carefully:
./configure --enable-optimizations --with-ensurepip=install --prefix=/usr/local/python3.12 2>&1 | grep "disabled"
This command displays all disabled modules. Furthermore, after running make, check for “Failed to build these modules” messages near the end of the compilation output. Install the corresponding -dev packages, then run ./configure and make again to rebuild with full module support.
Shared Library Linking Errors
When running python3.12 after source compilation, you may encounter an error stating error while loading shared libraries: libpython3.12.so.1.0: cannot open shared object file. This happens when the dynamic linker cannot locate the Python shared library because it was installed outside standard library paths.
To resolve this, first verify the library exists:
find /usr/local -name "libpython3.12.so*"
Then update the linker cache with the correct path. If you installed to the default prefix, use:
sudo ldconfig /usr/local/lib
Alternatively, add the library path to your environment permanently by creating a configuration file:
echo "/usr/local/python3.12/lib" | sudo tee /etc/ld.so.conf.d/python3.12.conf
sudo ldconfig
PIP Externally-Managed Environment Errors
On Ubuntu 23.04 and newer, running python3.12 -m pip install package-name may fail with an “externally-managed-environment” error per PEP 668. Ubuntu marks the system Python as externally managed to prevent conflicts between apt-installed packages and pip-installed packages.
The recommended solution is creating a virtual environment for each project:
python3.12 -m venv myproject_env
source myproject_env/bin/activate
pip install package-name
Alternatively, use the --break-system-packages flag to bypass the restriction, though this approach risks dependency conflicts with apt packages and is not recommended for production systems. Use this only when you fully understand the risks: this flag allows pip to write directly to system directories, bypassing apt’s conflict detection. This can break system tools, interfere with apt upgrades, and create difficult-to-diagnose dependency conflicts. Only use --break-system-packages in isolated development VMs or throwaway containers, never on production servers or shared systems:
python3.12 -m pip install --break-system-packages package-name
APT Breaking After Installing Python 3.12 PPA
In some cases, if apt update or apt install fails with Python-related errors after adding the Deadsnakes PPA, ensure you did not accidentally replace Ubuntu’s system Python 3 interpreter. Check the /usr/bin/python3 symlink:
ls -la /usr/bin/python3
This should point to the Ubuntu default version (python3.12 on 24.04, python3.10 on 22.04), not an alternate version. If it points incorrectly, restore the original symlink 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
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.
Virtual Environment Creation Fails
When python3.12 -m venv fails with “ensurepip is not available” or similar errors, the python3.12-venv package is missing. Install it explicitly:
sudo apt install python3.12-venv
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.
Update or Remove Python 3.12
Update Python 3.12 Packages
sudo apt update
sudo apt upgrade
Any pending python3.12 updates from the Ubuntu repositories or Deadsnakes PPA appear in the upgrade list alongside other packages.
Remove Python 3.12 Packages
CRITICAL WARNING – Ubuntu 24.04 Users: 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 with custom prefixes like
/usr/local/python3.12or/opt/python3.12.
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 -a
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
Clean Up Source-Compiled Installations
For source-compiled installations, delete the installation directory and ld.so entry. When you compiled with the default prefix (/usr/local), remove these files:
sudo rm -rf /usr/local/python3.12
sudo rm /etc/ld.so.conf.d/python3.12.conf
sudo ldconfig
If you used a custom prefix like --prefix=/opt/python3.12, simply remove that directory:
sudo rm -rf /opt/python3.12
sudo ldconfig
Remove update-alternatives Entries
Finally, if you configured update-alternatives for Python version switching, remove the python3.12 entry:
sudo update-alternatives --remove python /usr/bin/python3.12
Verify the removal completed successfully:
sudo update-alternatives --list python
Conclusion
Python 3.12 delivers a stable, production-ready runtime with security support through October 2028 and modern language features that benefit new projects and existing codebases. Ubuntu 24.04 LTS ships Python 3.12 by default with automatic security updates; Ubuntu 22.04 LTS users can import the Deadsnakes PPA or compile from source for custom optimization. The installation process covers repository management, pip configuration within externally-managed environments, virtual environment workflows, and version switching without disrupting Ubuntu’s system interpreter.
Useful Links
Here are some valuable links related to using Python 3.12 on an Ubuntu system:
- Python Official Website: Visit the official Python website for information about the programming language, its features, and download options.
- Python Documentation: Access comprehensive documentation for detailed guides on installing, configuring, and using Python.
- PEP 693: Python 3.12 Release Schedule: Review the official release schedule and security support timeline through October 2028.
- Python Community: Join the Python community to connect with other developers, participate in discussions, and find support.
- What’s New in Python 3.12: Explore new features, deprecations, and improvements in Python 3.12.
- Python FAQ: Find answers to frequently asked questions about using Python.
- Python GitHub Repository: Visit the Python GitHub repository to view the source code, report issues, and contribute to the development.
- PyPI (Python Package Index): Explore the Python Package Index to find and install packages and libraries for Python.
- Deadsnakes PPA: Access the community-maintained repository for Python versions not yet in Ubuntu’s official repositories.
sudo add-apt-repository universe
sudo apt update
apt install python3.12
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:
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.