Signup/Sign In

update-alternatives command in Linux

If you have two or more versions of the same command, you can quickly switch between them and choose which one to run by default by using update-alternatives. The symbolic links that make up the package alternatives system are created, removed, maintained, and shown using the update-alternatives command.

Multiple programmes that perform the same or related functions may be installed simultaneously on a single system. For instance, many systems simultaneously have multiple text editors installed. This provides system users the option to choose their editor of choice if they so choose, but also makes it challenging for a program to choose an editor to invoke if the user has not indicated a specific preference

How to install update-alternatives command?

The update-alternatives command is a part of a package manager utility and it is installed with dpkg/chkconfig package:

  • Debian/Ubuntu/Raspbian - apt-get install dpkg
  • Alpine - apk add dpkg
  • CentOS - yum install chkconfig
  • Fedora - dnf install chkconfig
  • OS X - brew install dpkg
  • Dockerfile - dockerfile.run/update-alternatives
  • Docker - docker run cmd.cat/update-alternatives update-alternatives

The general syntax of update-alternatives command:

update-alternatives [option...] command

update-alternatives command in linux terminal

How to use update-alternatives command?

Let's say you have Python 2 and Python 3 installed on your PC as an example. You can create a new Python executable (/usr/local/bin/python) and add every Python version that is currently available to the alternatives database by using the update-alternatives command. The Python version that will be used by default can then be readily adjusted and it can also be switched simply.

How create a new alternatives python and install Python 2 interpreter /usr/bin/python2 as an alternative with the priority 10 as follows:

sudo update-alternatives --install path/to/symlink command_name path/to/command_binary priority

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

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 It is always suggested that you should 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.

Example uses of update-alternative command:

1. Syntax used to add a symbolic link:

sudo update-alternatives --install /usr/local/bin/java java /opt/java/jdk1.8.0_192/bin/java 3

2. Configure a symbolic link for `java`

sudo update-alternatives --config python

3. Remove a symbolic link

sudo update-alternatives --remove java /opt/java/jdk1.8.0_192/bin/java

4. Display information about a specified command

update-alternatives --display python

5. Display all commands and their current selection

update-alternatives --get-selections

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

sudo update-alternatives --auto python

Example: Bash script to automate the installation of JDK

#!/bin/bash
cd /opt/
sudo mkdir java
sudo tar -zxvf ~/Downloads/jdk-8u192-linux-x64.tar.gz
sudo ln -s  jdk1.8.0_192 current
for file in /opt/java/current/bin/*
do
   if [ -x $file ]
   then
      filename=`basename $file`
      sudo update-alternatives --install /usr/bin/$filename $filename $file 20000
      sudo update-alternatives --set $filename $file
      #echo $file $filename
   fi
done

This script is intended to set up an alternative version of a Java Development Kit (JDK) on a Linux system. It installs the JDK in the /opt/java directory, creates a symbolic link called current that points to the JDK installation, and installs the JDK executables as alternatives for the corresponding commands. This allows the user to switch between different versions of the JDK by using the update-alternatives command.

galternatives: GUI for update-alternatives

In addition to the update-alternatives command, Linux also provides a graphical tool called galternative that can be used to manage alternative programs. Here are the commands used to install galternative in Debian-based operating systems:

sudo apt-get install galternatives
sudo galternatives

galternative is a useful tool for managing alternatives on a Linux system, and can be a convenient option for users who prefer a graphical interface to the command line. However, it is important to note that galternative is not available on all Linux distributions, and may not be installed by default.

galternatives tool in Linux operating systems to install and change location of different commands.

Conclusion

The update-alternatives command is a useful tool for managing multiple versions of a program on a Linux system. It allows the user to specify which version of a program should be used by default, and provides an easy way to switch between alternatives if needed. By using the various flags and options available with the update-alternatives command, users can easily manage the alternatives for any command on their system. Lastly, galternatives provides a GUI for update-alternative which makes it really smooth to edit configuration.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.