Mead's Guide to Upgrading GCC and Clang on Linux

Last update: November 16, 2021 at 15:56:15
If you just want the required commands without having to learn anything, go to the summary.

Overview

Linux Mint 17 (including 17.1, 17.2, and 17.3) comes with GCC 4.8. To be exact, Linux Mint 17.3 comes with 4.8.4. If you need a later version of the compilers, you can either upgrade the entire distribution to Linux Mint 18.x or install newer versions of GCC alongside of the existing 4.8 version.

Upgrading Mint 17.x to 18.x is straight-forward, although I would recommend a fresh install of 18.x as sometimes the upgrade process doesn't work as seamless as it should. Installing other versions of the compiler is a little more involved, but it's a good way of keeping your existing (and functional) Mint 17.x system going. This page will demonstrate how to install newer versions of GCC and Clang.

Most of the work will be done from the command line, as that is the simplest way of doing it. You can just simply copy and paste the command lines listed below into a terminal. This document assumes that you have an up-to-date version of Linux Mint 17.x currently running on your system. It also assumes that you have GCC version 4.8.x and Clang version 3.5. (The default version of Clang is 3.4.) By default gcc is installed but you may have to install g++ manually:

sudo apt-get install g++
If you don't have Clang installed and you try to run it, you'll see something like this:
The program 'clang' can be found in the following packages:
 * clang-3.3
 * clang-3.4
 * clang-3.5
Try: sudo apt-get install 
If you just install Clang:
sudo apt-get install clang
version 3.4 will be installed by default. This has the advantage that you can just type clang at the command line. If you install another version, say, 3.5, you will have to qualify it as clang-3.5 as that is the name of the executable. There are ways to "fix" this, e.g. rename the executable or make a symbolic link (shortcut). Later, I'll show you a better way (update-alternatives) that allows you to have an unlimited number of versions of a particular piece of software installed and available.

I chose version 3.5 because this will co-exist with Clang version 3.8 nicely. I have discovered (through trial and error) that earlier versions of Clang are removed when installing Clang 3.8. I wanted to keep the older version around because sometimes I like to see how newer versions differ from older versions in the warnings they produce.

I will be updating GCC and Clang on version 17.3 of Linux Mint. To find out which version of GCC is currently enabled, just provide the --version option to gcc:

mmead@rosa:/home/mmead>gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
To see what files are currently installed:

gcc: (shows version 4.8 and support programs)

mmead@rosa:/home/mmead>ls -l /usr/bin/gcc*
lrwxrwxrwx 1 root root       7 May 18 21:23 /usr/bin/gcc -> gcc-4.8
-rwxr-xr-x 1 root root 775,888 May  7 05:30 /usr/bin/gcc-4.8
lrwxrwxrwx 1 root root      10 May 18 21:23 /usr/bin/gcc-ar -> gcc-ar-4.8
-rwxr-xr-x 1 root root  26,968 May  7 05:30 /usr/bin/gcc-ar-4.8
lrwxrwxrwx 1 root root      10 May 18 21:23 /usr/bin/gcc-nm -> gcc-nm-4.8
-rwxr-xr-x 1 root root  26,968 May  7 05:30 /usr/bin/gcc-nm-4.8
lrwxrwxrwx 1 root root      14 May 18 21:23 /usr/bin/gcc-ranlib -> gcc-ranlib-4.8
-rwxr-xr-x 1 root root  26,968 May  7 05:30 /usr/bin/gcc-ranlib-4.8
g++: (shows version 4.8)
mmead@rosa:/home/mmead>ls -l /usr/bin/g++*
lrwxrwxrwx 1 root root       7 Apr  7  2014 /usr/bin/g++ -> g++-4.8
-rwxr-xr-x 1 root root 775,888 May  7 05:09 /usr/bin/g++-4.8
clang: (shows version 3.5, support programs, and clang++)
mmead@rosa:/home/mmead>ls -l /usr/bin/clang*
lrwxrwxrwx 1 root root 25 Feb 12  2015 /usr/bin/clang-3.5 -> ../lib/llvm-3.5/bin/clang
lrwxrwxrwx 1 root root 27 Feb 12  2015 /usr/bin/clang++-3.5 -> ../lib/llvm-3.5/bin/clang++
lrwxrwxrwx 1 root root 44 Feb 12  2015 /usr/bin/clang-apply-replacements-3.5 -> ../lib/llvm-3.5/bin/clang-apply-replacements
lrwxrwxrwx 1 root root 31 Feb 12  2015 /usr/bin/clang-check-3.5 -> ../lib/llvm-3.5/bin/clang-check
lrwxrwxrwx 1 root root 31 Feb 12  2015 /usr/bin/clang-query-3.5 -> ../lib/llvm-3.5/bin/clang-query
lrwxrwxrwx 1 root root 32 Feb 12  2015 /usr/bin/clang-tblgen-3.5 -> ../lib/llvm-3.5/bin/clang-tblgen
lrwxrwxrwx 1 root root 30 Feb 12  2015 /usr/bin/clang-tidy-3.5 -> ../lib/llvm-3.5/bin/clang-tidy
Installing version 3.5 of clang removed version 3.4. I'm not exactly sure why that happened, and it may be possible to have both, but I wasn't interested in keeping it around so it's a non-issue. You'll notice that the installer gave us links so we can just type gcc instead of typing gcc-4.8. The same is true with g++, but not so with clang. I'll fix this after I upgrade the compilers.

Preparation

OK, so we've got a system that has older versions of the compilers installed. Now, it's time to install newer ones, but we also want to keep the older versions. By default, Linux Mint (as well as other distributions) tries to keep the major versions of software the same throughout the life-cycle of the distribution. This means that, since Mint 17 shipped with version 4.8 of GCC, it will stay at that version with the 17.x versions.

Version 5.x of GCC may be radically different than the 4.x series, so it won't go into Mint until version 18 of Linux Mint. So, if you want to "get ahead" and install versions that are significantly different (newer), you have to do so manually.

Adding a repository

By default, you can't just type:
sudo apt-get install gcc-5
or whatever and expect the software to be upgraded. You'll likely see something like this:
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package gcc-5
The default repositories for Linux Mint 17 contain the current version of the software packages. To gain access to the latest versions (sometimes, not so stable), you have to add extra repositories so that programs like apt-get can search those repositories, as well. Fortunately, like installing programs, this is trivial with Linux Mint (and most distributions). The program is part of the APT package management system and it's called add-apt-repository. Here's how to add the repository for GCC:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
You'll see something like this:
You are about to add the following PPA to your system:
 Toolchain test builds; see https://wiki.ubuntu.com/ToolChain

 More info: https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test
Press [ENTER] to continue or ctrl-c to cancel adding it
If this is what you intended, then press ENTER and you'll see this (output wrapped for easier reading):
Executing: gpg --ignore-time-conflict --no-options --no-default-keyring 
  --homedir /tmp/tmp.ZBVGPqLqwd --no-auto-check-trustdb --trust-model always 
  --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg 
  --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys BA9EF27F
gpg: requesting key BA9EF27F from hkp server keyserver.ubuntu.com
gpg: key BA9EF27F: public key "Launchpad Toolchain builds" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
This shows that the repository has been successfully added. To add the repository for Clang:
sudo apt-add-repository "deb http://llvm.org/apt/trusty/ llvm-toolchain-trusty-3.8 main"
Now, before you can access it either of these, you have to update the repository information in the usual fashion:
sudo apt-get update
That's it! Now you can install GCC version 5 and Clang version 3.8.

Installation

You simply install it like any other package:
sudo apt-get install gcc-5 g++-5
And you'll see something like this:
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  cpp-5 gcc-5-base gcc-6-base gcc-6-base:i386 libasan2 libatomic1 libcc1-0
  libcilkrts5 libgcc-5-dev libgcc1 libgcc1:i386 libgomp1 libisl15 libitm1
  liblsan0 libmpfr4 libmpx0 libquadmath0 libstdc++-5-dev libstdc++6:i386
  libstdc++6 libtsan0 libubsan0
Suggested packages:
  gcc-5-locales g++-5-multilib gcc-5-doc libstdc++6-5-dbg gcc-5-multilib
  libgcc1-dbg libgomp1-dbg libitm1-dbg libatomic1-dbg libasan2-dbg
  liblsan0-dbg libtsan0-dbg libubsan0-dbg libcilkrts5-dbg libmpx0-dbg
  libquadmath0-dbg libstdc++-5-doc
The following NEW packages will be installed:
  cpp-5 g++-5 gcc-5 gcc-5-base gcc-6-base gcc-6-base:i386 libasan2 libcc1-0
  libcilkrts5 libgcc-5-dev libisl15 liblsan0 libmpx0 libstdc++-5-dev libubsan0
The following packages will be upgraded:
  libatomic1 libgcc1 libgcc1:i386 libgomp1 libitm1 libmpfr4 libquadmath0
  libstdc++6:i386 libstdc++6 libtsan0
10 upgraded, 15 newly installed, 0 to remove and 14 not upgraded.
Need to get 84.4 MB of archives.
After this operation, 407 MB of additional disk space will be used.
Do you want to continue? [Y/n] 
Depending on the speed of your computer and, more importantly, the speed of your Internet connection, this will take between a few seconds and a few minutes. Checking the files in /usr/bin you'll see something like this now: gcc: (shows both 4.8 and 5, with the default being 4.8)
mmead@rosa:/home/mmead>ls -l /usr/bin/gcc*
lrwxrwxrwx 1 root root       7 May 18 21:23 /usr/bin/gcc -> gcc-4.8
-rwxr-xr-x 1 root root 775,888 May  7 05:30 /usr/bin/gcc-4.8
-rwxr-xr-x 1 root root 878,384 Dec  5  2015 /usr/bin/gcc-5
lrwxrwxrwx 1 root root      10 May 18 21:23 /usr/bin/gcc-ar -> gcc-ar-4.8
-rwxr-xr-x 1 root root  26,968 May  7 05:30 /usr/bin/gcc-ar-4.8
-rwxr-xr-x 1 root root  26,976 Dec  5  2015 /usr/bin/gcc-ar-5
lrwxrwxrwx 1 root root      10 May 18 21:23 /usr/bin/gcc-nm -> gcc-nm-4.8
-rwxr-xr-x 1 root root  26,968 May  7 05:30 /usr/bin/gcc-nm-4.8
-rwxr-xr-x 1 root root  26,976 Dec  5  2015 /usr/bin/gcc-nm-5
lrwxrwxrwx 1 root root      14 May 18 21:23 /usr/bin/gcc-ranlib -> gcc-ranlib-4.8
-rwxr-xr-x 1 root root  26,968 May  7 05:30 /usr/bin/gcc-ranlib-4.8
-rwxr-xr-x 1 root root  26,976 Dec  5  2015 /usr/bin/gcc-ranlib-5
g++: (shows both 4.8 and 5, with the default being 4.8)
mmead@rosa:/home/mmead>ls -l /usr/bin/g++*
lrwxrwxrwx 1 root root       7 Apr  7  2014 /usr/bin/g++ -> g++-4.8
-rwxr-xr-x 1 root root 775,888 May  7 05:09 /usr/bin/g++-4.8
-rwxr-xr-x 1 root root 882,480 Dec  5  2015 /usr/bin/g++-5

Starting with version 5 of GCC, the version numbering scheme has changed somewhat. You no longer install gcc-5.1 or gcc-5.2. Instead, you simply install gcc-5, and whichever version is the latest one, that's what you get. This means that you won't see a third number in the versioning (e.g. 4.8.4). In the old system, going from 4.8.2 to 4.8.4 meant it was a trivial update (small changes, no new features). Going from 4.8 to 4.9 was consider non-trivial. In the new scheme, going from 5.1 to 5.2 is considered trivial. For non-trivial changes, the version will go from 5.x to 6. This means the first number is going to change more rapidly now. In fact, version 6 of GCC is already available.

Installing Clang 3.8

sudo apt-get install clang-3.8
Check the files:
mmead@rosa:/home/mmead>ls -l /usr/bin/clang*
lrwxrwxrwx 1 root root 25 Feb 12  2015 /usr/bin/clang-3.5 -> ../lib/llvm-3.5/bin/clang
lrwxrwxrwx 1 root root 27 Feb 12  2015 /usr/bin/clang++-3.5 -> ../lib/llvm-3.5/bin/clang++
lrwxrwxrwx 1 root root 25 Jun 30 00:35 /usr/bin/clang-3.8 -> ../lib/llvm-3.8/bin/clang
lrwxrwxrwx 1 root root 27 Jun 30 00:35 /usr/bin/clang++-3.8 -> ../lib/llvm-3.8/bin/clang++
lrwxrwxrwx 1 root root 44 Feb 12  2015 /usr/bin/clang-apply-replacements-3.5 -> ../lib/llvm-3.5/bin/clang-apply-replacements
lrwxrwxrwx 1 root root 44 Jun 30 00:35 /usr/bin/clang-apply-replacements-3.8 -> ../lib/llvm-3.8/bin/clang-apply-replacements
lrwxrwxrwx 1 root root 31 Feb 12  2015 /usr/bin/clang-check-3.5 -> ../lib/llvm-3.5/bin/clang-check
lrwxrwxrwx 1 root root 31 Jun 30 00:35 /usr/bin/clang-check-3.8 -> ../lib/llvm-3.8/bin/clang-check
lrwxrwxrwx 1 root root 28 Jun 30 00:35 /usr/bin/clang-cl-3.8 -> ../lib/llvm-3.8/bin/clang-cl
lrwxrwxrwx 1 root root 31 Feb 12  2015 /usr/bin/clang-query-3.5 -> ../lib/llvm-3.5/bin/clang-query
lrwxrwxrwx 1 root root 31 Jun 30 00:35 /usr/bin/clang-query-3.8 -> ../lib/llvm-3.8/bin/clang-query
lrwxrwxrwx 1 root root 32 Jun 30 00:35 /usr/bin/clang-rename-3.8 -> ../lib/llvm-3.8/bin/clang-rename
lrwxrwxrwx 1 root root 32 Feb 12  2015 /usr/bin/clang-tblgen-3.5 -> ../lib/llvm-3.5/bin/clang-tblgen
lrwxrwxrwx 1 root root 30 Feb 12  2015 /usr/bin/clang-tidy-3.5 -> ../lib/llvm-3.5/bin/clang-tidy
Now, we've successfully installed the updates. We have versions 4.8 and 5 installed for gcc and g++ and we have versions 3.5 and 3.8 installed for g++. Right now, if you type gcc or g++, you're going to get version 3.8. If you just type clang, you'll get an error.

To use version 5 of gcc, you have to type gcc-5. This is all fine and good, but it's tedious and if you have makefiles that assume gcc (which most do), this won't work very well. We need to do a few things to allow us to easily switch between GCC and Clang versions as well as just typing the shorter form without explicitly stating the version.

Managing Multiple Versions

I mentioned above that there are lots of ways that you can manage multiple versions of a program. This could be by renaming files (not an ideal solution) or manually creating symbolic links (not a bad idea). Linux has a mechanism already in place to deal with just this situation. It calls multiple versions alternatives. All of the details about how this mechanism works is well beyond the scope of this little tutorial, but I'll give you a brief introduction.

If you look in /etc/alternatives/ you will probably see lots (maybe hundreds) of links. Here are a few from the Linux Mint 17.3 system I've been using for this tutorial (there are actually about 200 links):

lrwxrwxrwx 1 root root  12 Aug  4 08:49 c++ -> /usr/bin/g++
lrwxrwxrwx 1 root root  12 May 18 21:23 cc -> /usr/bin/gcc
lrwxrwxrwx 1 root root   9 May 18 21:23 editor -> /bin/nano
lrwxrwxrwx 1 root root  17 May 18 21:23 ex -> /usr/bin/vim.tiny
lrwxrwxrwx 1 root root  19 May 18 21:23 ftp -> /usr/bin/netkit-ftp
lrwxrwxrwx 1 root root   9 May 18 21:23 pager -> /bin/less
lrwxrwxrwx 1 root root   9 May 18 21:23 pico -> /bin/nano
lrwxrwxrwx 1 root root  17 May 18 21:23 vi -> /usr/bin/vim.tiny
lrwxrwxrwx 1 root root  17 May 18 21:23 view -> /usr/bin/vim.tiny
To see which executable programs in /usr/bin are using alternatives, just type this:
ls -l /usr/bin/ | grep alternatives
and you will see which programs are just links to alternative programs (or versions). There are about 60 files on this Linux Mint 17.3 system and here's a sample:
lrwxrwxrwx 1 root  root            21 Aug  4 08:37 c++ -> /etc/alternatives/c++
lrwxrwxrwx 1 root  root            20 May 18 21:23 cc -> /etc/alternatives/cc
lrwxrwxrwx 1 root  root            24 May 18 21:23 editor -> /etc/alternatives/editor
lrwxrwxrwx 1 root  root            20 May 18 21:23 ex -> /etc/alternatives/ex
lrwxrwxrwx 1 root  root            21 May 18 21:23 ftp -> /etc/alternatives/ftp
lrwxrwxrwx 1 root  root            23 May 18 21:23 pager -> /etc/alternatives/pager
lrwxrwxrwx 1 root  root            22 May 18 21:23 pico -> /etc/alternatives/pico
lrwxrwxrwx 1 root  root            20 May 18 21:23 vi -> /etc/alternatives/vi
lrwxrwxrwx 1 root  root            22 May 18 21:23 view -> /etc/alternatives/view
You can see that these are just links to the corresponding link in /etc/alternatives/. For those of you that are programmers (likely, all of you), you can think of a link here as a pointer-to-pointer-to-a-file. So, if you typed ftp, this is what's happening:
/usr/bin/ftp ===> /etc/alternatives/ftp ===> /usr/bin/netkit-ftp
Also, remember that /usr/bin/ is in the PATH, but /etc/alternatives/ is not. So, if there was an alternative FTP program installed (say, fast-ftp) and the user wanted to use that instead, the system would just update the link in /etc/alternatives/, so the chain might look like this:
/usr/bin/ftp ===> /etc/alternatives/ftp ===> /usr/local/bin/fast-ftp
This is a fictitous program (I think!), but I'm just showing how you could have another FTP program installed in a completely different location and then just update the link to point to that one. See? It's just like programming with pointers!

Managing all of these alternative links (pointers) is done by a program called, appropriately enough, update-alternatives. There are lots of options with this program, but I'm just going to cover the basics so you can use it effectively. To see what alternatives are available for the FTP program (ftp), just type:

update-alternatives --list ftp
and you'll see this (on a default Linux Mint 17.3 install)
/usr/bin/netkit-ftp
What it's telling us is that there is only one "alternative" program for (ftp). Most, if not all of the links in /etc/alternatives/ have only 1 program associated with them, so there really is no "alternative". But, they are in place so that when you decide to have multiple versions/programs for one command, you can do so easily. Let's look at gcc:
update-alternatives --list gcc
Output:
update-alternatives: error: no alternatives for gcc
This is telling us that there currently is no alternative for gcc. However, we just installed version 5.3 alongside of version 4.8, so we do have an alternative. To set it up, do this:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50
Output:
update-alternatives: using /usr/bin/gcc-4.8 to provide /usr/bin/gcc (gcc) in auto mode
The general form of the command is:
update-alternatives link name path priority
where: You probably won't ever need the priority, so just set it to some integer value. I generally choose unique values between 20 and 80, with no particular reason why. Now, if you list the alternatives:
update-alternatives --list gcc
You'll see this:
/usr/bin/gcc-4.8
To add version 5 of GCC to the set of alternatives, just do this:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 60
Now, list the alternatives:
update-alternatives --list gcc
and you'll see this:
/usr/bin/gcc-4.8
/usr/bin/gcc-5
OK, so that's all fine and good. You now know how to add (or install) alternatives. If you had another version, say version 6, you would do the same thing for that, changing gcc-5 to gcc-6 in the above commands.

Now, I could do the same thing with g++, as well. But, I don't really want to use two different versions of gcc and g++ at the same time (e.g. gcc-4.8 and g++-5). I want them to be synchronized so that if I change gcc, then g++ will also change. This is the command to do that:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 60 --slave /usr/bin/g++ g++ /usr/bin/g++-5
This ties gcc-4.8 with g++-4.8 and gcc-5 with g++-5 so when we change gcc, then g++ will also change. And that's the last step I need to show you: How to switch between the alternatives. Type this:
sudo update-alternatives --config gcc
and you should see something like this:
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path              Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-5     60        auto mode
  1            /usr/bin/gcc-4.8   50        manual mode
  2            /usr/bin/gcc-5     60        manual mode

Press enter to keep the current choice[*], or type selection number: 
This tells us that item #0 (with the asterisk) is the current alternative (version 5). If you want to make version 4.8 the current alternative, simply type 1 and hit ENTER. To verify the versions of the compilers, type:
gcc --version && g++ --version
And you should see:

(When configured for version 4.8)

gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
(When configured for version 5)
gcc (Ubuntu 5.3.0-3ubuntu1~14.04) 5.3.0 20151204
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

g++ (Ubuntu 5.3.0-3ubuntu1~14.04) 5.3.0 20151204
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
To setup the two versions of Clang:
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-3.5 50 --slave /usr/bin/clang++ clang++ /usr/bin/clang++-3.5
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-3.8 60 --slave /usr/bin/clang++ clang++ /usr/bin/clang++-3.8
This also ties clang and clang++ together for each version. So, to configure the system to use a particular version of the compilers, just run:
sudo update-alternatives --config compiler
where compiler is either gcc or clang. This will persist until you run the update-alternatives command again.

Note: If you run

update-alternatives --config gcc
with only one compiler configured, you'll see something like this:
There is only one alternative in link group gcc (providing /usr/bin/gcc): /usr/bin/gcc-4.8
Nothing to configure.

Summary (tl;dr)

Steps to install and enable multiple compilers (this assumes GCC version 4.8/5 and Clang version 3.5/3.8):
  1. Add the necessary repositories on Linux Mint 17.x for GCC and Clang:
    sudo add-apt-repository ppa:ubuntu-toolchain-r/test
    sudo apt-add-repository "deb http://llvm.org/apt/trusty/ llvm-toolchain-trusty-3.8 main"
    
  2. Update the repositories:
    sudo apt-get update
    
  3. Install gcc-5, g++-5, and clang-3.8:
    sudo apt-get install gcc-5 g++-5 clang-3.8
    
  4. Add each version of each compiler as an alternative program:
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 60 --slave /usr/bin/g++ g++ /usr/bin/g++-5
    sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-3.5 50 --slave /usr/bin/clang++ clang++ /usr/bin/clang++-3.5
    sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-3.8 60 --slave /usr/bin/clang++ clang++ /usr/bin/clang++-3.8
    
    This also ties (synchronizes) gcc with g++ and clang with clang++
  5. To enable a particular version (for the entire system):
    sudo update-alternatives --config compiler  
    
    where compiler is either gcc or clang.
Notes:

Update - Install GCC 7.x

If your system already has GCC 5 installed and you'd like to upgrade to GCC 7.x, there is a new script: install-gcc7.sh. You will have to download it and make it executable.

It's similar to the script above, but installs version 7.x instead. It also doesn't include Clang in the installation. If you've already run the above script, you don't need the first line in the new script:

# Add the repositories
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
because you've already set up the repository. It won't hurt if you try to add it again, though.

You can probably use this script to install any version of GCC (6, 7, 8, etc.) in the future simply by changing the appropriate lines.

Note: This procedure will not remove version 5.x from your computer. It will install version 7.x alongside of version 5.x. This means that you can switch between the two versions at any time using the command:

sudo update-alternatives --config gcc

Update - Install all newer versions

The previous section showed you how to install version 7.x of the GNU gcc compiler. It turns out, it's trivial to install other later versions as well. I've updated the script: install-gcc-all.sh to install versions 6, 7, 8, 9, 10, and 11. If you don't want to install and configure all of those compilers, just comment out the lines that you don't want. (To comment out a line, just put a # in front of it.)

The script looks like this below. Note that some of the older compilers may not be available from the original locations. If you don't want the 32-bit versions, just remove all references to "multilib" files.

#!/bin/bash

# Add the repositories
sudo add-apt-repository ppa:ubuntu-toolchain-r/test

# Update the repositories:
sudo apt-get update

# Install gcc/g++ 5,6,7,8,9,10,11 32-bit and 64-bit
sudo apt-get --assume-yes install gcc-5 g++-5 gcc-5-multilib g++-5-multilib
sudo apt-get --assume-yes install gcc-6 g++-6 gcc-6-multilib g++-6-multilib
sudo apt-get --assume-yes install gcc-7 g++-7 gcc-7-multilib g++-7-multilib
sudo apt-get --assume-yes install gcc-8 g++-8 gcc-8-multilib g++-8-multilib
sudo apt-get --assume-yes install gcc-9 g++-9 gcc-9-multilib g++-9-multilib
sudo apt-get --assume-yes install gcc-10 g++-10 gcc-10-multilib g++-10-multilib
sudo apt-get --assume-yes install gcc-11 g++-11 gcc-11-multilib g++-11-multilib

# Add each version of each compiler as an alternative program:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 50 --slave /usr/bin/g++ g++ /usr/bin/g++-5
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-6
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70 --slave /usr/bin/g++ g++ /usr/bin/g++-7
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 80 --slave /usr/bin/g++ g++ /usr/bin/g++-8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 --slave /usr/bin/g++ g++ /usr/bin/g++-9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 91 --slave /usr/bin/g++ g++ /usr/bin/g++-10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 92 --slave /usr/bin/g++ g++ /usr/bin/g++-11

# Set the desired compiler versions:
sudo update-alternatives --config gcc

# Check that the versions are correct:
gcc --version
I've arbitrarily set the priority for each compiler. If you wish to change those, consult this reference for the meaning of those numbers.

To change the version of the compiler you want to use, just run the command:

sudo update-alternatives --config gcc
and choose the version from the list. You should see something like this displayed:
There are 5 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path            Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-9   90        auto mode
  1            /usr/bin/gcc-5   50        manual mode
  2            /usr/bin/gcc-6   60        manual mode
  3            /usr/bin/gcc-7   70        manual mode
  4            /usr/bin/gcc-8   80        manual mode
  5            /usr/bin/gcc-9   90        manual mode

Press <enter> to keep the current choice[*], or type selection number: 

VirtualBox Caveat

If you don't use VirtualBox, you can ignore this section.

There is something peculiar that I have noticed over the last couple of years when it comes to updating
VirtualBox on Linux. If you have installed a newer compiler than the one that came with your Linux distribution, your virtual machines (VMs) may no longer run. Searching the Internet, I've never found any definitive reason why this happens. Actually, I've found posts that indicate what the solution is, but none of them have ever worked for me. However, the fix that I use each time this happens is to revert back to the original version of gcc that came with the distribution.

For example, one of my systems runs Linux Mint 17 (Qiana). It's been running just fine for years, although it's getting a little old and at some point it will no longer be supported. I update/upgrade my system every so often like this:

sudo apt-get update && sudo apt-get upgrade
If VirtualBox happens to be upgraded, when I try to run a virtual machine, it fails. Unfortunately, I don't have the exact error message handy, but it said something along these lines:
Kernel driver not installed (rc=-1908)
You can see a similar error message from Ubuntu here (Note to self: Post the error message here next time this happens!)

None of the "solutions" I found on the Internet solved the problem. The fix for me was to simply switch the version of gcc to the original version temporarily by running:

sudo update-alternatives --config gcc
and choosing the older version. In my case, that version is 4.8. Then I ran the suggested command:
sudo /sbin/vboxconfig
and now the virtual machines will run again. This may not happen to you, but I'm putting this here in case it helps. It helps me, and I sometimes refer to these page myself.

References

This was just a quick introduction to installing newer versions of the compilers, as well as using update-alternatives to manage them. For more information, follow these links:

You can also Google for update-alternatives to find more references and information on using it.