Install Python 3.13, 3.12, or 3.11 on Ubuntu 24.04, 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.

While doing this article, the latest version of the Python programming language 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 24.04, 22.04, or 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 proceeding, 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 the current version of Python is available on our Ubuntu system so that after installing the new one, we will have a better understanding of the existing version on our system.

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

python -V
check current python version

3. Add DeadSnake Python PPA

We cannot have the latest versions of Python by using Ubuntu Linux’s default system repository. Therefore, we need to manually download the version we want from the official website and then configure it (manually) or use a third-party repository. Perhaps the third-party repo method is easier and quicker for most users when getting the newer Python versions. Therefore, we are adding the popular DeadSnake PPA to get the newer and 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

    4. Installing Python 3.13, 3.12 or 3.11 on Ubuntu

    Now, after adding the PPA repository, it is possible to install the latest versions of Python on Ubuntu. To check whether the version we want is available through the added repo, 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, 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, but you may have fewer 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”

    This means no alternatives have been configured for Python, so let’s do something.

    Here, we are setting all the available versions installed recently as alternatives; 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 (below) are used to set the priority of the versions. But you can interchange them if you want.

    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

    Enter the Python version you want to set as the system-wide default on Ubuntu. For example, in the given screenshot, you can see the system automatically used the latest version, but we want to set Python 3.12 as the default one. So, for that, we type its selection number, which is2, 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, PIP, can use the command given to install it if they don’t already have 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 Python versions that are not available through Ubuntu’s default system repos, don’t remove the PPA. However, if you want to use the default version that comes with Ubuntu, 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.