Upgrade Python to latest version (3.9) on Ubuntu Linux

Linux systems come with Python install by default, however, they are usually not the latest. Python also typically is not updated by a typical apt-get upgrade command as well.

To check the version of Python installed on your system run

python3 --version

Typically python keyword is used for Python 2.x versions which have been deprecated

Updating Python to the latest version

Typically Ubuntu's default repositories do not contain the latest version of Python, but an open source repository named deadsnakes does. Let's use that by running

Step 1: Check if Python3.9 is available for install

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

Check if Python 3.9 is available by running

apt list | grep python3.9

This will produce the below result, if you see python3.9 it means you can install it

apt list check if python is present

Step 2: Install Python 3.9

Now you can install Python 3.9 by running

sudo apt-get install python3.9

Now though Python 3.9 is installed, if you check the version of your python by running python3 --version you will still see an older version. This is because you have two version of Python installed and you need to choose Python 3.9 as the default.

Step 3: Set Python 3.9 as default

To do this you need to add both versions to an alternatives by running the below

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2

Now run

sudo update-alternatives --config python3

Choose the selection corresponding to Python3.9 (if not selected by default). Python alternatives on linux

Now run python3 --version again and you should see the latest Python as the output

Fix pip and disutils

Installing the new version of Python will break pip as the disutils for Python3.9 is missing. You will see an error similar to the below

ImportError: cannot import name 'sysconfig' from 'distutils' (/usr/lib/python3.9/distutils/__init__.py)

Or you might also see an error stating No module named 'distutils.util'.

To fix this, we need to first remove the previous version of Python by running

sudo apt remove python3.8
sudo apt autoremove

Next to install disutils, run the below

sudo apt install python3.9-distutils

Reinstall the latest pip by running

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3.9 get-pip.py

Fix pip-env errors

Run the following to fix errors similar to Error: Command '['/path/to/env/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1

sudo apt install python3.9-venv

Fix Python3-apt

The update would also break python3-apt, that will generate an error like

Traceback (most recent call last):   
    File "/usr/lib/command-not-found", line 28, in <module>     
        from CommandNotFound import CommandNotFound   
    File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in <module>     
        from CommandNotFound.db.db import SqliteDatabase   
    File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module>     
        import apt_pkg ModuleNotFoundError: No module named 'apt_pkg'

To fix this first remove the current version of python3-apt by running

sudo apt remove --purge python3-apt

Then do some cleanup

sudo apt autoremove

It is complicated, but this is how you update Python to latest version

Need Help? Open a discussion thread on GitHub.