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

In this article, we will upgrade to Python 3.11 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 04 April 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.11.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.11

Follow the simple steps to install and configure Python 3.11

Step 1: Add the repository and update

The latest Python 3.11 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.11

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

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

install Python 3.11 by using the below command :

sudo apt-get install python3.11

Step 3: Add Python 3.10 & Python 3.11 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.10 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2

Step 4: Update Python 3 for point to Python 3.11

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

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.11. Remember the selected number may differ so choose the selection number which is for Python 3.11.

Step 5: Test the version of python

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

python3 -V

You should get Python 3.11 as an output.

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

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

13 thoughts on “How to upgrade to Python 3.11 on Ubuntu 20.04 and 22.04 LTS”

  1. Hey there., I did the same thing as you’ve mentioned., Also got upgraded to python 3.11. But the problem is, I can’t open my terminal., Both ctrl+alt+T and open by icon click, doesn’t work. Have you got any solutions?
    Thanks in advance

    Reply
      • You can have both 3.10 and 3.11 but in order to start the terminal when you boot up the computer for example you need to be on 3.10.

        Write this:
        sudo update-alternatives –config python3

        Choose the number representing the python3.10 and it will work.

        PS. I have VS Code installed (which works on both versions of Pythons). So if I notice that the terminal and some other apps won’t open I access the terminal from there and update the python.

        Hope it helps!

        Reply
      • TL;DR Follow these steps to remediate your ‘apt_pkg’ error:
        `sudo apt remove –purge python3-apt`
        `sudo apt install python3-apt`
        `sudo apt autoremove`

        DETAILED EXPLANATION:
        Had the same issue while following the instructions in an Ubuntu 22.04.3 distro of Windows Subsystem for Linux (WSL).

        I tried this solution first:
        `sudo apt-get install –reinstall python3-apt`

        However, I still got the same error. Turns out, “The error is primarily because of library apt_pkg.cpython-35m-x86_64-linux-gnu.so not being present in /usr/lib/python3/dist-packages.” So completely uninstalling and reinstalling python3-apt seems to hit a more rigorous check, which it seems like the one-liner reinstall misses.

        Reply

Leave a Comment