Python 3.12 – How to Install on Ubuntu 22.04 or 20.04

Python 3.12 is still in the development phase, which started on the 24th of May, 2022. For developers and enthusiasts that wish to install the latest version of Python 3.12 to get a head start, this tutorial will teach you how to import the Python PPA and install Python 3.12 on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa. The tutorial will also cover swapping alternative default versions, so you can easily switch between Python versions.

Update Ubuntu

Before proceeding with the tutorial, it is highly advised to run an update in your terminal to ensure all packages are up-to-date to avoid any conflicts during the installation. This is important when installing packages such as new kernels and graphic card drivers, especially.

sudo apt update

Optionally, you can list the updates for users who require review or are curious.

sudo apt --list upgradable

Proceed to upgrade any outdated packages using the following command.

sudo apt upgrade

Method 1: LaunchPAD PPA (Recommended)

The first and easiest solution for Ubuntu users would be to import the “deadsnakes” team Launchpad PPA. This will always contain the latest updates for Python and all extra packages that may be required.

Importing Python 3.12 Repository

First, install the following packages that are required. These are most likely installed but run the command to be safe.

sudo apt install dirmngr ca-certificates software-properties-common apt-transport-https -y

For users who have not previously imported a GPG key from the Ubuntu keyserver, the command line terminal will often have issues importing GPG keys from LaunchPAD PPAs because the directories are not created. This is an easy fix. Use the following command that will, in turn, generate the directories.

sudo gpg --list-keys

Example output:

gpg: directory '/root/.gnupg' created
gpg: keybox '/root/.gnupg/pubring.kbx' created
gpg: /root/.gnupg/trustdb.gpg: trustdb created

This can be skipped, but if you encounter an issue, just run the command and re-try.

The next task is to import the GPG key needed.

sudo gpg --no-default-keyring --keyring /usr/share/keyrings/deadsnakes.gpg --keyserver --recv-keys F23C5A6CF475977595C89F51BA6932366A755776

Example output:

gpg: key BA6932366A755776: public key "Launchpad PPA for deadsnakes" imported
gpg: Total number processed: 1
gpg:               imported: 1

With the GPG key now imported, you can import the LaunchPAD PPA.

echo "deb [signed-by=/usr/share/keyrings/deadsnakes.gpg] $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/pythonppa-deadsnakes.list

Before you continue, run an APT update to reflect the newly imported PPA.

sudo apt update

Run installation command for Python 3.12

With the 3.12 PPA now imported, you can install Python by executing the following command.

sudo apt install python3.12 -y

Verify the installation and build version using the following command.

python3.12 --version

Example output:

Python 3.12.0a3

Optionally, you can install the following extras. Remember, this command has most of the modules, you can remove whatever you do not want.

sudo apt install python3.12-{tk,dev,dbg,venv,gdbm,distutils}

If you are after a module not listed above, try searching for it in your terminal with the apt search command that will list all the packages for Python 3.12.

apt search python3.12-*

Alternatively, to install all extras, run the full installation command.

sudo apt install python3.12-full

Lastly, if you need various versions of Python installed with Python 3.12 from the PPA, you can install them all or certain ones using the following command.

sudo apt install python{2.7,3.7,3.8,3.9,3.10,3.11,3.12}

Remember to remove which version numbers you do not need, ideally you should not be installing all these versions as some are EOL. Now you can learn about setting default python versions.

Switch Default Python Versions

You may have a particular one you want as the default for users needing multiple versions of Python on their system. The following steps will show you how to change python versions when you have numerous installed.

See also
How to Install GIMP on Rocky Linux EL9 or EL8

First, you must add the symbolic links for every Python version separately. Next to the symlink, you will add the group name python and the option number.

Example (you can customize this or copy):

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 3
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 4
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 5
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.11 6
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.12 7

Remember, it does not require you to have all versions listed up; you can copy the entire command, and the versions you have installed will have symlinks created.

Next, list the python versions with the following command.

sudo update-alternatives --config python

Example output:

As mentioned above, the tutorial machine has Python versions 3.12, 3.11, 3.10, 3.9, 3.8, and 3.7 installed, with 3.12 currently being the default selected version. This can be seen with the version with an Asterix * next to the selection number.

If you want to make, for example, Python 3.11 the default version, you will enter the selection number 3 in this example, and yours will vary, given the number of versions installed, as the selection numbers may change.

If successful, you will get the following output.

update-alternatives: using /usr/bin/python3.11 to provide /usr/bin/python (python) in manual mode

If you relist the alternative options, Python 3.11 is the default version with the Asterix * default sign.


Install Python 3.12 PIP

By default, installing python 3.12 using the following APT command should work for all those using the Python PPA repository.

sudo apt install python3-pip

Once installed, it is a good idea to check for upgrades.

python -m pip install --upgrade pip

Example output:

joshua@ubuntu-linux:~$ python -m pip install --upgrade pip
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pip in ./.local/lib/python3.12/site-packages (22.3.1)

Now verify the PIP 3.8 version installed with the following command.

pip --version

Example output:

pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)


In the tutorial, you have learned how to import the Python PPA and install the latest development version of Python 3.12. This PPA will always be up-to-date and when Python 3.12 is finally released you will be automatically upgraded too it.

Leave a Comment

Share to...