Managing an Ubuntu server I found this tool that lets me manage program versions. Let’s say you have to work with some old programs and you need to use gcc version 5.5.0, but you type gcc --version and it says 10.3.0, it would be great if you could do something that lets you use the older version as gcc instead of gcc-5.5. Here is how you do it.

Update-alternatives

Update-alternatives is a great tool to set versions for different versions of the same program such as compilers or libraries. We could compare it to Environment modules, but the main advantage of update-alternatives is that the learning curve is not as steep. The main advantage of Environment modules is that it doesn’t require superuser permissions to configure and I believe it’s a better solution for a server where multiple users need different versions. For our use case, update-alternatives is perfectly fine. This is a Debian based tool and I know it’s in RedHat based distros but I don’t think it is a thing for Arch.

I’ll give a brief introduction to this tool.

Setting it up

To add a version you do:

sudo update-alternatives --install <link> <name> <path> <priority>

Here are to core concepts:

  • Link: This is the link where it will create a symlink to the correct version.
  • Name: The name of the tool (gcc, cuda, python…).
  • Path: The path to the version in question. This could be an executable as in gcc or a directory as with cuda.
  • Priority: A number. The higher the number the more it’ll want to be set to that for the automatic setting. Usually the version or a multiple of it is the best way to set it.

One cool trick is to set slaves with:

sudo update-alternatives --install <link> <name> <path> <priority> --slave <slave_link> <slave_path>

When you set the configuration for the alternative the slave will also be set. Examples where this could be useful are python and its corresponding pip version or java and its multiple executables.

Changing version

Once it’s configures you can change your versions with:

sudo update-alternatives --config <name>

It will prompt a menu for you to select the desired version.

Example

Ok, so let’s say you installed three different versions of gcc and g++. You want to be able to swap between them easily. You also want them to be synced, why would you need gcc and g++ to have different versions at the same time? Here is how you would configure it.

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 --slave /usr/bin/g++ g++ /usr/bin/g++-10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 --slave /usr/bin/g++ g++ /usr/bin/g++-9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 80 --slave /usr/bin/g++ g++ /usr/bin/g++-8

After that, you could change between them using.

sudo update-alternatives --config gcc

And you would get a menu asking you the version you need.