Home > linux > Update-alternatives by example

Update-alternatives by example

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: ,
  1. April 17th, 2014 at 05:31 | #1

    Excellent post. I was checking constantly this blog and I am impressed!
    Very helpful iformation specifically the last part 🙂
    I care for such information a lot. I was lookinng for this certain info for a
    long time. Thank you and good luck.

  1. September 12th, 2016 at 15:46 | #1
  2. October 22nd, 2016 at 11:43 | #2
  3. November 18th, 2016 at 02:03 | #3
  4. November 24th, 2016 at 23:17 | #4
  5. February 20th, 2017 at 21:00 | #5
  6. February 25th, 2017 at 21:45 | #6