How to Change the Default Python Version to Python 3.9

After installing the latest version of Python 3 on your system, you will notice that an older version is still used when executing Python VIA the python and python3 commands.

 

In this article, we will learn how to change the default Python version so that it can be used without explicitly typing a version number.

 

Setting the Latest Python Version

If you have followed my article on how to install Python 3.9 on Ubuntu, you will notice that 3.9 is not the version used when running the following command:

 

python -V
Python 2.7

 

To update Python to the latest version installed on your system, we can use update-alternatives to change the python shortcut command to point to Python 3.9.

 

Before we do this, we need to add Python 3.9 as an option in update-alternatives. Start by listing all the installed versions of Python 3 so you can choose the version options to add.

 

ls /usr/bin/python3*

 

You may also want to list Python 2.* versions if you want to add one of them as an option:

 

ls /usr/bin/python2*

 

Now run the following commands changing the version numbers to suit what you have.

 

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 3
update-alternatives: using /usr/bin/python3.6 to provide /usr/bin/python (python) in auto mode
update-alternatives: using /usr/bin/python3.9 to provide /usr/bin/python (python) in auto mode
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode

 

Note - the number at the end of each command is the priority number.

 

Now you can change the default version of Python using the following command:

 

sudo update-alternatives --config python
There are 3 choices for the alternative python (providing /usr/bin/python).

Selection  Path        Priority  Status
------------------------------------------------------------
* 0      /usr/bin/python2.7  3     auto mode
1      /usr/bin/python2.7  3     manual mode
2      /usr/bin/python3.6  1     manual mode
3      /usr/bin/python3.9  2     manual mode

 

Type the number of the version you wish to set as the default and press ENTER.

 

Now check the default Python version has changed:

 

python -V
Python 3.9.1

 

Conclusion

In this tutorial, you have learned how to create a list of alternate Python versions for update-alternatives and set the default.

python 3