146

What is the command update-alternatives used for?

Take this example:

sudo update-alternatives --install /usr/bin/java java /usr/local/java/jre1.7.0_09/bin/java 1

What does it do? How is it different from adding jdk to the path?

Let's say that I have run the command. How would I revert back to the original state?

1
  • 2
    You can also use sudo update-alternatives --config java and javac and javaws to choose between installed versions.
    – Shayan
    Commented Feb 28, 2018 at 15:04

4 Answers 4

113

It updates the links in /etc/alternatives to point to the program for this purpose. There's lots of examples, like x-www-browser, editor, etc. that will link to the browser or editor of your preference. Some scripts or system tools may want you to edit a file manually (e.g. configuration conflict in dpkg) and they'll look into the alternatives to give you the editor of choice. For java, this is the Java runtime environment - Oracle's, OpenJRE, etc.

The links in /etc/alternatives are just symbolic links. You can see them using for example

ls -l /etc/alternatives

Moreover, the regular /usr/bin binaries are also symlinks. E.g.:

ls -l /usr/bin/java
  lrwxrwxrwx 1 root root 22 Aug 14 10:33 /usr/bin/java -> /etc/alternatives/java
ls -l /etc/alternatives/java
  lrwxrwxrwx 1 root root 46 Aug 14 10:33 /etc/alternatives/java -> /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java

So, no PATH has to be modified. It just uses symbolic links.

4
  • So in other words I can type in a single command and it will prompt me for choices. That is the only added advantage over setting a path right. Also lets say I have a different vendor of java also installed How would I update java to point to both the two different vendors? Commented Dec 27, 2012 at 11:33
  • You can't point to both like you describe. The whole purpose of it is to have a single pointer to the implementation of the entry as in the example given. Without questions. And any of the candidates should work - it should just be a preference of the administrator. And again: it has nothing to do with PATHs.
    – gertvdijk
    Commented Dec 27, 2012 at 11:43
  • 1
    Okay. That clears things up. I know I am coming back to paths but I just want to understand the difference between them . In case of a path we have to explicitly specify the location but in this case it just sym links so that when we type java it searches /etc/alternatives first. Or am i wrong in making this assumption? Commented Dec 27, 2012 at 12:11
  • I'm trying to find out if this will change the version "system-wide". I am about 90% confident having read this the answer is yes. Meaning that if you update the symlink using update-alternatives this will affect the whole system, including processes that run as root, parts of the Operating System itself, etc. My thoughts are that perhaps this is a way to break your system. For example if there is some part of the OS which depends on python3 and you update the alternative to point to a Python 2 version, isn't this likely to break something? Commented Apr 11 at 8:43
6

While @gertvdjik's answer is a good explanation of how alternatives work at the lower level, it doesn't explicitly say how to revert the original state.

I find it easier to use the corresponding GUI galternatives which is available as a package. To install it, just run:

sudo apt-get install galternatives

Then managing alternatives becomes much easier. For java in particular, you have a lot of auxiliary binaries which you'll have to update and it's faster to overview them in the GUI.

2

update-alternatives creates, removes, maintains and displays information about the symbolic links comprising the Debian alternatives system.

...

The generic name is not a direct symbolic link to the selected alternative. Instead, it is a symbolic link to a name in the alternatives directory, which in turn is a symbolic link to the actual file referenced. This is done so that the system administrator's changes can be confined within the /etc directory ...

Source and more details: man update-alternatives.

Aside: This master/slave terminology may be updated at some point.

As an example on Ubuntu 22.04, I can switch which Java I am using with sudo update-alternatives --config java:

$ 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-11-openjdk-amd64/bin/java      1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode
* 2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

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

So to answer the original questions:

  • update-alternatives updates symbolic links
  • from context, it looks like you meant PATH, however it does not change PATH or JDK, e.g. from Java 11 to Java 8 directly
  • it has nothing like history built in, so in the general case it's not possible to revert back to an original state. I'd expect you need to pick the most relevant alternative for the system state that works for you (in practice there's a good chance that you only have two alternatives when you first encounter this, so if you recently installed a second alternative that broke something for you, there's only one other to switch back to)
1

And update-alternatives can create a group to switch easily. For example javaw, java and javac needs to go together if java switches. Then javaw and javac should be installed as slaves of java. Or oc and kubectl (openshift client and kubernetes control) should go together, too. I have installed oc v3 and v4, and each version goes with its own kubectl. So I do:

sudo update-alternatives --install /usr/bin/oc oc /opt/oc3/oc 2 --slave /usr/bin/kubectl kubectl /opt/oc3/kubectl
sudo update-alternatives --install /usr/bin/oc oc /opt/oc4/oc 1 --slave /usr/bin/kubectl kubectl /opt/oc4/kubectl

So oc3 and its kubectl are prioritized.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .