Debian update-alternatives

Auto Date Tuesday, August 29th, 2006

A real brilliant feature of Debian is the alternatives system. Couple of days ago i was playing with java. and I wanted both the Sun java and the Kaffe version installed on my system. The problem here is, how to easily tell wich java compiler to use, the sun version or kaffe? I’m a big fan of the ruby language and i regularly switch between version of the ruby interpeter for testing. You could achieve this in a couple of ways, change the shebang to point to the correct version of the interpeter or don’t use the shebang but instead call the correct interpeter before executing each script or maybe you change the PATH and have the preferred interpeter somewhere in the beginning of your PATH. I don’t really like any of these solutions for various reasons, there is a solution and you might have guessed it “the alternatives system”.

NOTE: output of “ls -l” is edited in order to reduce the width on this page. Also command line commands might be truncated via the backslash character.

Java

Lets dive right in and start looking at the problems described above. The first problem we encountered was the java compiler. If you installed the sun java package via “java-package” package from debian then your all set, debian already configured the alternatives for you.

$ java -version            
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

This tells us that currently we are running the sun java version, also checkout the man page of java. Notice that NAME section reads.

java - Java application launcher

If you scroll all the way to the bottom of the man page down to the SEE ALSO section, you see external links to the http://java.sun.com website. So clearly we are dealing with the sun java man pages as well. Like i said if you installed the “java-package” then debian has already setup the alternatives system for you. Let us inspect the java binary.

$ which java
/usr/bin/java
$ ls -l /usr/bin/java
lrwxrwxrwx  /usr/bin/java -> /etc/alternatives/java

As you can see the we tried to locate the java command we found that it was being execute from the /usr/bin path, however as you can see it’s a symbolic link and it links to /etc/alternatives/java. The /etc/alternatives directly is where all the alternatives are configured, let inspect a little further.

$ ls -l /etc/alternatives/java
lrwxrwxrwx  /etc/alternatives/java -> /usr/lib/j2re1.5-sun/bin/java
$ ls -l /usr/lib/j2re1.5-sun/bin/java
-rwxr-xr-x  /usr/lib/j2re1.5-sun/bin/java

There we go, another symbolic link, this time referring to /usr/lib/j2re1.5-sun/bin/java and a little digging afterwards reveals us that this is the real java executable. By now you might know how the alternatives system works or maybe you find it very inconvenient to have all these symlinks pointing to other symlinks and so on. What the alternatives system does is that it will link the executable in the default path to the appropriate file in /etc/alternatives and this will finally link to the executable we actually want to use. Because of this it becomes really easy to just update the symlink in alternatives directory and symlink it to the executable we want to use. We don’t do this linking by hand, there is a nice tool for us that manages the symlinks called “update-alternatives”.

Using update-alternatives we could quickly figure out which alternatives are currently configured for a certain executable. Let us look at the current setup of java.

$ /usr/sbin/update-alternatives --display java
java - status is auto.
link currently points to /usr/lib/j2re1.5-sun/bin/java
/usr/lib/kaffe/bin/java - priority 300
slave java.1.gz: /usr/share/man/man1/java.kaffe.1.gz
/usr/lib/j2re1.5-sun/bin/java - priority 315
slave java.1.gz: /usr/lib/j2re1.5-sun/man/man1/java.1.gz
Current `best' version is /usr/lib/j2re1.5-sun/bin/java.

As you can see the current link point to /usr/lib/j2re1.5-sun/bin/java as we already discovered. There is another alternative which is /usr/lib/kaffe/bin/java and it has a priority of 300, the priority is important we will get to that shortly. The priority of /usr/lib/j2re1.5-sun/bin/java is 315. Did you notice the “java - status is auto.” on top? This means that the alternatives system will look at which package as the highest priority and uses that executable. Because of this the last line says that the current best priority link is /usr/lib/j2re1.5-sun/bin/java.

Now if we want to use kaffe instead, we can tell update-alternatives to use kaffe instead, let us do this by using the interactive mode.

$ sudo /usr/sbin/update-alternatives --config java 

There are 2 alternatives which provide `java'.

Selection    Alternative
-----------------------------------------------
      1        /usr/lib/kaffe/bin/java
*+    2        /usr/lib/j2re1.5-sun/bin/java

Press enter to keep the default[*], or type selection number: 1
Using `/usr/lib/kaffe/bin/java' to provide `java'.

In interactive mode we see al the available alternatives for java. We already knew by looking at the –display output that there where only two option. Now we can select which option we want and as you can see i selected select number 1. No let’s look at the output of display again.

$ /usr/sbin/update-alternatives --display java
java - status is manual.
link currently points to /usr/lib/kaffe/bin/java
/usr/lib/kaffe/bin/java - priority 300
slave java.1.gz: /usr/share/man/man1/java.kaffe.1.gz
/usr/lib/j2re1.5-sun/bin/java - priority 315
slave java.1.gz: /usr/lib/j2re1.5-sun/man/man1/java.1.gz
Current `best' version is /usr/lib/j2re1.5-sun/bin/java.

There we got “link currently points to /usr/lib/kaffe/bin/java” so let’s do the same checking as we did before.

$ which java
/usr/bin/java
$ ls -l /usr/bin/java
lrwxrwxrwx  /usr/bin/java -> /etc/alternatives/java
$ ls -l /etc/alternatives/java
lrwxrwxrwx  /etc/alternatives/java -> /usr/lib/kaffe/bin/java
$ java -version
Kaffe Virtual Machine

Copyright (c) 1996-2004 Kaffe.org project contributors (please see
  the source code for a full list of contributors).  All rights reserved.
Portions Copyright (c) 1996-2002 Transvirtual Technologies, Inc.

The Kaffe virtual machine is free software, licensed under the terms of
the GNU General Public License.  Kaffe.org is a an independent, free software
community project, not directly affiliated with Transvirtual Technologies,
Inc.  Kaffe is a Trademark of Transvirtual Technologies, Inc.  Kaffe comes
with ABSOLUTELY NO WARRANTY.

Engine: Just-in-time v3   Version: 1.1.5   Java Version: 1.1

Only the link within the /etc/alternatives got changed just as we expected. When looking at the java version we see that it’s using the Kaffe Virtual Machine, great!

It gets even better, look at the man page of java. The NAME section now reads “kaffe - a virtual machine to execute Java(tm) bytecode”. So it not only changed the binary executable but also the man page. You might wonder how it knows this, if you paid attention to the output of –display you might have noticed the lines starting with slave.

$ /usr/sbin/update-alternatives --display java
java - status is manual.
link currently points to /usr/lib/kaffe/bin/java
/usr/lib/kaffe/bin/java - priority 300
slave java.1.gz: /usr/share/man/man1/java.kaffe.1.gz
/usr/lib/j2re1.5-sun/bin/java - priority 315
slave java.1.gz: /usr/lib/j2re1.5-sun/man/man1/java.1.gz
Current `best' version is /usr/lib/j2re1.5-sun/bin/java.

With update-alternatives it’s possible to create a group of files which all have a relation with each other, just like our java.kaffe manpages having a relation with the kaffe java executable. This is done with slaves, looking at the –display output you can see that java.1.gz is a slave to the master file /usr/lib/kaffe/bin/java. So when the master file is changed so is the slave file.

You can also setup your own alternatives, let’s do this with our second problem which is having “different ruby interpeters”.

Pages: 1 2

Bookmark on del.icio.us

Comments are closed.