update-alternatives is a Linux command-line utility that is used to maintain symbolic links for determining the default commands. It is commonly used in systems with multiple versions of the same command installed, such as different versions of Java, Python, or editors like Vim or Emacs. update-alternatives allows you to choose which version of the command should be used as the default system-wide.
When you install multiple versions of a command or program, they are usually installed in different directories. update-alternatives creates symbolic links in the system’s bin directories (e.g., /usr/bin) to point to the specific version of the program you want to use. These symbolic links are used as the default command when you run the command’s name in the terminal.
For example, suppose you have multiple versions of Python installed on your system, such as Python 2.7, 3.6, and 3.9. If you want to use Python 3.9 as the default Python version, you can use the update-alternatives command to create a symbolic link for the python command to point to the python3.9 binary. This will make Python 3.9 the default version that is used when you run python in the terminal.
To use update-alternatives, you first need to register the program with the utility using the –install option. This option takes several arguments, including the name of the command, the path to the binary, and a priority value that determines the order in which the versions are presented to the user when selecting the default. After registering the program, you can use the –config option to select the default version and –remove to remove a registered alternative.
update-alternatives Command Examples
1. Add a symbolic link:
# sudo update-alternatives --install path/to/symlink command_name path/to/command_binary priority
2. Configure a symbolic link for `java`:
# sudo update-alternatives --config java
3. Remove a symbolic link:
# sudo update-alternatives --remove java /opt/java/jdk1.8.0_102/bin/java
4. Display information about a specified command:
# update-alternatives --display java
5. Display all commands and their current selection:
# update-alternatives --get-selections
In summary, update-alternatives is a powerful and convenient tool for maintaining symbolic links to determine default commands in Linux. It allows you to easily switch between different versions of a command and choose which version should be used as the default system-wide.