Archive

Posts Tagged ‘update-alternatives’

Update-alternatives by example

March 13th, 2014 1 comment

Hi everyone,

some Linux distributions are using a symlink-based method to manage multiple versions of the same binary. For example one could have several Java VM version installed on the same system. Usually typing the java command will start one of them … but which one? The command update-alternatives manages a set of symlinks, located in /etc/alternatives by default, which refer to the correct binary. In the case of java, /etc/alternatives/java will refer to the binary somewhere in /usr/lib/jvm (for Debian). To change this link just call:

sudo update-alternatives –config java
There are 2 choices for the alternative java (providing /usr/bin/java).

Selection Path Priority Status
————————————————————
* 0 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 1071 auto mode
1 /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java 1061 manual mode
2 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 1071 manual mode

Press enter to keep the current choice[*], or type selection number:

The command’s output will show a list of installed alternatives for this binary, which will be configured by the packet manager. Selecting a number will change the symlink.

To add a custom alternative, update-alternatives –install is used. The description of this command can be found in it’s manpage, but I think an example will make it more clear. Let’s say we got JDK7 from the oracle website and want to add it. First install/copy it do a useful destination. I’ve chosen /usr/lib/jvm/java-7-sunjdk-amd64/jdk/. 😉

update-alternatives –install /usr/bin/java java /usr/lib/jvm/java-7-sunjdk-amd64/jdk/bin/java 1072

The first parameter (excluding –install) is the path of the symlink your system will use to find the binary. Therefore, it has to be at a location listed in your PATH environment variable. Thereafter, the name of the group is specified followed by the absolute location of the binary. A group is an alias which assorts the similar binaries. Finally, a priority value is set. The result looks like this:

update-alternatives –config java
There are 3 choices for the alternative java (providing /usr/bin/java).

Selection Path Priority Status
————————————————————
0 /usr/lib/jvm/java-7-sunjdk-amd64/jdk/bin/java 1072 auto mode
* 1 /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java 1061 manual mode
2 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 1071 manual mode
3 /usr/lib/jvm/java-7-sunjdk-amd64/jdk/bin/java 1072 manual mode

 

Categories: linux Tags: ,