How to upgrade to Python 3.12 on Ubuntu 20.04 and 22.04 LTS

In this article, we will upgrade to Python 3.12 and configure it as the default version of Python in Ubuntu 20.04 and 22.04 LTS.

The current stable version of Python was released on 02 October 2023. Many Ubuntu users are facing problems during upgrading Python to the latest version. Python 3.10 is available as default when we install Ubuntu 22.04 LTS and Python 3.9 is available in Ubuntu 20.04. Python 3.12.0 is not available in the default Ubuntu 20.04 and 22.04 repositories.

So let’s start with checking the currently installed version of Python on your system.

python3 -V

As seen in the image above, my currently installed Python version is 3.10 but yours may differ.

Install Python 3.12

Follow the simple steps to install and configure Python 3.12

Step 1: Add the repository and update

The latest Python 3.12 is not available in Ubuntu’s default repositories. So, we have to add an additional repository. On launchpad repository named deadsnakes is available for Python Packages.

Add the deadsnakes repository using the below commands.

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update

Update the package list using the below command.

apt-get update

Verify the updated Python packages list using this command.

apt list | grep python3.12

As seen in the image above, Now we have Python 3.12 available for installation.

Step 2: Install the Python 3.12 package using apt-get

install Python 3.12 by using the below command :

sudo apt-get install python3.12

Step 3: Add Python 3.11 & Python 3.12 to update-alternatives

Add both old and new versions of Python to Update Alternatives.

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 2

Step 4: Update Python 3 for point to Python 3.12

By default, Python 3 is pointed to Python 3.11. That means when we run python3 it will execute as python 3.11 but we want to execute this as python 3.12.

Type this command to configure python3:

sudo update-alternatives --config python3

You should get the above output. Now type 2 and hit enter for Python 3.12 Remember the selected number may differ so choose the selection number which is for Python 3.12.

Step 5: Test the version of python

Finally, test the current version of Python by typing this :

python3 -V

You should get Python 3.12 as an output.

In this article, we learn how to upgrade Python to the latest version which is 3.12 in Ubuntu 20.04 and 22.04 LTS.

Share your thoughts in the comment section. Happy Learning …!!

4 thoughts on “How to upgrade to Python 3.12 on Ubuntu 20.04 and 22.04 LTS”

  1. after upgrading to python 3.12 using the procedures described here, I get the following error when executing ‘apt update’:
    E: Could not open lock file /var/lib/apt/lists/lock – open (13: Permission denied)
    E: Unable to lock directory /var/lib/apt/lists/
    W: Problem unlinking the file /var/cache/apt/pkgcache.bin – RemoveCaches (13: Permission denied)
    W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin – RemoveCaches (13: Permission denied)

    Reply

Leave a Comment