How to Install Python 3.12 on Ubuntu 22.04

Python 3.12 is the newest major release of the Python programming language, and it contains many new features and optimizations.

Manually build Python 3.12 from the source code

Update System

First, ensure your system is up-to-date by running,

apt update -y

apt upgrade -y

Install Required Dependencies

Install necessary packages to build Python from source,

apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev \
    libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
    xz-utils tk-dev libffi-dev liblzma-dev python3-openssl git

Download Python Source Code

Visit the Python downloads page to get the source code. Then, using wget, download the source,

wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz

Now, Extract the Archive,

tar -xf Python-3.12.0.tgz

Configure and Build,

cd Python-3.12.0
./configure --enable-optimizations

Build Python,

make -j 8

Replace 8 with the number of CPU cores you want to allocate for the build process.

Install Python

make altinstall

Using altinstall instead of install prevents it from replacing the system's default Python interpreter (which could cause system tools to malfunction).

Verify Installation

Check if Python 3.12 has been installed successfully,

python3.12 --version

Output:

root@vps:~/Python-3.12.0# python3.12 --version
Python 3.12.0
root@vps:~/Python-3.12.0#

This should output the Python version you just installed.

Install Python 3.12 from the deadsnakes PPA

Install the prerequisite for adding custom PPAs,

apt install software-properties-common -y

Add deadsnakes PPA,

add-apt-repository ppa:deadsnakes/ppa

Update the package lists to include packages from the newly added PPA,

apt update -y

Install Python 3.12

Now, you can install Python 3.12 using the apt package manager.

apt install python3.12

Verify Installation,

python3.12 --version

Output:

root@vps:~# python3.12 --version
Python 3.12.0
root@vps:~# 

Once installed Python 3.12 using the APT package manager, the PIP will not be installed by default. Install PIP, run the following command,

curl -sS https://bootstrap.pypa.io/get-pip.py | python3.12

Now check the PIP version using the below command,

pip3.12 -V

Output:

root@vps:~# pip3.12 -V
pip 24.0 from /usr/local/lib/python3.12/dist-packages/pip (python 3.12)
root@vps:~#

And that's it! You may now use Python 3.12 to develop software, build web apps, establish workflows, and more.