Instantly share code, notes, and snippets.

Embed
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
@mdhanasar

This comment has been minimized.

mdhanasar commented Jun 28, 2016

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

@GHnubsST

This comment has been minimized.

GHnubsST commented Jul 17, 2016

worked perfectly thanks.

@AnanthanM

This comment has been minimized.

AnanthanM commented Aug 16, 2016

Thank You

@yech1990

This comment has been minimized.

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

This comment has been minimized.

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!

@andrewrech

This comment has been minimized.

andrewrech commented Nov 28, 2016

Very helpful, thank you

@jwarlander

This comment has been minimized.

jwarlander commented Dec 1, 2016

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

This comment has been minimized.

Sakib37 commented Dec 2, 2016

Thanks. Worked perfectly. :)

@ArulrajAnto

This comment has been minimized.

ArulrajAnto commented Dec 4, 2016

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.

@ArulrajAnto

This comment has been minimized.

ArulrajAnto commented Dec 4, 2016

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

@HungryHowies

This comment has been minimized.

HungryHowies commented Feb 2, 2017

Good Stuff!! thank you worked great on Ubuntu 16.0.4

@AraHaan

This comment has been minimized.

AraHaan commented Feb 17, 2017

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

@KyCodeHuynh

This comment has been minimized.

KyCodeHuynh commented Mar 7, 2017

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.

@S0ulR3av3R

This comment has been minimized.

S0ulR3av3R commented Mar 16, 2017

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

@talland

This comment has been minimized.

talland commented Mar 16, 2017

Nice, thank you very much

@jasonbeach

This comment has been minimized.

jasonbeach commented Mar 25, 2017

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

@smac89

This comment has been minimized.

smac89 commented Apr 20, 2017

This is great
Gcc version 6.2.0

@zheive

This comment has been minimized.

zheive commented Apr 21, 2017

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

Thanks!

@ivanmara555

This comment has been minimized.

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

@application2000

This comment has been minimized.

Owner

application2000 commented May 30, 2017

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

@peterhanneman

This comment has been minimized.

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

This comment has been minimized.

toffyjan commented Jun 21, 2017

Work very well for me.

Thanks!

@application2000

This comment has been minimized.

Owner

application2000 commented Jun 22, 2017

@peterhanneman Thank you for the update. Take care

@k-five

This comment has been minimized.

k-five commented Jun 24, 2017

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

@hanzin

This comment has been minimized.

hanzin commented Jun 27, 2017

thank you , this work very well !

@jacobtylerdavis

This comment has been minimized.

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

@smutandwa

This comment has been minimized.

smutandwa commented Jul 16, 2017

Thanks, this worked for me on Ubuntu 16.04.

@application2000

This comment has been minimized.

Owner

application2000 commented Jul 30, 2017

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

@jlettvin

This comment has been minimized.

jlettvin commented Aug 14, 2017

Thank you peterhanneman. Your one-liner worked perfectly.

@paberline

This comment has been minimized.

paberline commented Aug 21, 2017

This is gold dust! Many thanks.

@komeilr

This comment has been minimized.

komeilr commented Aug 24, 2017

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

@NemoDomum

This comment has been minimized.

NemoDomum commented Sep 7, 2017

Thank you, solved an issue on Ubuntu 17.04

@justinjk007

This comment has been minimized.

justinjk007 commented Sep 8, 2017

Thank you very much. Upgraded from 5 to 6.3

@BeErikk

This comment has been minimized.

BeErikk commented Sep 17, 2017

Thanks, confirmed works on WSL Ubuntu 16.04

@SwapnilBorse123

This comment has been minimized.

SwapnilBorse123 commented Sep 18, 2017

worked fine! Thanks a lot :)

@MostertMB

This comment has been minimized.

MostertMB commented Oct 7, 2017

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

@siebeniris

This comment has been minimized.

siebeniris commented Nov 10, 2017

excellent! thanks very much!

@amendment19

This comment has been minimized.

amendment19 commented Nov 16, 2017

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

@RianWardana

This comment has been minimized.

RianWardana commented Nov 28, 2017

It works. Thanks!

@vikchopde

This comment has been minimized.

vikchopde commented Dec 30, 2017

Thanks .Very helpfull !!

@zixuan75

This comment has been minimized.

zixuan75 commented Jan 1, 2018

This is not correct. if i type "sudo apt-get install gcc-6 g++-6 -y" command, it says "E: Unable to locate package gcc-6" and then "E: Unable to locate package g++-6" and then "E: Couldn't find any package by regex 'g++-6'".

@thinkjrs

This comment has been minimized.

thinkjrs commented Jan 11, 2018

Thanks @peterhanneman!

@NickWoodhams

This comment has been minimized.

NickWoodhams commented Jan 25, 2018

Great work. 4.8 worked fantastically to compile mysql-python 1.2.5. Thanks!

@cryptomaniac26

This comment has been minimized.

cryptomaniac26 commented Feb 2, 2018

Awesome walk through. In detail and no nonsense! Thank you

@awwsmm

This comment has been minimized.

awwsmm commented Feb 16, 2018

Works great! Thanks.

@bryceschober

This comment has been minimized.

bryceschober commented Mar 5, 2018

@starjasmine

This comment has been minimized.

starjasmine commented Mar 8, 2018

That article is really informative! Thanks.

@stefson

This comment has been minimized.

stefson commented Mar 9, 2018

This might not be the right place to ask for it, but is there a possibility to install llvm + clang 5 toolchain for ubuntu 14.04 for armhf? I found ppa which has x86 and amd64, also 16.04 and newer have it in their repos? gcc is a bit buggy on arm, and also very demanding with memory consumption -_- where clang is just a blast. Faster in compiling, memory cut down by 50 to 80% (tested with amd64 and firefox), and also faster binaries.

Will llvm-4 and llvm-5 be added to trusty, as firefox seems to now depend on at least llvm-4?

@JorgeFrancoIbanez

This comment has been minimized.

JorgeFrancoIbanez commented Mar 16, 2018

It works, for me when I've tried to install strongswan from source code! 👍

@Lakshmikanthaks

This comment has been minimized.

Lakshmikanthaks commented Mar 18, 2018

It works, thank you :)

@heartonbit

This comment has been minimized.

heartonbit commented Mar 20, 2018

Thanks, it works!

@ODYTRON

This comment has been minimized.

ODYTRON commented Mar 27, 2018

Works like a charm!

@rkosti

This comment has been minimized.

rkosti commented Apr 11, 2018

works perfectly!

@ivanmara555

This comment has been minimized.

ivanmara555 commented Apr 25, 2018

GCC 8.1.0 on Ubuntu 14.04 & 16.04 & 18.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-8 g++-8 -y &&
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 60 --slave /usr/bin/g++ g++ /usr/bin/g++-8 &&
sudo update-alternatives --config gcc

select gcc-8

@cortical-iv

This comment has been minimized.

cortical-iv commented Apr 27, 2018

ivanmara555 thanks that works great on my Ubuntu 16 install!

@dustars

This comment has been minimized.

dustars commented May 2, 2018

THANK YOU! I have been searching for a solution nearly 10 hours, and finally your method works for me!

@ivanmara555

This comment has been minimized.

ivanmara555 commented May 3, 2018

method above now install gcc 8.1.0 ... Congratulations!)

@F001

This comment has been minimized.

F001 commented May 8, 2018

@ivanmara555 , I used above command, but only gcc 8.0.1 is installed (not 8.1.0).

gcc --version
gcc (Ubuntu 8-20180424-0ubuntu1~16.04.1) 8.0.1 20180424 (experimental) [trunk revision 259590]

My platform is Ubuntu 16.04.4 LTS. Could you please help to point out what's wrong?

@adrien1018

This comment has been minimized.

adrien1018 commented May 10, 2018

@F001 I got into the same situation on Ubuntu 14.04.5 LTS.

@ghost

This comment has been minimized.

ghost commented May 12, 2018

Many thanks.. it works greats :)

@nikunjsoni24

This comment has been minimized.

nikunjsoni24 commented May 14, 2018

Worked, Thank you very much

@roshanashraf

This comment has been minimized.

roshanashraf commented May 19, 2018

thanks

@bobuntu

This comment has been minimized.

bobuntu commented May 21, 2018

Nicely done and much appreciated. Thank you @ivanmara555 et al.

@MaitreManuel

This comment has been minimized.

MaitreManuel commented Jul 17, 2018

Love you sir, it worked !

@erictarn

This comment has been minimized.

erictarn commented Aug 9, 2018

sudo apt-get install python-software-properties needed for Ubuntu 12.04 in order to run add-apt-repository

@seanshpark

This comment has been minimized.

seanshpark commented Aug 13, 2018

Thank you!

@seergjej

This comment has been minimized.

seergjej commented Aug 22, 2018

http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu/dists/jessie/main/binary-amd64/Packages 404 Not Found

The whole jessie folder is gone. All other distros are there.

@88plug

This comment has been minimized.

88plug commented Aug 27, 2018

Here is the Docker friendly format

FROM ubuntu:18.04
RUN apt-get update ; apt-get upgrade -y ; apt-get install -y gcc-8 g++-8 && \
  update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 60 --slave /usr/bin/g++ g++ /usr/bin/g++-8 && \
  update-alternatives --config gcc

also available on https://hub.docker.com/r/88plug/gcc-8-ubuntu-18.04/

@sorgina13

This comment has been minimized.

sorgina13 commented Sep 23, 2018

Thank you!

@UsmanMaqbool

This comment has been minimized.

UsmanMaqbool commented Oct 2, 2018

thanks a lot :)

@KingOfMezi

This comment has been minimized.

KingOfMezi commented Nov 4, 2018

hi...
guide to install the update of GCC 8.2 or GCC 9.0 in Ubuntu 14.04 and 16.04 ???

@bin-y

This comment has been minimized.

bin-y commented Nov 5, 2018

Thanks!

@lisneyc

This comment has been minimized.

lisneyc commented Nov 8, 2018

Thank you @peterhanneman.

@oaix

This comment has been minimized.

oaix commented Nov 14, 2018

Thank you very much @ivanmara555
I test on ubuntu 16.04.5 without upgrading:

sudo apt-get update -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-8 g++-8 -y && 
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 60 --slave /usr/bin/g++ g++ /usr/bin/g++-8 && 
sudo update-alternatives --config gcc

select gcc-8

And the final version of gcc is 8.1.0. @F001
image

@abst0603

This comment has been minimized.

abst0603 commented Nov 27, 2018

Thanks. It works perfectly!

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