Configure multi-version switching with update-alternatives on Linux

When developing in a Linux environment, it is often necessary to use different versions of executable files or dynamic libraries.
Use the update-alternatives tool to allow multiple versions of binary programs or dynamic libraries to coexist and switch as needed.

Fundamental

  1. Binary program name with version number, for example: gcc-7, gcc-8.
  2. By soft link, create a name without a version number, for example: /usr/bin/gcc.
    Use a soft link to point to the actual program, for example: /usr/bin/gcc -> /usr/bin/gcc-7
  3. The default is to use the command with its name without a version number, for example:
$gcc -v
gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) 

$gcc-7 -v
gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) 
  1. The update-alternatives command can achieve the effect of multi-version coexistence and dynamic switching by modifying the connection to point to different actual programs or dynamic libraries.

command format

update-alternatives [option...] command

install command

update-alternatives --install link name path priority

link Link name without version number, for example: /usr/bin/gcc.
name update-alternatives database name, you can use this name to manage software after creation. Custom.
path The path of the actual executable program, for example: /usr/bin/gcc-7.
priority Priority. update-alternatives has 2 modes: manual (manual) and automatic (auto), the former is the version that the user specifies to use; auto mode, the program version with high priority (large value) will be used.

Example of install command:
create gcc and g++ links respectively, and set gcc-8 as high priority.

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 5

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 5
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 9

list command

Check which versions of a software are installed

$ update-alternatives --list gcc
/usr/bin/gcc-7
/usr/bin/gcc-8

display command

View the detailed information corresponding to the link

$ update-alternatives --display gcc
gcc - auto mode
  link best version is /usr/bin/gcc-8
  link currently points to /usr/bin/gcc-8
  link gcc is /usr/bin/gcc
/usr/bin/gcc-7 - priority 5
/usr/bin/gcc-8 - priority 9

query command

See the details for the link.
The difference from the display command is that the information is formatted.

$ update-alternatives --query gcc
Name: gcc
Link: /usr/bin/gcc
Status: auto
Best: /usr/bin/gcc-8
Value: /usr/bin/gcc-8

Alternative: /usr/bin/gcc-7
Priority: 5

Alternative: /usr/bin/gcc-8
Priority: 9

all command

The name is a bit odd, not the action. However, the help document man page ( reference for using the man command ) is indeed described as a command.
The function is to display and configure each link one by one.

update-alternatives --all

config command switch version

Currently gcc->gcc-7, use the config command to make gcc->gcc-8.
The config command displays a list of versions of the program that the link corresponds to, selected numerically.

$ sudo update-alternatives --config gcc
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path            Priority   Status
------------------------------------------------------------
  0            /usr/bin/gcc-8   9         auto mode
* 1            /usr/bin/gcc-7   5         manual mode
  2            /usr/bin/gcc-8   9         manual mode

Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/bin/gcc-8 to provide /usr/bin/gcc (gcc) in manual mode
$ gcc -v
gcc version 8.4.0 (Ubuntu 8.4.0-1ubuntu1~18.04) 

Next, switch to gcc-7, in order to demonstrate the effect of the auto command later.

$ sudo update-alternatives --config gcc
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path            Priority   Status
------------------------------------------------------------
  0            /usr/bin/gcc-8   9         auto mode
  1            /usr/bin/gcc-7   5         manual mode
* 2            /usr/bin/gcc-8   9         manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/gcc-7 to provide /usr/bin/gcc (gcc) in manual mode


$ gcc -v
gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) 

auto command

The result of the above command: gcc->gcc-7.
When creating a link, gcc-7 priority is 5, and gcc-8 priority is 9 (higher).
Using auto mode should automatically switch to gcc-8.

$ sudo update-alternatives --auto gcc
update-alternatives: using /usr/bin/gcc-8 to provide /usr/bin/gcc (gcc) in auto mode

$ gcc -v
gcc version 8.4.0 (Ubuntu 8.4.0-1ubuntu1~18.04) 

remove command

delete a link

$ update-alternatives --list gcc
/usr/bin/gcc-7
/usr/bin/gcc-8

$ sudo update-alternatives --remove gcc /usr/bin/gcc-7

$ update-alternatives --list gcc
/usr/bin/gcc-8

Guess you like

Origin blog.csdn.net/yinminsumeng/article/details/130201646
Recommended