最近经常使用编译,偶尔换环境,软件版本之间更换一般都是重装或者手动更换软连接,比较麻烦,查资料时偶然发现的这个工具,管理起来比较方便,记录备忘下
update-alternatives 命令用于处理linux系统中软件版本的切换,在各个linux发行版中均提供了该命令,命令参数略有区别,但大致是一样的。
使用方法
## 以gcc为例
# 1.查看当前版本
gcc -v # 查看当前版本
which gcc # 查看命令位置
ls -l /usr/bin/gcc # 可以看到是一个软连接
# 2.安装其他版本gcc
sudo apt install gcc-8 gcc-8-multilib g++-8 g++-8-multilib # 安装gcc-8
sudo apt install gcc-10 gcc-10-multilib g++-10 g++-10-multilib # 安装gcc-10
ll /usr/bin/gcc* # 查看所有gcc
# 3.在update-alternatives中注册gcc
# 注册 # --install注册 目标软连接 注册名称 软连接目标 优先级(数字越大优先级越高) --slave从属(同版本其他命令) ...与前面类似
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 10 --slave /usr/bin/g++ g++ /usr/bin/g++-9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 20 --slave /usr/bin/g++ g++ /usr/bin/g++-10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 30 --slave /usr/bin/g++ g++ /usr/bin/g++-8
# 4.查看已注册版本(爱看不看)
update-alternatives --display gcc
# 5.更换软件版本
sudo update-alternatives --config gcc
# 第一行 0 为自动模式,自动选择优先级最高的版本
# 下面的行都是手动模式,选择后若不手动更换不会自动更换版本
# 6.验证
gcc -v # 可以看到版本已经更换
ll /usr/bin/gcc # 软连接目标更换为了update-alternatives
# 方法对其它软件通用,其它使用方法看 附录Usage
附录 Usage
Usage: update-alternatives [<option> ...] <command>
Commands:
--install <link> <name> <path> <priority>
[--slave <link> <name> <path>] ...
add a group of alternatives to the system.
--remove <name> <path> remove <path> from the <name> group alternative.
--remove-all <name> remove <name> group from the alternatives system.
--auto <name> switch the master link <name> to automatic mode.
--display <name> display information about the <name> group.
--query <name> machine parseable version of --display <name>.
--list <name> display all targets of the <name> group.
--get-selections list master alternative names and their status.
--set-selections read alternative status from standard input.
--config <name> show alternatives for the <name> group and ask the
user to select which one to use.
--set <name> <path> set <path> as alternative for <name>.
--all call --config on all alternatives.
<link> is the symlink pointing to /etc/alternatives/<name>.
(e.g. /usr/bin/pager)
<name> is the master name for this link group.
(e.g. pager)
<path> is the location of one of the alternative target files.
(e.g. /usr/bin/less)
<priority> is an integer; options with higher numbers have higher priority in
automatic mode.
Options:
--altdir <directory> change the alternatives directory.
--admindir <directory> change the administrative directory.
--log <file> change the log file.
--force allow replacing files with alternative links.
--skip-auto skip prompt for alternatives correctly configured
in automatic mode (relevant for --config only)
--quiet quiet operation, minimal output.
--verbose verbose operation, more output.
--debug debug output, way more output.
--help show this help message.
--version show the version.