惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
博客园 - Franky
Apple Machine Learning Research
Apple Machine Learning Research
酷 壳 – CoolShell
酷 壳 – CoolShell
阮一峰的网络日志
阮一峰的网络日志
量子位
雷峰网
雷峰网
宝玉的分享
宝玉的分享
V
Visual Studio Blog
博客园_首页
小众软件
小众软件
The Cloudflare Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
博客园 - 聂微东
S
SegmentFault 最新的问题
博客园 - 【当耐特】
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
人人都是产品经理
人人都是产品经理
WordPress大学
WordPress大学

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Switch Command with update-alternatives on Ubuntu
2020-07-02 · via jdhao's digital space

To build different projects, we may need to install different versions of the same software. To use a specific version, we may need to create a sym link to that version of the executable.

On Ubuntu, we can also alleviate the issue of switching command versions with update-alternatives.

One of the most common tool that needs multiple versions is GCC. On Ubuntu 18.04, the default GCC version is 7.4.0. However, sometimes, we also need to use older GCC such as gcc-4.8. The GCC toolset will install several executables under /usr/bin/, such as gcc-4.8, g++-4.8, among others.

If we want the command gcc to point to /usr/bin/gcc-4.8, we often want g++ to point to /usr/bin/g++-4.8. Using symlinks to manage version switch is not convenient in this case, since we need to create two or more symlinks. If we want to switch back to GCC 7.4, we then need to remove the old symlinks and create new ones, which is cumbersome.

update-alternatives provides what is called master and slave alternatives, where if you change the master symlink, the slave symlinks will also get changed automatically. The syntax is:

update-alternatives --install link name path priority [--slave link name path]

link is the generic name used to invoke a command, for example, /usr/bin/gcc. name is the alternative name used under the alternative directory (which is usually /etc/alternatives), for example, gcc. path is the actual path to a specific version, e.g., /usr/bin/gcc-4.8. priority is a score given to this alternative. If you have several alternatives for a command, the one with the highest priority will be used. The link after --install is the master link.

The format for --slave is the same as master link. You are allowed to add multiple slave links like this:

update-alternatives --install link name path priority --slave link1 name1 path1 --slave link2 name2 path2

For GCC, we can use the following command to add two alternatives:

update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 10 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 20 --slave /usr/bin/g++ g++ /usr/bin/g++-7

So by default, GCC 7.4 will be used. In order to switch to another versions, we can run the following command:

update-alternatives --config gcc

You will be prompted to choose an alternative from a list of alternatives. After that, the gcc and g++ version will be switched to the version you want.

The alternatives work like this. /usr/bin/gcc will point to /etc/alternatives/gcc, which in turn points to the real command /usr/bin/gcc-7 or /usr/bin/gcc-4.8 depending on which alternative you choose. So when you change the alternative, you are changing the mapping from /etc/alternatives/gcc to the actual executables.

References#