Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
How to install latest gcc on Ubuntu LTS (12.04, 14.04, 16.04)
These commands are based on a askubuntu answer http://askubuntu.com/a/581497
To install gcc-6 (gcc-6.1.1), I had to do more stuff as shown below.
USE THOSE COMMANDS AT YOUR OWN RISK. I SHALL NOT BE RESPONSIBLE FOR ANYTHING.
ABSOLUTELY NO WARRANTY.
If you are still reading let's carry on with the code.
sudo apt-get update && \
sudo apt-get install build-essential software-properties-common -y && \
sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y && \
sudo apt-get update && \
sudo apt-get install gcc-snapshot -y && \
sudo apt-get update && \
sudo apt-get install gcc-6 g++-6 -y && \
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-6 && \
sudo apt-get install gcc-4.8 g++-4.8 -y && \
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8;
When completed, you must change to the gcc you want to work with by default. Type in your terminal:
sudo update-alternatives --config gcc
To verify if it worked. Just type in your terminal
gcc -v
If everything went fine you should see gcc 6.1.1 by the time I am writing this gist
Happy coding!
See my blog post at https://www.application2000.com

Worked without any issues on a new install of Ubuntu 16.04. Thank you..

worked perfectly thanks.

Thank You

yech1990 commented Aug 28, 2016

g++-6 : Depends: gcc-6-base (= 6.1.1-2ubuntu12~16.04) but 6.1.1-3ubuntu11~14.04.1 is to be installed
         Depends: libstdc++-6-dev (= 6.1.1-2ubuntu12~16.04) but it is not going to be installed
 gcc-6 : Depends: cpp-6 (= 6.1.1-2ubuntu12~16.04) but it is not going to be installed
         Depends: gcc-6-base (= 6.1.1-2ubuntu12~16.04) but 6.1.1-3ubuntu11~14.04.1 is to be installed
         Depends: libgcc-6-dev (= 6.1.1-2ubuntu12~16.04) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

zxshoe commented Sep 21, 2016

This worked fine today.

update-alternatives --config gcc
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).
`

Selection Path Priority Status

  • 0 /usr/bin/gcc-6 60 auto mode
    1 /usr/bin/gcc-4.8 60 manual mode
    2 /usr/bin/gcc-6 60 manual mode

Press to keep the current choice[*], or type selection number:`

Thanks!

Very helpful, thank you

Thanks, worked great!

I modified it slightly to keep the default 16.04 gcc-5 around:

 sudo apt-get update && \
 sudo apt-get install build-essential software-properties-common -y && \
 sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y && \
 sudo apt-get update && \
 sudo apt-get install gcc-snapshot -y && \
 sudo apt-get update && \
 sudo apt-get install gcc-6 g++-6 -y && \
 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-6 && \
-sudo apt-get install gcc-4.8 g++-4.8 -y && \
-sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8;
+# Already installed gcc-5 as part of build-essential; I didn't need this line..
+# sudo apt-get install gcc-5 g++-5 -y && \
+sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 60 --slave /usr/bin/g++ g++ /usr/bin/g++-5;

Sakib37 commented Dec 2, 2016

Thanks. Worked perfectly. :)

Hi, i am getting below error while trying to upgrade gcc,

root@compute:/tmp# sudo apt-get update && \

sudo apt-get install build-essential software-properties-common -y &&
sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y &&
sudo apt-get update &&
sudo apt-get install gcc-snapshot -y &&
sudo apt-get update &&
sudo apt-get install gcc-6 g++-6 -y &&
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-6 &&
-sudo apt-get install gcc-4.8 g++-4.8 -y &&
-sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8;
Hit:1 http://ppa.launchpad.net/ubuntu-toolchain-r/ppa/ubuntu xenial InRelease
Ign:2 http://ubuntu-cloud.archive.canonical.com/ubuntu xenial-updates/newton InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu xenial InRelease
Hit:4 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu xenial InRelease
Hit:5 http://ubuntu-cloud.archive.canonical.com/ubuntu xenial-updates/newton Release
Get:7 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Get:8 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
Get:9 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
Fetched 306 kB in 1s (199 kB/s)
Reading package lists... Done
Reading package lists... Done
Building dependency tree
Reading state information... Done
build-essential is already the newest version (12.1ubuntu2).
build-essential set to manually installed.
software-properties-common is already the newest version (0.96.20.4).
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
cpp-5 : Depends: gcc-5-base (= 5.4.0-6ubuntu116.04.2) but it is not going to be installed
g++-5 : Depends: gcc-5-base (= 5.4.0-6ubuntu1
16.04.2) but it is not going to be installed
gcc-5 : Depends: gcc-5-base (= 5.4.0-6ubuntu116.04.2) but it is not going to be installed
libasan2 : Depends: gcc-5-base (= 5.4.0-6ubuntu1
16.04.2) but it is not going to be installed
libatomic1 : Depends: gcc-5-base (= 5.4.0-6ubuntu116.04.2) but it is not going to be installed
libcc1-0 : Depends: gcc-5-base (= 5.4.0-6ubuntu1
16.04.2) but it is not going to be installed
libcilkrts5 : Depends: gcc-5-base (= 5.4.0-6ubuntu116.04.2) but it is not going to be installed
libgcc-5-dev : Depends: gcc-5-base (= 5.4.0-6ubuntu1
16.04.2) but it is not going to be installed
libgomp1 : Depends: gcc-5-base (= 5.4.0-6ubuntu116.04.2) but it is not going to be installed
libitm1 : Depends: gcc-5-base (= 5.4.0-6ubuntu1
16.04.2) but it is not going to be installed
liblsan0 : Depends: gcc-5-base (= 5.4.0-6ubuntu116.04.2) but it is not going to be installed
libmpx0 : Depends: gcc-5-base (= 5.4.0-6ubuntu1
16.04.2) but it is not going to be installed
libquadmath0 : Depends: gcc-5-base (= 5.4.0-6ubuntu116.04.2) but it is not going to be installed
libstdc++-5-dev : Depends: gcc-5-base (= 5.4.0-6ubuntu1
16.04.2) but it is not going to be installed
libstdc++-5-dev:i386 : Depends: gcc-5-base:i386 (= 5.4.0-6ubuntu116.04.2) but it is not going to be installed
Depends: libgcc-5-dev:i386 (= 5.4.0-6ubuntu1
16.04.2) but it is not going to be installed
Depends: libstdc++6:i386 (>= 5.4.0-6ubuntu116.04.2) but it is not going to be installed
Depends: libc6-dev:i386 (>= 2.13-0ubuntu6) but it is not going to be installed
libstdc++6 : Depends: gcc-5-base (= 5.4.0-6ubuntu1
16.04.2) but it is not going to be installed
libtsan0 : Depends: gcc-5-base (= 5.4.0-6ubuntu116.04.2) but it is not going to be installed
libubsan0 : Depends: gcc-5-base (= 5.4.0-6ubuntu1
16.04.2) but it is not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).

please help me to short it out.

root@compute:/tmp# update-alternatives --config gcc
update-alternatives: error: no alternatives for gcc

Good Stuff!! thank you worked great on Ubuntu 16.0.4

AraHaan commented Feb 17, 2017

nice, just need it for clang 3.9 for Travis CI.

Hash sum mismatches when running apt-get update can be resolved by flushing metadata. Without the ability to update, the gcc-6 and g++-6 packages won't be available for install.

what does the '-y' at the end of the line?

talland commented Mar 16, 2017

Nice, thank you very much

the -y flag causes apt-get to assume "yes" when it would normally prompt you with a yes /no question

smac89 commented Apr 20, 2017

This is great
Gcc version 6.2.0

zheive commented Apr 21, 2017

Work very well for me, I had problems after update my kernel version with vmware.

Thanks!

ivanmara555 commented May 23, 2017

Toolchain ppa updates to gcc-7.1 on 16.04 Xenial. Additional install instructions:

apt-get upgrade
apt-get full-upgrade
apt-get install gcc-7 g++-7
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 60 --slave /usr/bin/g++ g++ /usr/bin/g++-7
update-alternatives --config gcc

select gcc-7

Owner

application2000 commented May 30, 2017

Hopefully it could help someone. Take care and Have a nice day. Cheers.

peterhanneman commented Jun 9, 2017

GCC 7.1 on Ubuntu 14.04 & 16.04:

sudo apt-get update -y && \
sudo apt-get upgrade -y && \
sudo apt-get dist-upgrade -y && \
sudo apt-get install build-essential software-properties-common -y && \
sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y && \
sudo apt-get update -y && \
sudo apt-get install gcc-7 g++-7 -y && \
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

Work very well for me.

Thanks!

Owner

application2000 commented Jun 22, 2017

@peterhanneman Thank you for the update. Take care

k-five commented Jun 24, 2017

thank you ivanmara555 worked on 16.04 LTS..
Sat Jun 24 2017 GMT

hanzin commented Jun 27, 2017

thank you , this work very well !

jacobtylerdavis commented Jul 14, 2017

Thank you! This works on Debian as well but instead of adding the Ubuntu repo you can add deb http://ftp.us.debian.org/debian testing main contrib non-free to the end of /etc/apt/sources.list

Thanks, this worked for me on Ubuntu 16.04.

Owner

application2000 commented Jul 30, 2017

Thanks for the tip @1Forge. Thank you all for your comments. Hope it helps everybody. Happy coding!

jlettvin commented Aug 14, 2017

Thank you peterhanneman. Your one-liner worked perfectly.

This is gold dust! Many thanks.

komeilr commented Aug 24, 2017

Bro are you serious right now?! This worked like a fucking dream!

Thank you, solved an issue on Ubuntu 17.04

Thank you very much. Upgraded from 5 to 6.3

BeErikk commented Sep 17, 2017

Thanks, confirmed works on WSL Ubuntu 16.04

worked fine! Thanks a lot :)

Excellent! Still works from 4.9.3 to 6.3! Very helpful thank you!

excellent! thanks very much!

Thank you! Such good directions. Worked like a charm.

It works. Thanks!

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