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.
Videos
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.
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.
Assuming one has installed a JDK in /opt/java/jdk1.8.0_144 then:
Install the alternative for javac
$ sudo update-alternatives --install /usr/bin/javac javac /opt/java/jdk1.8.0_144/bin/javac 1Check / update the alternatives config:
$ sudo update-alternatives --config javac
If there is only a single alternative for javac you will get a message saying so, otherwise select the option for the new JDK.
To check everything is setup correctly then:
$ which javac
/usr/bin/javac
$ ls -l /usr/bin/javac
lrwxrwxrwx 1 root root 23 Sep 4 17:10 /usr/bin/javac -> /etc/alternatives/javac
$ ls -l /etc/alternatives/javac
lrwxrwxrwx 1 root root 32 Sep 4 17:10 /etc/alternatives/javac -> /opt/java/jdk1.8.0_144/bin/javac
And finally
$ javac -version
javac 1.8.0_144
Repeat for java, keytool, jar, etc as needed.
You will notice a big change when selecting options if you type in "java -version" after doing so. So if you run update-alternatives --config java and select option 3, you will be using the Sun implementation.
Also, with regards to auto vs manual mode, making a selection should take it out of auto mode per this page stating:
When using the
--configoption, alternatives will list all of the choices for the link group of which given name is the master link. You will then be prompted for which of the choices to use for the link group. Once you make a change, the link group will no longer be inauto mode. You will need to use the--autooption in order to return to the automatic state.
And I believe auto mode is set when you install the first/only JRE/JDK.
(tl;dr at bottom)
Apologies for sounding dumb, but I'm trying to understand Ubuntu's update-alternatives by reading the man pages and I just can't seem to understand it.
I'm trying to set atom (/usr/bin/atom) as an alternative for editor. There are two ways the man pages seem to be telling me to do it, but I get an error on both that I don't totally understand:
$ sudo update-alternatives --set editor /usr/bin/atom update-alternatives: error: alternative /usr/bin/atom for editor not registered; not setting
There's also update-alternatives --install <link> <name> <path> <priority>, but I can't figure out what it wants for <link>. For all other alternatives it seems to just be a symlink to <path>, which is a bit strange, but I tried creating a symlink and I got an error Okay, now I'm not getting the same error, but it was something like "Max recursion reached". Now I can set the editor alternative to atom, but nothing appears to be different when I try to open a text document.
I feel like I'm just misunderstanding the arguments... could I get some help, please?
TL;DR I want to set /usr/bin/atom as the editor alternative, how do I do it
Basically it says to your machine to use this alternative of Jave JDK instead of the default one, which, in Linux systems, is OpenJDK.
A brief extract from the man page is better than any answer I could write:
update-alternatives creates, removes, maintains and displays information about the symbolic links comprising the Debian alternatives system.
It is possible for several programs fulfilling the same or similar functions to be installed on a single system at the same time. For example, many systems have several text editors installed at once. This gives choice to the users of a system, allowing each to use a different editor, if desired, but makes it difficult for a program to make a good choice for an editor to invoke if the user has not specified a particular preference.
Debian's alternatives system aims to solve this problem. A generic name in the filesystem is shared by all files providing interchangeable functionality. The alternatives system and the system administrator together determine which actual file is referenced by this generic name. For example, if the text editors ed(1) and nvi(1) are both installed on the system, the alternatives system will cause the generic name /usr/bin/editor to refer to /usr/bin/nvi by default. The system administrator can override this and cause it to refer to /usr/bin/ed instead, and the alternatives system will not alter this setting until explicitly requested to do so.
With --install you specified a link, "/usr/bin/java" a name "java" and a path "/usr/lib/java/JDK...." and you add a group of alternatives to the system. link is the generic name for the master link, name is the name of its symlink in the alternatives directory, and path is the alternative being introduced for the master link.
I hope to be clear enough, Here there is a post regarding java alternatives.
For the complete usage list I suggest to look at the same manual page, typing man update-alternatives on your OS shell;
i want to explain it for you from scratch ... if you have 2 or more versions of a program that starts with a same command in linux you can use update-alternatives command for determining the default version of that program and you can also change the default version of that command easily .. assume that you have 2 version of python on your system. python 2 & python 3. by default python command on linux starts the python2 interpreter on the shell . you think that python 2 is old and you want to use python 3 instead of python2 . one of the solution is that you enter python3 command on the shell and start the python interpreter that its version is more than 3 ... but you want to enter python command and shell recognize that you want to run python3 interpreter . here,update-alternatives command do it's work and determine the default version and versions of a program you want to use for a specific command .
i told you that python command start python 2 interpreter by default . you want to change it . follow the example :
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1.
when you execute this command on linux you are telling to shell that after this time two links of two different programs are linked to python command on linux .. infact you install the python3 program on python command on linux and two programs exist at 1 command that you can change the defaults of these programs and run what of these programs you want by changing priority in command or configure it manually to set the default program ..
hope this is useful for you !