Site Overlay

Latest gcc compiler on Ubuntu

I recently had some trouble compiling source code from the net which would not run under Ubuntu’s default gcc version, which is 5.4.0 for Xenial. The first thing to know is where to get the most recent gcc version, the second problem is how to tell Ubuntu (and the IDEs you are running) which of the gcc versions installed should be used.

Getting the latest gcc

You can read how to get the latest gcc in this post. The good news is: You don’t have to build it yourself, there is a ppa. Just type

In my case, this triggered a whopping 500 MB download. So lean back and let apt do its work.

Setting the default version for gcc

Installing the gcc compiler alone won’t get you far. The reason is that unlike with updating some standard application like VirtualBox, the installation does not replace the old version by the new one but it installs the new version along the existing one(s). This however means that even if you have a more recent version installed, Ubuntu and your IDE will revert to the old version as your existing one.

You can easily see that Ubuntu still reverts to the old 5.4.0 version after the installation:

As you can see, Ubuntu still invokes its default 5.4.0 version although the latest version 6 compiler is already installed. You can set the new default version with the update-alternatives command as described in this post.

We start with removing any alternative instructions:

We now establish a new update-alternatives “hierarchy”:

When installing cc and c++ you may get an warning saying that update-alternatives is forcing the installation of /usr/bin/g++ because link group c++ is broken (or equivalent for cc). Don’t worry, it just means that update-alternatives has so far not produced any corresponding symlink into /usr/bin/ and it will produce that symlink now.