Upgrade Python to latest version (3.10) on Ubuntu Linux
Last Updated: 2020-11-13
Linux systems come with Python install by default, but, they are usually not the latest. Python also cannot be updated by a typical
apt upgrade command as well.
To check the version of Python installed on your system run
pythonkeyword is used for Python 2.x versions which has been deprecated
Updating Python to the latest version
Ubuntu's default repositories do not contain the latest version of Python, but an open source repository named
Step 1: Check if Python3.10 is available for install
sudo add-apt-repository ppa:deadsnakes/ppa sudo apt update
Check if Python 3.10 is available by running
apt list | grep python3.10
This will produce the below result, if you see python3.10 it means you can install it
Step 2: Install Python 3.10
Now you can install Python 3.10 by running
sudo apt install python3.10
Now though Python 3.10 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.10 as the default.
Step 3: Set Python 3.10 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.8 1 sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2
sudo update-alternatives --config python3
Choose the selection corresponding to Python3.10 (if not selected by default).
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.10 is missing. You will see an error like the below
ImportError: cannot import name 'sysconfig' from 'distutils' (/usr/lib/python3.10/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.10-distutils
Reinstall the latest pip by running
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python3.10 get-pip.py
Fix pip-env errors
Run the following to fix errors like
Error: Command '['/path/to/env/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1
sudo apt install python3.10-venv
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