update-alternatives
is a command line utility in Linux systems 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 Java installed on your system, such as Java 17, 21, and 22. If you want to use Java 21 as the default Java version, you can use the update-alternatives
command to create a symbolic link for the java
command to point to the Java 21 binary. This will make Java 21 the default version that is used when you run java
in the terminal.
Download multiple versions of Java using wget
command.
wget https://download.oracle.com/java/22/latest/jdk-22_linux-x64_bin.tar.gz
wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz
wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz
Extract it to opt
directory
sudo tar -zxf jdk-22_linux-x64_bin.tar.gz -C /opt
sudo tar -zxf jdk-21_linux-x64_bin.tar.gz -C /opt
sudo tar -zxf jdk-17_linux-x64_bin.tar.gz -C /opt
Install multiple versions of Java using update-alternatives
command with --install
option.
sudo update-alternatives --install /usr/bin/java java /opt/jdk-17.0.11/bin/java 10
sudo update-alternatives --install /usr/bin/java java /opt/jdk-21.0.3/bin/java 20
sudo update-alternatives --install /usr/bin/java java /opt/jdk-22.0.1/bin/java 30
/usr/bin/java
is the path of the symlink, java
is the name of the symlink, /opt/jdk-17.0.11/bin/java
is the path of the binary file for which you want to create the symbolic link, 10
is the priority.
To list all entries of alternatives in the system, use
sudo update-alternatives --get-selections
To list all alternatives of java, use
sudo update-alternatives --list java
In automatic mode, the alternatives pointed to java version which have the highest priority. To switch to automatic mode, use
sudo update-alternatives --auto java
To remove an alternative of java, use
sudo update-alternatives --remove java /opt/jdk-17.0.11/bin/java