Detailed explanation of Ubuntu update alternates command

This command is equivalent to the method of selecting file opening (or associated file type) in windows. For example, to edit a file, Notepad, Notepad + +, EDITPLUS and so on can be used. Under Ubuntu, update alternates is used to manage the file. Its principle is to establish a soft connection for each managed program in the/etc/alternates directory, Man, he can find out why it’s designed this way

time is precious, so don’t talk nonsense. Turn several articles and read them together:

Article 1

If you have installed different versions of the same software, you can use the update alternates command to set which version to use by default. For example, if you have installed gcc-4.6 and gcc-4.8 in Ubuntu 12.04, you want the GCC command to use version 4.8 automatically


After installing G + + – 4.8, set it as the default. GCC is the same as
in the same way

# First let the system know that we have multiple versions of g++ installed
# The 20 and 50 at the end of the command are the priority levels, if you use auto select mode, the system will default to the higher priority
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.6 20
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 50
# Use the command interactively to select the default version to use
sudo update-alternatives --config g++
# Other commands.
# Query which versions are installed on the system
sudo update-alternatives --query g++

 

 

Article 2:
jdk installation

Move the downloaded jdk-7u11-linux-i586.tar.gz archive binary to the directory you want to install.

Open a Terminal, the the command to unpack the tarball and install the JDK:

$tar zxvf jdk-7u11-linux-i586.tar.gz

For example, if you download the .tar.gz is /home/you, your installed directory will be /home/you/jdk1.7.0_11.

Then, you have to update alternatives for javac and java:

$sudo update-alternatives –install /usr/bin/javac javac /home/you/jdk1.7.0_11/bin/javac 1

$sudo update-alternatives –install /usr/bin/java java /home/you/jdk1.7.0_11/bin/java 1

If you have more than one javac installed, you have to config your javac alternative

$sudo update-alternatives –config javac

$sudo update-alternatives –config java

Finally, delete the downloaded .tar.gz after then.

Cyper battle.
Although I installed jdk, set the environment variables, and also used a long time, but since the study of alternatives, practice a hand, the process is as follows.

cyper@zerorun:~$ which java
/opt/jdk/jdk1.7.0_45/bin/java
cyper@zerorun:~$ update-alternatives --config java 
update-alternatives: error: no alternatives for java.
cyper@zerorun:~$ sudo update-alternatives --install /usr/bin/java java `which java` 1 
[sudo] password for cyper: 
update-alternatives: using /opt/jdk/jdk1.7.0_45/bin/java to provide /usr/bin/java (java) in auto mode.
cyper@zerorun:~$ update-alternatives --config java 
There is only one alternative in link group java: /opt/jdk/jdk1.7.0_45/bin/java
Nothing to configure.
cyper@zerorun:~$ sudo update-alternatives --install /usr/bin/javac javac `which javac` 1 
update-alternatives: using /opt/jdk/jdk1.7.0_45/bin/javac to provide /usr/bin/javac (javac) in auto mode.
cyper@zerorun:~$ ls /usr/bin/java* 
/usr/bin/java /usr/bin/javac
cyper@zerorun:~$ ls -l /usr/bin/java*
lrwxrwxrwx 1 root root 22 Dec 20 23:07 /usr/bin/java -> /etc/alternatives/java
lrwxrwxrwx 1 root root 23 Dec 20 23:07 /usr/bin/javac -> /etc/alternatives/javac
cyper@zerorun:~$ ls -l /etc/alternatives/java* 
lrwxrwxrwx 1 root root 29 Dec 20 23:07 /etc/alternatives/java -> /opt/jdk/jdk1.7.0_45/bin/java
lrwxrwxrwx 1 root root 30 Dec 20 23:07 /etc/alternatives/javac -> /opt/jdk/jdk1.7.0_45/bin/javac
cyper@zerorun:~$ which java
/opt/jdk/jdk1.7.0_45/bin/java
cyper@zerorun:~$ sudo update-alternatives --remove java `which java` 
cyper@zerorun:~$ ls -l /usr/bin/java 
ls: cannot access /usr/bin/java: No such file or directory
cyper@zerorun:~$ sudo update-alternatives --remove-all java 
update-alternatives: error: no alternatives for java.
cyper@zerorun:~$ which java
/opt/jdk/jdk1.7.0_45/bin/java
cyper@zerorun:~$ sudo update-alternatives --set java `which java` 
update-alternatives: error: no alternatives for java. 
cyper@zerorun:~$ sudo update-alternatives --install /usr/bin/java java `which java` 1 
update-alternatives: using /opt/jdk/jdk1.7.0_45/bin/java to provide /usr/bin/java (java) in auto mode.
cyper@zerorun:~$ sudo update-alternatives --set java `which java` 

Note: the format of install is as follows:

Sudo update alternates — install link name path priority, very easy to understand

 

‘which Java’ in the command line is

/opt/jdk/jdk1.7.0_ 45/bin/Java, too lazy to write the full path.

 

As you can see, for Java commands, he did the following:

1) create a new symbolic link/usr/bin/Java, pointing to/etc/alternatives/Java

2) and this second file is also a symbolic link, pointing to/opt/JDK/* * * *, your actual JDK installation directory.

 

Because I only have one version of JDK installed, it’s useless to use sudo update alternates — config Java

Last play — query, – set

cyper@zerorun:~$ sudo update-alternatives --query java 
Link: java
Status: manual
Best: /opt/jdk/jdk1.7.0_45/bin/java
Value: /opt/jdk/jdk1.7.0_45/bin/java

Alternative: /opt/jdk/jdk1.7.0_45/bin/java
Priority: 1
cyper@zerorun:~$ sudo update-alternatives --query editor 
Link: editor
Status: auto
Best: /bin/nano
Value: /bin/nano

Alternative: /bin/ed
Priority: -100
Slaves:
 editor.1.gz /usr/share/man/man1/ed.1.gz

A cyper@zerorun:~$ sudo update-alternatives --config editor 
There are 4 choices for the alternative editor (providing /usr/bin/editor).

 Selection Path Priority Status
------------------------------------------------------------
* 0 /bin/nano 40 auto mode
 1 /bin/ed -100 manual mode
 2 /bin/nano 40 manual mode
 3 /usr/bin/vim.basic 30 manual mode
 4 /usr/bin/vim.tiny 10 manual mode

Press enter to keep the current choice[*], or type selection number: ^Ccyper@zerorun:~$ ^C
cyper@zerorun:~$ sudo update-alternatives --set editor 2 
update-alternatives: error: alternative 2 for editor not registered, not setting.
cyper@zerorun:~$ sudo update-alternatives --set editor /usr/bin/vim.basic 
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in manual mode.
cyper@zerorun:~$ sudo update-alternatives --config editor 
There are 4 choices for the alternative editor (providing /usr/bin/editor).

 Selection Path Priority Status
------------------------------------------------------------
 0 /bin/nano 40 auto mode
 1 /bin/ed -100 manual mode
 2 /bin/nano 40 manual mode
* 3 /usr/bin/vim.basic 30 manual mode
 4 /usr/bin/vim.tiny 10 manual mode

Similar Posts: