Linux systems allow easily switching between programs of similar functionality or goal. So we can set a given version of a utility program or development tool for all users. Moreover, the change applies not only to the program itself but to its configuration or documentation as well.
In this tutorial, we’ll learn about the update-alternatives command.
2. Command’s Basics
The command uses symbolic links to keep track of alternatives. Then, update-alternatives accepts commands to manage alternatives without going through the underlying links.
Now, let’s check the command’s version:
$ update-alternatives --version Debian update-alternatives version 1.21.1. This is free software; see the GNU General Public License version 2 or later for copying conditions. There is NO warranty.
Let’s notice that the command is of Debian origin, but it’s also adopted in other distributions like Fedora. However, on Debian-like distributions, it well integrates with the rest of the system. So we’ll work on Ubuntu 22.04 LTS.
3. The editor Example
Let’s check editor, which is usually set during the installation of the system. So, let’s review its configuration with config:
Let’s notice the Selection column with a numeric key to pick up the alternative. The star symbol indicates that nano is the current alternative. Next, we see the path to the program. Then comes the program’s priority and status – either auto or manual.
4. Selecting Alternative
Let’s discern the manual and auto mode. In manual mode, we don’t need to regard priorities. On the contrary, the automatic mode always sets the alternative with the highest priority.
So, the auto mode is helpful to keep order after installing a new alternative or a new package. In addition, an automatic choice is a fail-safe option after removing the current alternative.
4.1. Choosing the Alternative in Manual Mode With config
Now let’s change the program hidden behind the generic name editor. So we need to run update-alternatives with config as sudo user. Subsequently, we’re going to choose vim.tiny:
4.2. Choosing the Alternative With set
Instead of working interactively, we can select the current editor with the set command. However, we need the path to the desired program. So, let’s obtain it with the list command:
$ update-alternatives --list editor /bin/ed /bin/nano /usr/bin/mcedit /usr/bin/vim.basic /usr/bin/vim.tiny
Then, let’s choose mcedit:
$ sudo update-alternatives --set editor /usr/bin/mcedit update-alternatives: using /usr/bin/mcedit to provide /usr/bin/editor (editor) in manual mode
4.3. Set Back to Auto Mode
Let’s switch the editor‘s alternatives back to auto mode. So, we can use config once again and select zero. However, we can use the auto command as well:
$ sudo update-alternatives --auto editor update-alternatives: using /bin/nano to provide /usr/bin/editor (editor) in auto mode
5. Links Configuration
Assuming that nano is our choice, let’s check with whereis what editor really is:
$ whereis editor editor: /usr/bin/editor /usr/share/man/man1/editor.1.gz
Then let’s examine /usr/bin/editor:
$ ls -all /usr/bin/editor lrwxrwxrwx 1 root root 24 Apr 19 12:03 /usr/bin/editor -> /etc/alternatives/editor
Thus, it doesn’t point to /bin/nano, as we’d expect at the first glance. Let’s follow this link:
$ ls -all /etc/alternatives/editor lrwxrwxrwx 1 root root 17 Aug 9 18:24 /etc/alternatives/editor -> /usr/bin/nano
So, the interim link in the /etc/alternatives folder points to the actual program. Furthermore, this folder is a place to keep all alternative links system-wide.
5.1. Querying Alternatives Links With display
Now, let’s extract editor‘s links with display:
$ update-alternatives --display editor editor - auto mode link best version is /bin/nano link currently points to /bin/nano link editor is /usr/bin/editor slave editor.1.gz is /usr/share/man/man1/editor.1.gz slave editor.da.1.gz is /usr/share/man/da/man1/editor.1.gz # ... /bin/ed - priority -100 slave editor.1.gz: /usr/share/man/man1/ed.1.gz # ... /usr/bin/vim.tiny - priority 15 slave editor.1.gz: /usr/share/man/man1/vim.1.gz # ...
From the output, we can learn that we’re in the automatic mode, that the current editor is nano, and that nano has the best score alternative. Then, we’re provided with data concerning all other editor‘s alternatives.
5.2. Master and Slave Links
Let’s find out from display‘s output that the link to the actual program may be accompanied by slave links. Consequently, the program’s link is the master one. The slave links point to other files and should follow the change of the master. In our case, they point to manuals.
So, let’s select vim as editor and check editor‘s manual:
$ sudo update-alternatives --set editor /usr/bin/vim.tiny update-alternatives: using /usr/bin/vim.tiny to provide /usr/bin/editor (editor) in manual mode
Which turns out to be the vim‘s one:
$ man editor | head -n 1 VIM(1) General Commands Manual VIM(1)
So, the change of the editor‘s alternative is reflected by the appropriate change to the manual.
Let’s notice that we can set slaves links not only to documentation but also to configuration or even other executable files. So we have a flexible way to change the whole surroundings of the program with one command.
6. Managing Alternatives
Let’s look through some common actions which we can take during work with the alternatives.
6.1. Adding New Alternative With install
Let’s assume that we’ve installed yet another editor, micro:
$ sudo apt-get install micro
Next, we can add it to the alternatives with install:
sudo update-alternatives --install /usr/bin/editor editor /usr/bin/micro 100
Let’s look through the command’s arguments:
- /usr/bin/editor – the alternative link – it’s the same for all alternatives
- editor – the generic name – it corresponds to the link
- /usr/bin/micro – the path to the actual editor
- 100 – new alternative’s priority
Let’s notice that the priority is higher than the highest to date. So the new editor becomes the automatic mode choice. However, we can’t overwrite the manual selection in this way.
6.2. Updating Slave Links
With micro selected, let’s try to obtain the editor‘s manual:
$ man editor No manual entry for editor
So we need to add a slave link to the micro‘s links group. First, let’s check the editor‘s links:
$ sudo update-alternatives --display editor editor - auto mode link best version is /usr/bin/micro link currently points to /usr/bin/micro link editor is /usr/bin/editor slave editor.1.gz is /usr/share/man/man1/editor.1.gz # more output skipped
So we can find a slave manual link /usr/share/man/man1/editor.1.gz. Unfortunately, it doesn’t exist:
$ ls /usr/share/man/man1/editor.1.gz ls: cannot access '/usr/share/man/man1/editor.1.gz': No such file or directory
Thus, let’s check what micro offers with whereis:
$ whereis micro micro: /usr/bin/micro /usr/share/man/man1/micro.1.gz
Finally, we need to reinstall micro and add the lacking manual link with slave:
$ sudo update-alternatives --install /usr/bin/editor editor /usr/bin/micro 100 \ --slave /usr/share/man/man1/editor.1.gz editor.1.gz /usr/share/man/man1/micro.1.gz
Let’s notice the syntax similar to that of install. First, we need to provide the generic link, then the generic name – editor.1.gz, and finally the location of the actual file. Moreover, with one call to update-alternatives we can add as many slave links as we want.
6.3. Deleting Alternative With remove
Now, let’s get rid of the micro alternative in the editor group with remove:
$ sudo update-alternatives --remove editor /usr/bin/micro
Let’s notice that we need the path to the targeted program. Then, if the removed alternative has been chosen manually, the group switches to the auto mode. On the other hand, removing the automatic choice alternative leads to selecting a new one with the highest priority.
6.4. Listing Alternatives With get-selections
Finally, let’s list all alternatives available in the system with the get-selections command:
$ update-alternatives --get-selections animate auto /usr/bin/animate-im6.q16 animate-im6 auto /usr/bin/animate-im6.q16 arptables auto /usr/sbin/arptables-nft awk auto /usr/bin/gawk builtins.7.gz auto /usr/share/man/man7/bash-builtins.7.gz c++ auto /usr/bin/g++ # ...
So we see the master name, status, and file name of the actual program.
7. Integration With the Software Manager
On Debian-like Linux distributions, alternatives may update during installing or removing applications with apt-get.
As an example, let’s install one more editor, joe:
$ sudo apt-get install joe
Now, let’s go straight to checking editor with display:
So, we can see that joe is on the list with priority 70. As we’ve been in the auto mode, it makes joe the new choice.
Let’s notice that this happens thanks to the postinst script, which is triggered after the installation of the package. By the same token, the prerm and postrm scripts update alternatives during the removal of the package.
These scripts come with a package, which contains the program. In the case of joe, we can find joe.postinst and joe.prerm in /var/lib/dpkg/info.
Some packages as e.g. micro doesn’t provide the means to update the alternative system. So, if such a program is the current one, we can face a warning about a dangling alternative after removal.
In this article, we learned how to manage alternative programs. First, we introduced the update-alternatives command, which facilitates the managing of the different programs in the group of alternatives. Then, we studied the example of the editor group.
Next, we examined the underlying system of symbolic links. Moreover, we learned that slave links could switch other files related to the alternative.
As the next topic, we looked through some common actions with alternatives. Finally, we checked how the installation or removal of the program’s package might update-alternatives.