Sunday 15 March 2015

Using the alternatives system (update-alternatives, /etc/alternatives)

Yes, it’s basic. However, I found it challenging to find a page with exactly the info I was looking for. So I’m recording the things I wanted in a single page here.

General Background

The alternatives system is a way for several programs that are installed on your system and that carry out the same, or similar, functions to be listed as alternatives (for each other). One of the installed programs will be designated as the default for this set of alternative programs. Examples where this may be used would be text editors or java versions.

Management of what alternatives are listed and which is set as the default is handled by the update-alternatives command. (On some systems, the alternatives command could be used.) Each of the programs in a set of alternatives is assigned a priority level. The alternative with the highest priority level determines the default chosen when running in auto mode.

The update-alternatives command creates, removes, maintains and displays information about the symbolic links that make up the alternatives system.

More details

For systems supporting the alternatives feature, symlinks are created from
/usr/bin/ to /etc/alternatives/ . This latter link is a symlink to the real program, which is determined by the priorities of the alternatives available.

On a package-based system (e.g. Debian, Ubuntu), a package creator sets the priority level. So you have to hope that the creator of a package considers the impact of the priority they set.

Downsides

One downside to the use of alternatives is probably mostly felt by old-schoolers (aka me) who really want to know what it is that is launched when we launch a program. For example, “vi”. If I type “vi”, am I getting vi or vim or something else? In olden days, I could just see if there were symlinks, if so follow them, and I’d know. Now, you’d have to access the alternatives system and puzzle it out via priorities.*

*Well, take that with a pinch of salt! That is my understanding and could be wrong. Leave a comment if it’s wrong and you know it, please. :-)

Working with alternatives

  • update-alternatives
  • alternatives
  • galternatives - a graphical system on Debian for working with the alternatives system. (I have not tried this.)


Working with alternatives - java from scratch

The notes below were from a point in time where I had just installed a new jdk (jdk1.7.o_71) on a machine. I then needed to register java in the alternatives system, and then add other java functionality into alternatives.

I cannot guarantee all the information below is strictly correct, as I’m writing this from my notes made at the time I did this, but the general pattern is as it happened. Please refer to the man page for update-alternatives for the ultimate truth.
  • Enter the new jdk into the alternatives system:

     sudo update-alternatives --install /usr/bin/java java /opt/jdk1.7.o_71/bin/java 2
    
  • If java was not already in your alternatives system, you set up a new configuration for java by running:

    sudo update-alternatives --config 
    
    If java is already there, then use --set not --config. See bullet point below. 
    
  • Include jar processing and the java compiler in the alternatives system in a similar way to the above.

    sudo update-alternatives --install /usr/bin/jar jar /opt/jdk1.7.0_71/bin/jar  2
    sudo update-alternatives --install /usr/bin/javac javac /opt/jdk1.7.0_71/bin/javac 2
    
  • Set these as the defaults in the (existing) alternatives setup using –set

    sudo alternatives --set jar /opt/jdk1.7.0_71/bin/jar
    sudo alternatives --set javac /opt/jdk1.7.0_71/bin/javac
    

For java, you may then wish to set up some environmental variables as per usual. If you were just to do this for a given session (probably not what you want to do long term though), then you could just run these commands in the case of the java discussed in this section:

export JAVA_HOME=/opt/jdk1.7.0_71/
export JRE_HOME=/opt/jdk1.7.0_71/jre
export PATH=$PATH:/opt/jdk1.7.0_71/bin:/opt/jdk1.7.0_71/jre

No comments:

Post a Comment