It is common to have multiple versions of the same software installed on a single ubuntu machine. With 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 install 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).
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.
To list all alternatives of
update-alternatives --list java.
root@ubuntu:~# update-alternatives --list java
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