Update Alternatives

Updated on 18 Apr 2021

Python

On my system I have python2 and python3 installed.

If I want to run a python script I need to use one of the following commands.

python2 helloWorld.py

or

python3 helloWorld.py

What I really want is

python helloWorld.py

Introduction update-alternatives

With update-alternatives it is possible to have different versions of the same program installed on the system which you can then access via a common command. The great thing with update-alternatives is that it is easy to switch the version of the program that you wish to use.

So for this example I want to use python as the command, and whatever I have selected as the default version in update-alternatives is the version of the interpreter that will be used.

Adding applications to update-alternatives

First lets find out where our installations of python are located.

which python2
which python3

Now we can add the executables.

sudo update-alternatives --install /usr/local/bin/python python /usr/bin/python2 20
sudo update-alternatives --install /usr/local/bin/python python /usr/bin/python3 30

The best way to understand this is that the first part is essentially referencing a symbolic link, the second part is the actual application we want it to link to, and the numbers are a priority in terms of what should take precendence when we call python.

my_link my_name application_path priority
  • my_link /usr/local/bin/python
  • my_name python
  • application_path /usr/bin/python2
  • priority 20

Viewing update-alternatives

Here are several commands to help you see which version of Python you are using. (Please note that after performing the above steps, you will need to start a new command line terminal)

which python
python --version

sudo update-alternatives --query python

sudo update-alternatives --config python

The entry with the * is the currently selected alternative. auto mode is selected by default, and will choose the application version based on the priority. (higher number => higher priority)

sudo update-alternatives --display python

Changing the default application

At the moment I have python3 as the default intepreter, however I can easily change that with the following steps.

sudo update-alternatives --config python

You are presented with a menu to change the application you wish to have associated with python. In this case I will enter 1 so that python2 is now my default interpreter.

Now when I run the following command

python --version

I get the python version associated with the previous selection I made

Removing entries

Removing an entry is done with the following syntax.

sudo update-alternatives --remove my_name application_path

Example

sudo update-alternatives --remove python /usr/bin/python3

And now if you have a look inside the config, you’ll see that python3 alternative has been removed. Also if something is really, really bad; you can always check /etc/alternatives, and try and fix things manually from there.