1. Overview
In this tutorial, let’s look at how to set the default Python in our system based on our preferred Python version. Most Linux distros usually have the latest stable release of Python included as the default, and in older systems, the deprecated Python2.* is the default.
We’ll see how we can change the default Python using the alias and update-alternatives commands.
2. Setting Default Using the alias Command
Before we make any changes, let’s check which version of Python is available in our system:
$ python --version
Python 3.10.6
$ python3 --version
Python 3.10.6
In the above snippet, the python command with the –version option shows our system’s currently set default Python version. Likewise, python3 displays whichever version of Python3 is installed.
To check for Python2, we run python2 –version. Alternatively, we can run python -V to get the same result.
The alias command lets us create shortcuts for commands or override the default options for the existing commands. Assuming we’ve installed Python3.8 in the /usr/bin directory and want it to be our default Python, we can use the alias command:
$ alias python=/usr/bin/python3.8
In this example, we’ve overridden the default Python and set our preferred one:
Using an alias, we can set both python and python3 commands to the same path. However, upon shutdown or rebooting, the setting isn’t retained. For this reason, to make the change permanent, we need to edit the bash_aliases file by appending our alias to it:
$ sudo vi ~/.bash_aliases
alias python=/usr/bin/python3.8
alias python3=/usr/bin/python3.8
We then save the file and activate the alias:
$ source ~/.bash_aliases
It’s important not to add our alias to the ~/.bashrc file. This is because if we do so, we won’t be able to use the aliased command with sudo.
Again, let’s check the version and find out what is the default version set:
$ python --version
Python 3.8.16
3. Using the update-alternatives Command
Now, we’ll look at how we can set the default Python version to whatever version we want using the update-alternatives command. We use this command to maintain symbolic links determining default commands.
Let’s say we’ve got several versions of Python installed. For instance, let’s assume these are the versions we currently have:
$ whereis python
python: /usr/bin/python3.5-config /usr/bin/python3.5m-config /usr/bin/python2.7 /usr/bin/python3.5 /usr/bin/python3.5m /usr/bin/python /usr/lib/python2.7 /usr/lib/python3.5 /etc/python2.7 /etc/python3.5 /etc/python /usr/local/bin/python3.11-config /usr/local/bin/python3.11 /usr/local/lib/python2.7 /usr/local/lib/python3.5 /usr/local/lib/python3.11 /usr/include/python3.5 /usr/include/python3.5m /usr/share/python /usr/share/man/man1/python.1.gz
Next, suppose we want the Python in /usr/local/bin/python3.11 set as the default. We’ll use the update-alternatives command following this syntax:
$ sudo update-alternatives --install needs <link> <name> <path> <priority>
We should note that flags with higher priority numbers have higher precedence in automatic mode:
$ sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.11 20
$ sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/local/bin/python3.11 20 auto mode
1 /usr/bin/python3 10 manual mode
2 /usr/local/bin/python3 20 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Depending on the Python version we want to be the default, we select a number shown and then click on the enter key.
Once more, let’s check if the Python version has changed to that contained in the path we chose:
$ python -V
Python 3.11
If there’s only a single link, we’ll get this error:
$ sudo update-alternatives --config python
There is only one alternative in link group python (providing /usr/bin/python):
/usr/bin/python3 Nothing to configure.
Additionally, we’ll also get this error if we don’t set the priority number:
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3
update-alternatives: --install needs <link> <name> <path> <priority>
Use 'update-alternatives --help' for program usage information.
4. Conclusion
In this article, we’ve looked at the two ways to set the default Python version of our preference. We also discussed some common errors we encounter while using the update-alternatives command.