How to switch between multiple GCC and G++ compiler versions on Ubuntu 22.04 LTS Jammy Jellyfish

The GCC compiler is used to compile C programs on a Linux system and the G++ compiler is used to compile C++ programs. Both have numerous versions available for installation on Ubuntu 22.04 Jammy Jellyfish.

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 on Ubuntu 22.04 Jammy Jellyfish.

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 on Ubuntu 22.04
Switching between multiple compiler versions on Ubuntu 22.04 Jammy Jellyfish Linux
Switching between multiple compiler versions on Ubuntu 22.04 Jammy Jellyfish Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu 22.04 Jammy Jellyfish
Software GCC and G++
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 and G++ compilers on Ubuntu 22.04 step by step instructions



Twitter icon Follow LinuxConfig.org on Twitter for the latest tips and tricks about Linux!

  1. Start by opening a command line terminal and using the following apt commands to install a few different versions of the GCC and G++ compilers on Ubuntu 22.04.
    $ sudo apt update
    $ sudo apt install build-essential
    $ sudo apt -y install gcc-8 g++-8 gcc-9 g++-9 gcc-10 g++-10
    
  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-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
    $ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 10
    $ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 10
    
  3. Check the available C and C++ compilers list on your Ubuntu 22.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-10  10         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++-10  10         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
    

Closing Thoughts




In this tutorial, you learned how to install and switch between multiple versions of the GCC and G++ compiler on Ubuntu 22.04 Jammy Jellyfish Linux. This gives you the ability to compile your C and C++ programs with different versions of the compilers by just executing a few commands each time you need to switch.