How to set default programs using update-alternatives on Debian-based distributions

More often than not, on our Linux system, we can find two or more applications of the same type installed: it is typically the case of text editors, but we can also have multiple web browsers, for example. Setting the default application used to perform a specific task system-wide, however, can sometimes be problematic. To solve this problem, on Debian and Debian-based Linux distributions, we can use the alternatives system and the “update-alternatives” tool.

In this tutorial we see how to use the Debian alternatives system and the “update-alternatives” tool to set the default program for a generic application type on Debian and Debian-based distributions.

In this tutorial you will learn:

  • How the Debian alternatives system is organized
  • How to use the update-alternatives tool to list the available alternatives group
  • How to list all the alternatives for a group and their priorities
  • How to change the current alternative for a group
  • How to create a custom group
  • How to delete one or all the alternatives for a group
How to set default programs using the Debian alternatives system
How to set default programs using the Debian alternatives system

Category Requirements, Conventions or Software Version Used
System Debian-based Linux distributions
Software update-alternatives
Other Administrative privileges
Conventions # – requires given linux-commands to be executed with root privileges either directly as a root user or by use of sudo command $ – requires given linux-commands to be executed as a regular non-privileged user

How the Debian alternatives system works

The Debian alternatives system makes use of basically two concepts: group names and symbolic links. Let’s see consider the case of text editors as an example. Even on minimal configurations we typically find multiple text editors installed, among the others, nano and vi.



To set the default system text editor, the Debian alternatives system uses an abstraction or generic name (“editor”) in this case. A file with the generic/group name, in the /etc/alternatives directory is a symbolic link which points to the actual application which is used as the default. The default text-editor in Debian is “nano”:

lrwxrwxrwx 1 root root 9 Jan 30 12:20 editor -> /bin/nano

As you can see from the output above, inside the /etc/alternatives directory, editor is a symbolic link to the nano executable: /bin/nano. Additionally, a symbolic link to the group name itself is typically created under /usr/bin (/usr/bin/editor in this case): this allows us to invoke the generic name as if it was a real application.

When we want to get information about alternatives, or add modify them, however, we don’t have to create symbolic links manually. What we need to do, instead, is to use the update-alternatives utility. Let’s see how.

Listing all the available groups

To list all the groups of alternatives available on the system, we just invoke “update-alternatives” with the --get-selections option:

$ update-alternatives --get-selections

The command  above returns the following (truncated) output:

arptables                      auto     /usr/sbin/arptables-nft
awk                            auto     /usr/bin/mawk
builtins.7.gz                  auto     /usr/share/man/man7/bash-builtins.7.gz
cpp                            auto     /usr/bin/cpp
default-GM.sf2                 auto     /usr/share/sounds/sf2/TimGM6mb.sf2
default-GM.sf3                 auto     /usr/share/sounds/sf2/TimGM6mb.sf2
desktop-background             auto     /usr/share/desktop-base/active-theme/wallpaper/contents/images/1920x1080.svg
desktop-background.xml         auto     /usr/share/desktop-base/active-theme/wallpaper/gnome-background.xml
desktop-grub                   auto     /usr/share/desktop-base/active-theme/grub/grub-4x3.png
desktop-lockscreen.xml         auto     /usr/share/desktop-base/active-theme/lockscreen/gnome-background.xml
desktop-login-background       auto     /usr/share/desktop-base/active-theme/login/background.svg
desktop-plasma5-wallpaper      auto     /usr/share/desktop-base/active-theme/wallpaper
desktop-theme                  auto     /usr/share/desktop-base/homeworld-theme
ebtables                       auto     /usr/sbin/ebtables-nft
editor                         auto     /bin/nano
[...]

In the first column we can read the group name, and in the second the “mode” the group is in (more on this later); in the third column the current target the group symbolic link points to, is reported.

Listing the available alternatives for a group

To list the available alternatives for a specific group, all we have to do is to invoke update-alternatives with the --display option, and pass the group name as argument. To retrieve the possible alternatives for the “editor” group, we would run:

$ update-alternatives --display editor



The command above returns the following output:

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
  slave editor.de.1.gz is /usr/share/man/de/man1/editor.1.gz
  slave editor.fr.1.gz is /usr/share/man/fr/man1/editor.1.gz
  slave editor.it.1.gz is /usr/share/man/it/man1/editor.1.gz
  slave editor.ja.1.gz is /usr/share/man/ja/man1/editor.1.gz
  slave editor.pl.1.gz is /usr/share/man/pl/man1/editor.1.gz
  slave editor.ru.1.gz is /usr/share/man/ru/man1/editor.1.gz
/bin/nano - priority 40
  slave editor.1.gz: /usr/share/man/man1/nano.1.gz
/usr/bin/vim.tiny - priority 15
  slave editor.1.gz: /usr/share/man/man1/vim.1.gz
  slave editor.da.1.gz: /usr/share/man/da/man1/vim.1.gz
  slave editor.de.1.gz: /usr/share/man/de/man1/vim.1.gz
  slave editor.fr.1.gz: /usr/share/man/fr/man1/vim.1.gz
  slave editor.it.1.gz: /usr/share/man/it/man1/vim.1.gz
  slave editor.ja.1.gz: /usr/share/man/ja/man1/vim.1.gz
  slave editor.pl.1.gz: /usr/share/man/pl/man1/vim.1.gz
  slave editor.ru.1.gz: /usr/share/man/ru/man1/vim.1.gz

In the very first line of the output we can see the mode the group is currently in. A group can be in two modes: auto (default) or manual. When a group is in “auto” mode, as in this case, the alternatives system automatically decides how the links are updated on the base of priorities, when packages are installed or removed. In this case, the alternatives for the “editor” group are “/bin/nano” and “/usr/bin/vim.tiny”. The former has an higher priority (40) than the latter (15), therefore is automatically selected as the default alternative. A group is put in “manual” mode after the system administrator performs manual changes.

To visualize just the path of the alternatives available for a group, without other information, we can use the --list option:

$ update-alternatives --list editor
/bin/nano
/usr/bin/vim.tiny

Setting the current alternative for a group

Setting the current alternative for a group is really easy. We can perform such operation in interactive or non-interactive way. The latter is useful because can be scripted, while the former is more user-friendly, and requires the user confirmation; let’s see them in order.

Setting a group current alternative interactively

To change an alternative for a group interactively, we invoke update-alternatives with the --config option and pass the name of the group as argument. When performing a writing operation, we need to launch the command using privilege escalation. Supposing we want to set vi as the default editor (the sane choice), we would run:

$ sudo update-alternatives --config editor

As soon as we launch the command, the available options will be displayed, each associated with a number, and we will be prompted to enter the one corresponding to our choice:

There are 2 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path               Priority   Status
------------------------------------------------------------
* 0            /bin/nano           40        auto mode
  1            /bin/nano           40        manual mode
  2            /usr/bin/vim.tiny   15        manual mode

Press  to keep the current choice[*], or type selection number: 2

In this case we enter “2”. A confirmation message is displayed:

update-alternatives: using /usr/bin/vim.tiny to provide /usr/bin/editor (editor) in manual mode

Setting the current group alternative non-interactively

If we need to change the current alternative for a group directly, perhaps from a script, we need to use another option: --set. In such case, we must provide the name of the group as first argument, and the path of the alternative we want to use for that group as the second one. Sticking to the previous example, to set vi, which on debian is /usr/bin/vim.tiny, as the current alternative for the “editor” group, we would run:

$ sudo update-alternatives --set editor /usr/bin/vim.tiny

Adding a custom group to the alternatives system

By using the “update-alternative” tool, we can create custom groups in the Debian alternative systems. To perform such action we use the --install option, and provide, in order:

  1. The path of the master link to be used as an executable
  2. The name of the symlink as it shoud appear in the /etc/alternatives directory
  3. The path of the alternative
  4. The priority of the alternative

Let’s clarify this with an example. Suppose we want to create a new group called “python-editor” and we want to set “idle” (Python Integrated Development and Learning Environment) and Vim as alternatives for that group, giving the former an higher priority. To set Idle as a default alternative, we would run:

$ sudo update-alternatives --install /usr/bin/python-editor python-editor /usr/bin/idle 40



Once we launch the command above, if everything goes as expected, the system will respond with the following message:

update-alternatives: using /usr/bin/idle to provide /usr/bin/python-editor (python-editor) in auto mode

The command created the /usr/bin/python-editor master link (first argument), which points to the provided file name (second argument) inside the /etc/alternatives directory:

$ ls -l /usr/bin/python-editorlrwxrwxrwx 1 root root 31 Jan 31 06:54 /usr/bin/python-editor -> /etc/alternatives/python-editor

The /etc/alternatives/python-editor file is itself a link which points to the “real” executable of the alternative we choose (third argument):

$ ls -l /etc/alternatives/python-editor
lrwxrwxrwx 1 root root 13 Jan 31 06:54 /etc/alternatives/python-editor -> /usr/bin/idle

The priority assigned to the alternative is the one we provided as fourth argument to the command (40):

python-editor - auto mode
  link best version is /usr/bin/idle
  link currently points to /usr/bin/idle
  link python-editor is /usr/bin/python-editor
/usr/bin/idle - priority 40

To add Vim as an alternative with a less priority we would just repeat the command with the appropriate changes:

$ sudo update-alternatives --install /usr/bin/python-editor python-editor /usr/bin/vim 30

Removing one or all alternatives from a group

If for some reason we want to remove an alternative from a group, we just use “update-alternatives” with the --remove option, and provide the name of the group as first argument, and the path of the alternative we want to remove, as the second one. To remove the /usr/bin/vim alternative from the “python-editor” group we created in the previous example, we would run:

$ sudo update-alternatives --remove python-editor /usr/bin/vim

To remove all existing alternatives from a group, instead, we need to use the --remove-all option, and pass just the name of the group as argument:

$ sudo update-alternatives --remove-all python-editor

Removing all the alternatives from a group causes the removal of the group itself, together with all the related symbolic links.

Conclusions

In this tutorial we learn how to use the Debian alternatives system and the “update-alternatives” utility to set the default program for a generic application type. We saw how to list all the available groups and all the related alternatives with their priorities, how to change the current alternative in a group, how to create and populate a custom group, and, finally, how to remove one or all alternatives from a group.



Comments and Discussions
Linux Forum