Install gcc 7.1 on Xubuntu 16.04 and make it default

Solution 1:

AFAIK the toochain-r PPA that you used is the recommended source for alternate versions of GCC. To make it the default, you can either use the update-alternatives mechanism as explained in this previous Q&A

How to use multiple instances of gcc?

or by direct symlinking as described in

Downloaded g++ 4.8 from the PPA but can't set it as default?


Although in practice it's often not necessary, since most build systems allow you to specify a particular compiler, either using command-line arguments or environment variables e.g.

CC=/usr/bin/gcc-7 ./configure

or

make CC=/usr/bin/gcc-7

or

cmake -D CMAKE_C_COMPILER=/usr/bin/gcc-7 ..

-- see for example CMake Useful Variables.

Solution 2:

You can use update-alternatives to make it default:

update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 60

For instance, this Dockerfile gives you Ubuntu 16.04 with gcc 7:

FROM ubuntu:16.04

RUN \
  apt-get update && \
  apt-get install -y software-properties-common && \
  add-apt-repository ppa:ubuntu-toolchain-r/test && \
  apt-get update && \
  apt-get install -y gcc-7 g++-7 && \
  update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 60 && \
  update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 60
CMD /bin/bash