Install Python 3.13, 3.12, or 3.11 on Ubuntu 22.04 or 20.4 Linux

Let’s see how to use a PPA repository on Ubuntu 24.04, 22.04, or 20.04 to install Python version 3.13, 3.12, or 3.11 using the command terminal.

The latest version of the Python programming language while doing this article was 13.12, however, 13.13 was available as a pre-release. The new versions always have several new features, improvements, and optimizations. Therefore, it is important to update the existing version of Python on your Ubuntu Linux to include new features, security improvements, and bug fixes. Python versions are typically numbered in a Major.Minor.Patch.

Let’s see the steps to install Python 3.13 or 3.12 on Ubuntu 22.04/20.04 with the help of this guide:

1. Start with the Ubuntu Update

We all know the best way to install packages on Linux is by using its default package manager, therefore, before moving further, why not make sure our system is up to date…

sudo apt update && sudo apt upgrade -y

2. Check the Current Python Version

It is also important to know what is the current version available on our Ubuntu system so that after installing the new one we will have a piece of knowledge about the existing Python version on our system.

While doing this tutorial, on our Ubuntu 22.04 system we have Python 3.10.12 by default.

python -V
check current python version

3. Add DeadSnake Python PPA

Well, we cannot have the extremely latest versions of Python by using the default system repository of Ubuntu Linux. Therefore, either we manually need to download the version we want from the official website and then configure it (manually) or use some third-party repository. Perhaps, the third-party repo method is easier and quicker for most of the users to get the newer Python versions. Therefore, here we are adding the popular DeadSnake PPA to get not only the newer but even the older versions of Python such as 3.9, 3.8, and 3.7.

sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa

Supported Ubuntu and Python Versions by Deadsnake Repository:

  • Ubuntu 20.04 (focal) supports Python3.5 – Python3.7, Python3.9 – Python3.13
  • Ubuntu 22.04 (jammy) supports Python3.7 – Python3.9, Python3.11 – Python3.13
  • Note: Python2.7 (all), Python 3.8 (focal), and Python 3.10 (jammy) are not provided by deadsnakes as upstream Ubuntu provides those packages.

4. Installing Python 3.13, 3.12 or 3.11 on Ubuntu

Now, after adding the PPA repository, it is possible to install the all available latest versions of Python on Ubuntu. To check whether the version we want is available through the added repo or not, we can use the given command syntax.

Let’s say we want to check the availability of Python 13.12

sudo apt-cache policy python3.12
Check Python versions available to install

Similarly, you can check for other versions as well… Now to install them, the syntax will be:

sudo apt install python(version)

For example: Commands to install the latest versions of Python on Ubuntu:

To install Python 3.13

sudo apt install python3.13

Python 3.12 installation

sudo apt install python 3.12

Similarly, Python 3.11 installation on Ubuntu:

sudo apt install python3.11

5. How to install Python Modules

Well, if you want to install some particular Python version module, let’s say we want to have a Python3.12 venv module then the command will be like this:

sudo apt install python3.12-venv

Similarly, we can install other versions of modules as well.

For the common module examples, change #.# with a version of the Python you want to install.

  • python#.#-dev: includes development headers for building C extensions
  • python#.#-venv: provides the standard library venv module
  • python#.#-distutils: provides the standard library distutils module
  • python#.#-lib2to3: provides the 2to3-#.# utility as well as the standard library lib2to3 module
  • python#.#-gdbm: provides the standard library dbm.gnu module
  • python#.#-tk: provides the standard library tkinter module

6. How to Set the default Python version

In many cases, especially if you are a developer then you might want to have multiple versions of Python on Ubuntu to fulfill the requirements of various projects. However, in that scenario, how will you set the required version of Python as the system’s default one? For that, here is the command – we can use Update-Alternatives…

List all Python versions are available on your systems:

ls /usr/bin/python*

We have four versions, you may have less or more depending on how many you have installed so far…

List all versions of Python

To know whether any version is configured as Python alternatives or not, run:

sudo update-alternatives --list python

If the output is:

“update-alternatives: error: no alternatives for python”

It means there are no alternatives that have been configured for Python, so let’s do some.

Here we are setting all the available versions as alternatives, installed recently; later we will be able to choose between them and set one of them as the system’s default one. Furthermore, the values 1, 2, 3, and 4 given at the end of each command respectively (below), are used to set the priority of the versions. But, if you want can interchange them.

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.11 2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.12 3
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.13 4
Set Python alternatives

We have configured the available versions to use as alternatives against each other, now, if we want to change the current one and set some other version as the default one, use the given command:

sudo update-alternatives --config python

Choose the Python version you want to set as the system-wide default on Ubuntu by entering its selection number. For example, in the given screenshot you can see the system automatically used the latest version but we want to set the Python 3.12 as the default one. So, for that, we, simply type its selection number which is – 2, and then hit the Enter key.

Set python version default using Update Alternatives

To check the version, we can again use:

python -V

7. How to install Pip3 (optional)

Well, those who require the Python package manager that is PIP, if not have it already, can use the command given to install it.

sudo apt install python3-pip

8. Uninstall Python and PPA (optional)

Sometimes, we don’t require the older version of Python on our Ubuntu system, if that is the case with you as well, then we can use the given command to remove any installed Python version:

Let’s say we want to remove Python3.12, then the command will be like this:

sudo apt remove --purge python3.12

If you want the latest updates for the Python versions which are not available through Ubuntu’s default system repos, then don’t remove the PPA. However, if you want to go for the default version which comes along your Ubuntu, then you would like to remove the Deadsnake PPA as well.

To remove PPA:

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

Other Articles:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.