How to switch between multiple GCC and G++ compiler versions on Ubuntu 20.04 LTS Focal Fossa

In this tutorial we will install multiple versions of GCC and G++ compilers using the apt install command. Furthermore, by use of the update-alternatives tool you will learn how to easily switch between multiple GCC and G++ compiler versions and how to check the currently selected compiler version.

In this tutorial you will learn:

  • How to install multiple GCC and G++ compiler versions
  • How to create alternative compiler version list
  • How to switch between multiple compiler versions

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Installed or upgraded Ubuntu 20.04 Focal Fossa
Software GCC
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Installing GCC the C compiler on Ubuntu 20.04 step by step instructions

  1. Install multiple C and C++ compiler versions:
    $ sudo apt install build-essential
    $ sudo apt -y install gcc-7 g++-7 gcc-8 g++-8 gcc-9 g++-9
    


  2. Use the update-alternatives tool to create list of multiple GCC and G++ compiler alternatives:
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 7
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 7
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 8
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 8
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 9
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 9
    
  3. Check the available C and C++ compilers list on your Ubuntu 20.04 system and select desired version by entering relevant selection number:
    $ sudo update-alternatives --config gcc
    There are 3 choices for the alternative gcc (providing /usr/bin/gcc).
    
      Selection    Path            Priority   Status
    ------------------------------------------------------------
      0            /usr/bin/gcc-9   9         auto mode
      1            /usr/bin/gcc-7   7         manual mode
    * 2            /usr/bin/gcc-8   8         manual mode
      3            /usr/bin/gcc-9   9         manual mode
    Press  to keep the current choice[*], or type selection number: 
    

    For C++ compiler execute:

    $ sudo update-alternatives --config g++
    There are 3 choices for the alternative g++ (providing /usr/bin/g++).
    
      Selection    Path            Priority   Status
    ------------------------------------------------------------
    * 0            /usr/bin/g++-9   9         auto mode
      1            /usr/bin/g++-7   7         manual mode
      2            /usr/bin/g++-8   8         manual mode
      3            /usr/bin/g++-9   9         manual mode
    
    Press  to keep the current choice[*], or type selection number:
    
  4. Each time after switch check your currently selected compiler version:
    $ gcc --version
    $ g++ --version