Ubuntu

How to Use update-alternatives Command on Ubuntu

If you have 2 or more versions of the same command, you can use update-alternatives to set which one to use by default and also switch between them very easily.  For example, let’s say, you have Python 2 and Python 3 installed on your computer.  Now, you have to run a Python 2 script as follows:

$ python2 <script-path>

Here, <script-path> is the path of the Python 2 script i.e. ~/scripts/hello.py

The same way, you have to run a Python 3 script as follows:

$ python3 <script-path>

Here, <script-path> is the path of the Python 3 script i.e. ~/scripts/hello2.py

So, what if you want to access both Python 2 and Python 3 using the same command python?

Well, using update-alternatives command, you can make a new executable python (/usr/local/bin/python) and add all the available Python versions to the alternatives database. Then, you can easily set which version of Python to use by default. You can also switch between the Python versions very easily. I am going to show you how to do this practically in this article. That way, you will gain practical knowledge of how update-alternative works.

So, let’s get started.

Prerequisites:

You must have Python 2 and Python 3 installed on your Ubuntu machine if you want to follow along and try out the examples in this article.

You can install Python 2 and Python 3 with the following command on Ubuntu:

$ sudo apt install python2 python3 -y

Python 2 and Python 3 should be installed if not installed already. In my case, they are already installed.

Now, find the Python 2 and Python 3 interpreter path with the following commands:

$ which python2
$ which python3

As you can see, the Python 2 interpreter path is /usr/bin/python2 and Python 3 interpreter path is /usr/bin/python3. Memorize them. We will need this later.

Installing New Alternatives:

Now, you can create a new alternatives python and install Python 2 interpreter /usr/bin/python2 as an alternative with the priority 20 as follows:

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

NOTE: Here, /usr/local/bin/python is the binary path of the python alternatives. You can change it to some other path such as /usr/bin/python if you want. But I would suggest you place it somewhere in the /usr/local/bin/ directory as this is the directory where user-space programs should be according to the directory structure of Linux.

A new alternatives python should be created and Python 2 interpreter should be added there as an alternative.

The same way, install Python 3 interpreter /usr/bin/python3 as an alternative of the python alternatives and set it a priority of 40 with the following command:

$ sudo update-alternatives --install /usr/local/bin/python python /usr/bin/python3 40

Python 3 interpreter alternative should be added to the python alternatives.

Now, you can see more information about the python alternatives you’ve just created as follows:

$ update-alternatives --query python

As you can see, the python alternatives has 2 alternative commands /usr/bin/python2 (priority 20) and /usr/bin/python3 (priority 40).

Auto Mode and Alternative Priority:

Every alternatives in the update-alternatives database has 2 modes: auto and manual.

In auto mode, the alternatives selects the command/executable/alternative with the highest available priority by default.

In manual mode, you select the default command/executable/alternative for the alternatives manually.

The default mode for any alternatives is auto.

In the earlier section, you’ve seen I’ve set the priority for Python 2 interpreter (/usr/bin/python2) alternative 20 and for Python 3 interpreter (/usr/bin/python3) alternative 40. This was because I wanted to set Python 3 interpreter as the default alternative for the python alternatives. As the default mode is auto, the python alternatives selected the Python 3 interpreter alternative automatically as it has higher priority than Python 2 interpreter alternative.

As you can see, the mode (Status) is auto. The Python 3 interpreter (/usr/bin/python3) is the Best one as it has the highest priority. Also, the currently selected one is also the Python 3 interpreter (/usr/bin/python3) as it has the highest priority and the mode is auto.

$ update-alternatives --query python

As you can see, python command executes the Python 3 interpreter.

$ python --version

In the next section of this article, I will show you how to change the alternatives mode to manual and select a lower priority command/executable for the alternatives as well.

Changing Alternatives Command Manually:

As I have said, Python 2 won’t be automatically selected as I have set it to a lower priority. So, if you want to select a lower priority alternative, you have to do that manually.

To select Python 2 alternative manually for the python alternatives, run the following command:

$ sudo update-alternatives --config python

Now, update-alternatives will list all the commands/alternatives you have installed for the python alternatives. In my case, it’s Python 2 and Python 3 alternatives.

You also have a Selection number for each version/alternative which you can use to select a version/alternative from the list. Selection number 0 is auto. Selection number other than 0 is manual.

Here, Selection number 1 is for Python 2 alternative and Selection number 2 is for Python 3 alternative.

So, to select Python 2 alternative, press 1 and then press <Enter>.

Python 2 should be set as the default alternative for the python alternatives.

As you can see, the current mode is manual. Even though the Best alternative is /usr/bin/python3 (Python 3), it’s using /usr/bin/python2 (Python 2).

 

As you can see, python command executes the Python 2 interpreter now.

$ python --version

Settings Alternatives Mode to Auto:

If you want to switch to auto mode for the python alternatives again, run the following command:

$ sudo update-alternatives --auto python

Based on the priority, an alternative should be set for the python alternatives. In this case, the Python 3 alternative.

As you can see, python command executes the Python 3 interpreter.

$ python --version

Removing Alternative from Alternatives:

You can remove an alternative from the python alternatives.

First, list all the available alternative for the python alternatives as follows:

$ sudo update-alternatives --query python

Now, select and copy the path of the alternative you want to remove. I am going to remove the /usr/bin/python2, the Python 2 interpreter here.

To remove the /usr/bin/python2 (Python 2 interpreter) alternative from the python alternatives, run the following command:

$ sudo update-alternatives --remove python /usr/bin/python2

As you can see, the /usr/bin/python2 (Python 2 interpreter) alternative is removed from the python alternatives.

$ sudo update-alternatives --query python

Removing All Commands from Alternatives:

You can also remove all the available alternatives from the python alternatives with the following command:

$ sudo update-alternatives --remove-all python

As you can see, there are no alternatives for python any more.

$ sudo update-alternatives --query python

So, that’s how you use update-alternatives command on Ubuntu to switch between different versions of the same program or command very easily. Thanks for reading this article.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.