Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Installing gcc-7 & g++-7 in Ubuntu 16.04LTS Xenial

Run the following in the terminal:

Install the gcc-7 packages:

sudo apt-get install -y software-properties-common
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update
sudo apt install g++-7 -y

Set it up so the symbolic links gcc, g++ point to the newer version:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 60 \
                         --slave /usr/bin/g++ g++ /usr/bin/g++-7 
sudo update-alternatives --config gcc
gcc --version
g++ --version

# This one if you want the **all** toolchain programs (with the triplet names) to also point to gcc-7. 
# For example, this is needed if building Debian packages.
# If you are already are root (e.g. inside a docker image), remove the "sudo" below.
ls -la /usr/bin/ | grep -oP "[\S]*(gcc|g\+\+)(-[a-z]+)*[\s]" | xargs sudo bash -c 'for link in ${@:1}; do ln -s -f "/usr/bin/${link}-${0}" "/usr/bin/${link}"; done' 7
@Mengmi
Copy link

Mengmi commented Jun 21, 2019

have you solved the issue above? I am trying to install rtx 2080 ti and nvidia driver also shows cc version check failure. I have to install gcc 7.3. instead of 7.4 but I dont know how

@songwenhuang
Copy link

songwenhuang commented Aug 21, 2019

This works perfect to install gcc and g++ 7.4.0

Thanks!

@thelittlekid
Copy link

thelittlekid commented Aug 27, 2019

Thanks! It worked and only got an error for g++
update-alternatives: error: alternative g++ can't be slave of gcc: it is a master alternative.
Need to configure gcc and g++ separately (https://askubuntu.com/questions/26498/how-to-choose-the-default-gcc-and-g-version)

@pan-22
Copy link

pan-22 commented Apr 6, 2020

I tried the listed steps but I got an error for adding ppa:
when I ran:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
I get

gpg: keyring /tmp/tmpir2cfkik/secring.gpg' created
gpg: keyring /tmp/tmpir2cfkik/pubring.gpg' created
gpg: requesting key BA9EF27F from hkp server keyserver.ubuntu.com
Error: retrieving gpg key timed out.
gpg: keyserver timed out
gpg: keyserver receive failed: keyserver error

I tried this but got the same error:
sudo add-apt-repository ppa:ubuntu-toolchain-r/ppa
Here is my linux distribution and kernel version:
Linux amax 4.15.0-45-generic #48~16.04.1-Ubuntu SMP Tue Jan 29 18:03:48 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
Can anyone help with it?

@Mizux
Copy link

Mizux commented Aug 14, 2020

FYI my docker version

FROM ubuntu:16.04 
...
# Install gcc 7
RUN apt update -qq \
&& apt install -yq software-properties-common \
&& add-apt-repository -y ppa:ubuntu-toolchain-r/test \
&& apt update -qq \
&& apt install -yq g++-7 \
&& apt clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# Configure alias
RUN update-alternatives \
 --install /usr/bin/gcc gcc /usr/bin/gcc-7 60 \
 --slave /usr/bin/g++ g++ /usr/bin/g++-7 \
 --slave /usr/bin/gcov gcov /usr/bin/gcov-7 \
 --slave /usr/bin/gcov-tool gcov-tool /usr/bin/gcov-tool-7 \
 --slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-7 \
 --slave /usr/bin/gcc-nm gcc-nm /usr/bin/gcc-nm-7 \
 --slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-7

with:

  • -y : auto confirm
  • -q: quiet mode (several increase quiet level)

@rancheng
Copy link

rancheng commented Nov 1, 2020

don't forget to update this executable:

update-alternatives --install /usr/bin/x86_64-linux-gnu-gcc x86_64-linux-gnu-gcc /usr/bin/x86_64-linux-gnu-gcc-7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment