alternatives Command in Linux



The alternatives in a Linux system is a powerful command that is widely used for managing multiple versions of an application installed on the system. It allows one to choose the default application for your system in case there are multiple applications installed on the system serving the same purpose.

The alternatives command uses the symbolic link to keep track of the alternatives installed on your system. Instead of directly linking to the specific binary file, it creates symbolic links to the alternatives. After that, once you choose an alternative, the symbolic link is updated to point to the desired binary. This centralized system makes it easy for us to choose the version according to our needs.

Table of Contents

Syntax of alternatives Command in Linux

The alternatives command originally started as an alternative to the update-alternatives command. On most Debian-based Linux systems, the update-alternatives command is used. In some Linux distributions like Red HAT, and Cent OS, you can use alternatives, as both commands serve the same purpose.

So, the choice to select the alternatives commands depends on the operating system you are using.

The basic syntax to use the alternatives command on Linux is provided below −

alternatives [options] command

Or

update-alternatives [options] command

Where common options include −

  • --install − Installs a new alternative for a specific command.
  • --remove − Removes an alternative.
  • --config − Manually configure an alternative.
  • --display − Display information about the alternative.
  • --auto − Automatically configure an alternative.

Examples of alternatives Command in Linux

The alternatives command has two modes: auto and manual. In auto mode, the command will set the alternative with the highest priority, while in manual mode, you have to choose the alternative according to your priority.

Let’s discuss some examples of alternatives command on Linux.

Manually Setting the Alternative with config

One of the basic examples of alternatives command is to manually select an alternative for a program according to your choice by adding the --config. As an example, let’s select an editor program on Linux with the following alternatives command −

sudo update-alternatives --config editor

When you run the above command, you will be provided with the several editors options to choose from; all these editors are installed on your system.

alternatives Command Linux 1

To choose an editor according to your choice, enter the selection number from the list. Here, as an example, we have picked 3, the vim editor −

alternatives Command Linux 2

After this operation, the alternatives command will manually select the application and make it as your default one.

Manually Setting the Alternative with set

Besides using the --config option, you can also use the set option to manual select your default application. However, before that, first list the available applications with the same purpose on your Linux system. Since, we are finding alternatives of editor on Linux, use the following command to list all available editors binary path on your system −

update-alternatives --list editor
alternatives Command Linux 3

Then, use the update-alternatives command or alternatives command with --set option and editor keyword followed by the binary path of the selected editor you want to use. Here, we are selecting the Vim editor on Linux from the update-alternatives command −

sudo update-alternatives --set editor /usr/bin/vim.tiny
alternatives Command Linux 4

Setting the Alternative Back to Auto

If you want to auto select the default application, you can use the config command provided above and select the 0 option from the list. Alternatively, you can also use the --auto keyword with the update-alternatives command followed by the application, which is editor in our case −

sudo update-alternatives --auto editor
alternatives Command Linux 5

Installing New Alternatives

If you have installed on new alternative on your Linux system for a specific program and want to set that alternative to the highest priority, you can use the following syntax −

sudo update-alternatives --install link name path priority

Here,

  • link is the symbolic link you want to create or update.
  • name is the name of the program.
  • path is the path to the actual binary executable of the alternative.
  • priority is setting a priority for the specified alternative.

Let’s say we have installed an additional editor micro on Linux system using the following command −

sudo apt install micro -y

Now, we want to add it as an alternative for the generic editor name editor and make it the default one, for that, purpose, you can use the below-given command −

sudo update-alternatives --install /usr/bin/editor editor /usr/bin/micro 100
alternatives Command Linux 6

This will set the micro editor as the default editor for your Linux system. You can verify it by using the config command already mentioned in the above section of the guide −

alternatives Command Linux 7

Displaying Information about Alternative

If you want to find to display information about an alternative, you can add the --display option with update-alternatives command followed by the group name. As an example, we are displaying the information of alternatives of editor group using the following command −

sudo update-alternatives --display editor
alternatives Command Linux 8

Removing an Alternative

To remove an alternative of a group from the Linux system, you can use the --remove option. Let’s suppose we want to remove micro editor from the group editor, so the following command will be used for this purpose −

sudo update-alternatives --remove editor /usr/bin/micro
alternatives Command Linux 9

Note − You can replace the group name and binary location of the alternative you want to remove from the Linux system.

Listing all Alternatives

If you want to list all alternatives available in the system, you can use the alternatives command in the following way −

update-alternatives --get-selections

The above command will list the master name, their current status and binary location −

alternatives Command Linux 10

That’s all we have from the alternatives command on Linux.

Conclusion

Through this article, we explained the basics of alternatives and update-alternatives command on Linux system. Both commands are similar in terms of their working, but the first one is used on REHL and Cent OS systems, while the other on Debian-based systems.

We explained several options and examples in this guide for the alternatives command. The examples include manually and auto setting the alternatives, installing a new alternative, or displaying and deleting alternative. You can use these examples as a guidance and start switching the applications on your Linux system with the alternatives command.

Advertisements