Selecting compiler versions with update-alternatives

less than 1 minute read

Switch between compilers e.g. g++-7 and g++-8 with simple commands as follows:

  1. (one-time) Setup your Bash shell to use ~/.local/bin instead of system-wide /usr
    mkdir ~/.local/bin

    Edit ~/.bashrc, adding the line:

    export PATH="$HOME/.local/bin:$PATH"
  2. (one-time setup) enable switching
    update-alternatives --install $HOME/.local/bin/g++ g++ /usr/bin/g++-7 20
    update-alternatives --install $HOME/.local/bin/g++ g++ /usr/bin/g++-6 10

    and so on for gcc and gfortran

  3. At any time, switch compiler versions:
    update-alternatives --config g++
    update-alternatives --config gcc
    update-alternatives --config gfortran

update-alternatives works with virtually any program including Java and Python.

Compiler version priority order

The last number of update-alternatives --install is priority. The highest priority number is used in “automatic” update-alternatives mode.

Leave a Comment