How to Upgrade Python on Linux
Python is a popular programming language. Like many other programming languages, there can be several different versions organized by release date. Certain applications may require a specific version of Python.
This article we uses Ubuntu and its APT package manager to upgrade Python. If you are using a different Linux distribution, replace the apt Linux command with the appropriate command featured for your package manager. We will guide you to upgrade to Python3.9 in this article
1. Start by update the repositories:
2. Next, install Python 3.9 by running:
apt install python3.9
When prompted, type Y to start the installation.
3. Once Python installs, invoke the 3.9 version by running:
4. However, checking the installation with the
python3 --version command still returns the old version. To fix this, you need to create a list of update alternatives. First, add the old version to the list with the command:
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.[old-version] 1
5. Now add the new version:
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
6. Next, type the following command to configure the priority status of the versions:
update-alternatives --config python3
The output displays the available choices and their assigned number (in the example below, the numbers are 0, 1, 2). Type the number of the the version you wish to use and press Enter.
7. If you are not planning to use the old version of Python, remove the symlink that contained the previous Python 3 version with:
8. Then, replace the symlink with the new version:
ln -s python3.9 /usr/bin/python3
9. Now, check the default version:
The output should confirm the successful installation and setup of the latest available version.
Since Python 3 was not a backward-compatible release, for a long time Python 2 remained the version of choice for those who wanted a stable development environment.
However, given that the official support for the final Python 2.7 release has ended, upgrading to Python 3 is now strongly recommended. Python 3 is faster, and its syntax is more user-friendly.
If you already work with Python 3, upgrading to the latest point release gives you all the security updates and bug fixes.
After reading this tutorial, you should know how to upgrade your Python 3 version on Linux.