It is common to have multiple versions of the same software installed on a single ubuntu machine. With Debian and Ubuntu's
update-alternatives utility, it is easy to choose the default one to use.
For example, let's assume you have JDK 11 installed in the machine, and by default
java points to JDK 11:
root@ubuntu:~# java -version
Now you would like to work on a project that only supports JDK 8. After installed JDK 8 with
apt-get install openjdk-8-jdk, JDK 11 is still the default one. How can we make JDK 8 as the default?
Ubuntu keeps track of the default programs by maintaining a list of symbolic links, under
/etc/alternatives directory. Each entry here is a shortcut points to the actual program, which may have more than one option (i.e. alternatives).
List All Entries of Alternatives
To list all entries of alternatives in the system, use
root@ubuntu:~# update-alternatives --get-selections
You can see
java is pointing to actual program at
/usr/lib/jvm/java-11-openjdk-amd64/bin/java, which belongs to JDK 11.
List All Alternatives of an Entry
To list all alternatives of
update-alternatives --list java.
root@ubuntu:~# update-alternatives --list java
Set Alternatives for an Entry
java to use JDK 8 as the default, you can use an interactive command
update-alternatives --config java.
root@ubuntu:~# update-alternatives --config java
After typing selection number
update-alternatives will modify the symbolic link to update the default java to use JDK 8.
Press <enter> to keep the current choice[*], or type selection number: 2
You can also do this in a script without interaction, if you know the full path of desired default program.
root@ubuntu:~# update-alternatives --set java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
Repeat This for All Programs
Now you have pointed
java program from JDK 11 to JDK 8 with
update-alternatives command. However, some other programs like
keytool are still pointing to JDK 11. You will need to repeat this process until all programs have been updated.