Linux 下使用 update-alternatives 切换软件默认版本

教程 666 字 约 3 分钟

update-alternatives 是管理符号链接的工具,在 Ubuntu 和 Debian 中可用。通过它,我们可以方便地切换同一命令使用的软件版本。比如,系统中同时安装了多个 gcc 版本(比如 gcc、riscv64-linux-gnu-gcc 和 aarch64-linux-gnu-gcc),对于不同项目需要使用不同的版本。

原理

update-alternatives 通过软链接来处理 Linux 中多版本切换问题,软链接存储于 /etc/alternatives 目录。

比如,cc 命令(/usr/bin/cc)其实是一个软链接,它指向 /etc/alternatives/cc ,而这也是一个软链接,指向 /usr/bin/gcc ,而这才是最终的可执行文件 。

注册版本

格式:sudo update-alternatives --install <链接> <名称> <路径> <优先级>`
<链接> 即为命令所在的路径
<名称> 为命令的名称
<路径> 为最终的可执行文件的路径
<优先级> 为正整数,数字越大,优先级越高

举个例子:

# 注册 riscv64-linux-gnu-gcc 版本
$ sudo update-alternatives --install /usr/bin/cc cc /usr/bin/riscv64-linux-gnu-gcc 19

# 注册 aarch64-linux-gnu-gcc 版本
$ sudo update-alternatives --install /usr/bin/cc cc /usr/bin/aarch64-linux-gnu-gcc 18

切换版本

格式:sudo update-alternatives --config <名称>

举个例子:

  • cc 命令的默认版本由 gcc 切换到 riscv64-linux-gnu-gcc

    $ sudo update-alternatives --config cc
    There are 3 choices for the alternative cc (providing /usr/bin/cc).
    
      Selection    Path                            Priority   Status
    ------------------------------------------------------------
    * 0            /usr/bin/gcc                     20        auto mode
      1            /usr/bin/aarch64-linux-gnu-gcc   18        manual mode
      2            /usr/bin/gcc                     20        manual mode
      3            /usr/bin/riscv64-linux-gnu-gcc   19        manual mode
    
    Press <enter> to keep the current choice[*], or type selection number: 3
    update-alternatives: using /usr/bin/riscv64-linux-gnu-gcc to provide /usr/bin/cc (cc) in manual mode
  • python3 命令的默认版本由 python3.10 切换到 python3.11

    $ sudo update-alternatives --config python3
    There are 2 choices for the alternative python3 (providing /usr/bin/python3).
    
      Selection    Path                 Priority   Status
    ------------------------------------------------------------
    * 0            /usr/bin/python3.10   2         auto mode
      1            /usr/bin/python3.10   2         manual mode
      2            /usr/bin/python3.11   1         manual mode
    
    Press <enter> to keep the current choice[*], or type selection number: 2
    update-alternatives: using /usr/bin/python3.11 to provide /usr/bin/python3 (python3) in manual mode

移除指定版本

格式:sudo update-alternatives --remove <名称> <路径>

举个例子:

# 移除 riscv64-linux-gnu-gcc 版本
$ sudo update-alternatives --remove cc /usr/bin/riscv64-linux-gnu-gcc

列出可用版本

格式:sudo update-alternatives --list <名称>

举个例子:

# 列出 cc 替换组中可用版本
$ sudo update-alternatives --list cc
/usr/bin/aarch64-linux-gnu-gcc
/usr/bin/gcc
/usr/bin/riscv64-linux-gnu-gcc

查看替换组信息

格式:sudo update-alternatives --display <名称>

举个例子:

  • 查看 cc 替换组信息

    $ sudo update-alternatives --display cc
    cc - auto mode
      link best version is /usr/bin/gcc
      link currently points to /usr/bin/gcc
      link cc is /usr/bin/cc
      slave cc.1.gz is /usr/share/man/man1/cc.1.gz
    /usr/bin/aarch64-linux-gnu-gcc - priority 18
    /usr/bin/gcc - priority 20
      slave cc.1.gz: /usr/share/man/man1/gcc.1.gz
    /usr/bin/riscv64-linux-gnu-gcc - priority 19
  • 查看 python3 替换组信息

    $ sudo update-alternatives --display python3
    python3 - manual mode
      link best version is /usr/bin/python3.10
      link currently points to /usr/bin/python3.11
      link python3 is /usr/bin/python3
    /usr/bin/python3.10 - priority 2
    /usr/bin/python3.11 - priority 1

设置默认版本

格式:sudo update-alternatives --set <名称> <链接>

举个例子:

# 设置默认版本为 gcc
$ sudo update-alternatives --set cc /usr/bin/gcc

# 设置默认版本为 riscv64-linux-gnu-gcc
$ sudo update-alternatives --set cc /usr/bin/riscv64-linux-gnu-gcc
Linux 下使用 update-alternatives 切换软件默认版本
https://blog.iamsjy.com/2024/07/19/update-alternatives/
本文作者
Tony
发布日期
2024-07-19
更新日期
2025-08-16
转载或引用本文时请遵守 CC BY-NC-SA 4.0 许可协议,注明出处、不得用于商业用途!

评论