Understanding the Command: update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 60
The update-alternatives utility is a standard tool in Debian-based Linux distributions (like Ubuntu) that manages multiple versions of programs providing the same functionality. It uses symbolic links and priority values to determine which version should be used by default when a command is invoked.
Consider the following command:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 60
This registers GCC version 13 as an available opsion for the gcc command. Below is a breakdown of each component:
--install
This subcommand adds a new alternative to the system. It requires four arguments: the link path, the group name, the actual binary path, and a priority value.
/usr/bin/gcc
This is the primary symlink that users interact with when running gcc. The update-alternatives system controls what this symlink points to based on configured alternatives and their priorities.
gcc
This is the name of the alternatives group. All registered versions of the GCC compiler are grouped under this identifier, allowinng them to be managed collectively.
/usr/bin/gcc-13
This specifies the full path to the actual executable—in this case, the GCC 13 compiler binary. This version will be linked to /usr/bin/gcc if selected by the alternatives system.
60
This integer represents the priority of this alternative. When multiple versions exist, the one with the highest priority is automatically selected as the default. For instance, if GCC 10 has priority 40 and GCC 13 has priority 60, the latter becomes the default.
After running the command, the system recognizes /usr/bin/gcc-13 as a valid candidate for the gcc command. If no other alternative has a higher priority, the symlink /usr/bin/gcc will point to /usr/bin/gcc-13.
To manually switch between installed versions, use:
sudo update-alternatives --config gcc
This presents an interactive menu listing all registered GCC versions, allowing you to select your preferred default.