Skip to content

Linux 的 update-alternatives 工具专为管理多版本程序设计。

添加两个版本:

bash
sudo update-alternatives --install /usr/bin/cmake cmake ~/opt/cmake-3.20.0/bin/cmake 10
sudo update-alternatives --install /usr/bin/cmake cmake ~/opt/cmake-3.16.3/bin/cmake 20
  • 数字为优先级,越高越优先
  • 第一次运行会自动设置默认版本为 3.16.3

切换版本:

bash
sudo update-alternatives --config cmake

输出示例:

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

  Selection    Path                                   Priority   Status
------------------------------------------------------------
* 0            /home/user/opt/cmake-3.16.3/bin/cmake   50        auto mode
  1            /home/user/opt/cmake-3.16.3/bin/cmake   50        manual mode
  2            /home/user/opt/cmake-3.20.0/bin/cmake   10        manual mode

输入编号即可切换。

验证:

bash
cmake --version
# 输出当前选中的版本

✅ 优点

  • 系统级生效,所有用户可用
  • 支持 ctestcpack 等工具自动同步
  • 命令行体验无缝切换

基于 VitePress 构建