23

After installing Ubuntu 16.04 LTS, I found that gcc 5.3 installed by default but I don't receive any updates to gcc. I opened up GCC website and I found the new release 6.1. How do I update?

Output of gcc --version:

gcc (Ubuntu 5.3.1-14ubuntu2.1) 5.3.1 20160413
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.
26

You can install GCC 6 by adding the ubuntu-toolchain-r/test PPA. To do so, run the following commands:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update
sudo apt install gcc-6

You can verify that gcc-6 is installed by running gcc-6 --version and the output should say gcc-6 (Ubuntu 6.1.1-2ubuntu12~16.04) 6.1.1 20160510.

As suggested by Mohamed Slama, if you want to further change the default GCC and G++ to the latest versions, install g++-6 with

sudo apt install g++-6

and then run

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-6    
  • Are you using it? how stable is it? – Videonauth Jun 5 '16 at 1:30
  • @Videonauth I am not using it. I know from a colleague that GCC 6 introduced regressions that broke the compilation of a software that we use. – edwinksl Jun 5 '16 at 1:31
  • Ah o.k. well then i rather stay at 5.3.1 for now. – Videonauth Jun 5 '16 at 1:32
  • @Videonauth Yeah, there is no harm installing gcc-6 though and trying to see if it works for you. Your gcc 5.3.1 is still around and gcc is still symlinked to gcc-5 instead of gcc-6. – edwinksl Jun 5 '16 at 1:33
  • what does 60 do here? – ar2015 Oct 1 '18 at 5:01
5

If you want to build it from source (which I recommend as you can for example make a cross-compiler, etc.) download the source from a mirror.

Then extract it with:

tar -xvf gcc-6.1.0.tar.gz

After that change directory to there:

cd gcc-6.1.0

Then create build directory and cd to it:

mkdir build
cd build

Then configure the makefile (--disable-multilib means to not build libraries for cross-compilation):

../configure --enable-languages=c,c++ --disable-multilib

If you ran into errors due to missing required libraries or other prerequisites: (Credits to this)

./contrib/download_prerequisites

And then build it:

make -j 8

This process may take some time and after done invoke this:

sudo make install

That's it!

  • While the -j flag speeds up the compilation quite a bit it is not advised to use it building tool-chain executables, and a make check should be run before installing. – Videonauth Jun 5 '16 at 3:20
  • Well that worked for me :) and I even built a cross compiler with it – Ehsan Jun 5 '16 at 3:21
  • 1
    I didn't say that it wont work, but gcc and g++ are more than only compilers for C, C++. – Videonauth Jun 5 '16 at 3:29
  • But @Videonauth when I didn't use -j flag it took 3 hours to build it which was very frustrating :| – Ehsan Jun 5 '16 at 3:31
  • I know, and it takes even longer on older systems to make a full compile and check on my very old dual core laptop took 3 days all in all. You might want to have a look at how a Linux is build from scratch this might shed some light, they explain on those sites as well why compiling with flags is not the best idea if you plan to rely on it. Saying that i have successfully build a couple of personal used Linux systems myself meanwhile. – Videonauth Jun 5 '16 at 3:35