How to install multiple versions of GCC and G++ on Ubuntu 20.04

Now that you have your latest Ubuntu, how to switch between various G++ and GCC versions? Let's first show you installing both the compilers followed by the method to switch between them.

As a C and C++ developer, it is not uncommon to handle projects that require different versions of compilers. With advancements in technology, it’s likely to find yourself in a situation where you need a particular GCC or G++ compiler version for a specific project.

In this tutorial, we will show you the method to install multiple versions of GCC and G++ on Ubuntu 20.04 using the apt install command. Additionally, we will show you how to switch between various G++ and GCC versions using the update-alternatives and confirm the selected version currently in use.

To ensure you don’t miss a bit, we will divide this tutorial into three steps, as shown below.

  • Install multiple versions of GCC and G++ compilers.
  • Create an alternative compiler version list.
  • Switch between multiple GCC and G++ compilers.

Before you proceed

Our Linux distribution of choice in this tutorial is the recently released Ubuntu 20.04 LTS. Ensure that all packages are up to date using the commands below.

sudo apt update
sudo apt upgrade

You will also need elevated privileges by executing commands using the sudo command.

Installing multiple GCC compiler versions on Ubuntu 20.04 LTS

We will use the apt install command. Execute the commands below using the sudo privileges.

sudo apt install build-essential
sudo apt -y install gcc-7 gcc-8

install gcc-7 gcc-8.
installing GCC-7 GCC-8

We will also install GCC 9.0 using the commands below.

sudo add-apt-repository ppa:jonathonf/gcc-9.0

install GCC -9 repository
install GCC -9 repository

sudo apt-get install gcc-9

Install GCC-9 on Ubuntu 20.04.
Install GCC-9 on Ubuntu 20.04.

Installing multiple G++ compilers on Ubuntu 20.04 LTS

With GCC compilers installed, we can now proceed to install multiple versions of G++ compilers.

sudo apt install build-essential 
sudo apt -y install g++-7 g++-8 g++-9

Install G++ Compilers
Install G++ Compilers

Alternatively, you can install both GCC and G++ compilers with a single command, as shown below.

sudo apt install build-essential
sudo apt -y install gcc-7 g++-7 gcc-8 g++-8 gcc-9 g++-9

Creating a list of GCC and G++ alternatives with update-alternatives

Step 1. Launch the Terminal and execute the commands below one by one.

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

Create a List of GCC and G++ aternatives
Create a List of GCC and G++ alternatives

Step 2. Check the GCC and G++ lists available on your system and select the default compiler you wish to use for your development. Let’s start by setting up the GCC compiler.

Launch your Ubuntu Terminal and execute the command below.

sudo update-alternatives --config gcc

Set your default GCC compiler.
Set your default GCC compiler.

Select the GCC compiler you wish to use by selecting the number under the selections menu. In this tutorial, I chose option two(2) since I wanted to use GCC-8 as my compiler for my current project.

Step 3. Now let’s do the same and choose our default G++ compiler. Execute the command below.

sudo update-alternatives --config g++

Select the Default G++ Compiler
Select the Default G++ Compiler

Here, I am also interested in using G++-8, and therefore, I will select option two(2), as shown in the image above.

To switch to another GCC or G++ compiler, repeat the config process in steps two(2) and three(3) above. Select a different option to set another Compiler version that you wish to use.

To affirm the changes we have made running the version command on the Terminal for each compiler.

gcc --version
g++ --version

Run the G++ and GCC version command.
Run the G++ and GCC version command.

From the image above, we can see that the system shows the GCC and G++ compilers that we configured to use as defaults.

That’s all about how you can install multiple versions of GCC and G++ compilers and switch between each of them on your Ubuntu machine. If you have additional information, feel free to share it with our readers in the comment section below.

Arun Kumar
Arun did his bachelor in computer engineering and loves enjoying his spare time writing for FOSS Linux. He uses Fedora as the daily driver and loves tinkering with interesting distros on VirtualBox. He works during the day and reads anything tech at night. Apart from blogging, he loves swimming and playing tennis.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,281FansLike
386FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.

5 Best Application Launchers for Ubuntu

Ubuntu is one of the most used Linux distributions worldwide. It is also the reason why it has the maximum number of available programs for itself. Today we are going to talk about one category of those programs, the application launchers.

5 Best Download Managers for Linux

We often need to download large files that can go corrupt due to various reasons such as slow internet or interrupted download. Using a broken downloaded file is not something one wants. Download managers make sure that the downloaded file maintains its integrity and also presents you with the ability to pause and resume downloads, provided the server supports it. When you are downloading a massive file, it's recommended to use a download manager.

The 5 Best Free and Open-Source PDF Editors

It is often the case that professionals prefer using the Portable Document Format (PDF) for all their official documents, whether it be school certificates, job letters, or important announcements.

Installing multiple Linux distros on a USB drive

A bootable Linux USB drive, which also referred to as a Live USB drive, is a drive containing files that enable a computer to boot into a specific Linux distro. It is most commonly used to test-drive various Linux distros before installing on the computer for use as a daily driver.