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
@proiecteit

This comment has been minimized.

Copy link

proiecteit commented Mar 27, 2018

In addition to the above, in case someone encounters issues, before adding the repository, if it's a fresh install of Ubuntu, you will need to run:

sudo apt-get install -y software-properties-common python-software-properties

Also, to make sure that you have the entire build stack you need to run:

sudo update-alternatives \
    --install /usr/bin/gcc gcc /usr/bin/gcc-7 60 \
    --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
@BryceFuller

This comment has been minimized.

Copy link

BryceFuller commented Jul 11, 2018

I tried the steps listed here and now I can't properly use apt or pip, it's saying the connections are not able to be established.
Can you please tell me how to undo the first command:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test

@jlblancoc, @proiecteit

To clarify, when I ran the command the first time it worked fine. I ran all of the commands listed here and shortly after they completed my vm crashed. I did not know if the changes had been saved (this has happened in the past) so I tried to re-run the commands and now the first command (the one listed above) returned an error saying: ERROR: '~ubuntu-toolchain-r' user or team does not exist.

When I ran
sudo apt update
I get

Err:1 http://us.archive.ubuntu.com/ubuntu xenial InRelease
  Temporary failure resolving 'us.archive.ubuntu.com'
Err:2 http://us.archive.ubuntu.com/ubuntu xenial-updates InRelease
  Temporary failure resolving 'us.archive.ubuntu.com'
Err:3 http://us.archive.ubuntu.com/ubuntu xenial-backports InRelease
  Temporary failure resolving 'us.archive.ubuntu.com'
Err:4 http://security.ubuntu.com/ubuntu xenial-security InRelease
  Temporary failure resolving 'security.ubuntu.com'
Err:5 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu xenial InRelease
  Temporary failure resolving 'ppa.launchpad.net'
Reading package lists... Done
Building dependency tree       
Reading state information... Done
281 packages can be upgraded. Run 'apt list --upgradable' to see them.
W: Failed to fetch http://us.archive.ubuntu.com/ubuntu/dists/xenial/InRelease  Temporary failure resolving 'us.archive.ubuntu.com'
W: Failed to fetch http://us.archive.ubuntu.com/ubuntu/dists/xenial-updates/InRelease  Temporary failure resolving 'us.archive.ubuntu.com'
W: Failed to fetch http://us.archive.ubuntu.com/ubuntu/dists/xenial-backports/InRelease  Temporary failure resolving 'us.archive.ubuntu.com'
W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/xenial-security/InRelease  Temporary failure resolving 'security.ubuntu.com'
W: Failed to fetch http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu/dists/xenial/InRelease  Temporary failure resolving 'ppa.launchpad.net'
W: Some index files failed to download. They have been ignored, or old ones used instead.

This did not happen the first time. Does anyone know what is wrong?

@abexultan

This comment has been minimized.

Copy link

abexultan commented Jul 31, 2018

@BryceFuller "Failed to fetch" usually means that there is no internet connection. It rarely happens, but ubuntu mirrors can be down for a short period as well.

@mingxingren

This comment has been minimized.

Copy link

mingxingren commented Aug 17, 2018

太棒了,太棒了,简直太棒了

@osmeest

This comment has been minimized.

Copy link

osmeest commented Aug 22, 2018

In the past, I've made a script that was recreating the /usr/bin/gcc and friends as symbolic link to a specific version. This allowed to switch like you do with update-alternative. The end result was very similar.
This has however a caveat: since Spectre fixes have been introduced, compiling kernel drivers fails because some compiler options used in kernel compilation are not present in the test toolchain, making the compiled driver incompatible with the kernel.

@josephrocca

This comment has been minimized.

Copy link

josephrocca commented May 21, 2019

Regarding switching gcc and g++ to reference the newly installed binary (gcc-8 in my case), this was helpful for me: https://askubuntu.com/a/26518/606179

@mikechen66

This comment has been minimized.

Copy link

mikechen66 commented May 28, 2019

Thanks for all of your answers. I have tried to install gcc-7.3 with the following commands. But I still get gcc 7.4.0. Can you provide a method only to install gcc 7.3.0?

$ sudo add-apt-repository ppa:jonathonf/gcc-7.3
$ sudo apt-get update
$ sudo apt-get install gcc-7 g++-7
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 60 --slave /usr/bin/g++ g++ /usr/bin/g++-7

After completion of the installation, I input the command of gcc --version and get the version of gcc 7.4.0.

Please see the message as follows

gcc ubuntu (Ubuntu 7.4.0-1ubuntu1~18.04) 7.4.0
............................................................................

I have Nvidia GeForce RTX 2060 which driver not supports gcc 7.4.0 but gcc 7.3.0. Please help me figure out the issue.

Note:

I also used the following commands to get gcc 7.4.0 rather than 7.3.0. So it is problem to install gcc 7.3.0.

$ sudo apt install build-essential
or
$ sudo apt install gcc

Thanks in advance,

Mike

@Mengmi

This comment has been minimized.

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

This comment has been minimized.

Copy link

songwenhuang commented Aug 21, 2019

This works perfect to install gcc and g++ 7.4.0

Thanks!

@thelittlekid

This comment has been minimized.

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.