In this article we will install multiple versions of GCC and G++ compilers. Furthermore, by use of the update-alternatives tool you will learn how to easily switch between multiple GCC and G++ compiler versions.

In this tutorial you will learn:
  • How to install multiple GCC and G++ compiler versions
  • How to 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

install 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. SUBSCRIBE TO NEWSLETTER
    Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


  3. 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
    
  4. Check the available C and C++ compilers list 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:
    
  5. Each time after switch check you currently selected compiler version:
    $ gcc --version
    $ g++ --version
    
ARE YOU LOOKING FOR A LINUX JOB?
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed on LinuxCareers.com.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.