80

How do I install the latest version of gcc and g++?

I am using a variety of C++11 and C++1y features; which is why I need this.

muru
  • 197,895
  • 55
  • 485
  • 740
A T
  • 2,528
  • 1
    @muru Except my question was months earlier. So his is a duplicate of mine. – A T Mar 26 '18 at 05:59
  • 1
    Duplicate independent of time? - That's a weird policy. - And a bit of a misnomer? – A T Mar 26 '18 at 06:02
  • Also I would argue that the answers to my question are of a higher quality and greater heterogeneity than his. Number of votes are comparable also. So I am calling for the merging of his into mine, rather than the reverse. – A T Mar 26 '18 at 06:05

7 Answers7

110

You can install close to upstream version of GCC from Ubuntu Toolchain PPA: https://wiki.ubuntu.com/ToolChain#PPA_packages

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install g++-4.9

Tested on Ubuntu 14.04.

  • 4
    I have Ubuntu 14.04 and that didn't work for me. Did you leave a step out? I don't see g++-4.9 in https://launchpad.net/~ubuntu-toolchain-r/+archive/test – test May 06 '14 at 06:27
  • test, I checked just now in Docker container: – Vladimir Rutsky May 08 '14 at 21:13
  • 1
    test, should work: https://gist.github.com/rutsky/bc40c6b4bee0ab5f9ee4 – Vladimir Rutsky May 08 '14 at 21:20
  • I think listed here https://launchpad.net/~ubuntu-toolchain-r/+archive/test packages are source packages, and there is gcc-4.9, from which g++-4.9 package is being built. You can ensure that .deb file is there here: http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu/pool/main/g/gcc-4.9/ – Vladimir Rutsky May 08 '14 at 21:22
  • Thanks, I followed the directions in your gist to get it working. I really appreciate it. – test May 10 '14 at 05:20
  • 9
    But it doesn't work for me on 12.04: E: Unable to locate package gcc-4.9 E: Couldn't find any package by regex 'gcc-4.9' Looks like 12.04 was specifically is not supported by this repository, even though both newer and older versions of Ubuntu are supported. – Nickolai Leschov May 16 '14 at 02:56
  • @NickolaiLeschov Did you find a workaround to force an installation? I'm running into the same issue. – mavam May 24 '14 at 17:40
  • 1
    @Matthias Looks like gcc-4.9 is only available for Ubuntu 14.04. For other versions, the latest available version is gcc-4.8. I googled arround and found no ppa's of gcc-4.9 for prior versions of Ubuntu. There's always an option to compile it from source, but from reading the instructions I gathered that it is a daunting process for the unexperienced like me, and abandoned this idea. – Nickolai Leschov May 25 '14 at 20:18
  • 1
    Don't forget to apt-get update after adding the PPA! – Vincent Robert May 28 '14 at 14:13
  • 9
    As a last step, I found it necessary to change the symbolic link /usr/bin/g++ from a target of /usr/bin/g++-4.8 to a target of /usr/bin/g++-4.9. ...or leave it unchanged but invoke g++-4.9 directly. Finally: std::regex support. – DavidO Jun 17 '14 at 19:43
  • @DavidO Thanks for that! You saved me some digging.. – quant Sep 14 '14 at 09:54
  • 3
    @DavidO - You should be able to use the alternatives system to avoid directly editing symlinks. See http://askubuntu.com/q/26498/5682. – Josh Kelley Nov 07 '14 at 14:07
  • 1
    Worked like a charm on Ubuntu 14.04 – Mythul Mar 18 '15 at 18:16
  • If you need it just for compilation of a package from source you can set env variable CXX=/usr/bin/g++-4.9 to say which compiler your configure script should use. So you can configure script via CXX=/usr/bin/g++-4.9 ./configure --prefix=/usr/local. This way you don't need to play with alternatives or symlinks – walkeros Sep 03 '15 at 06:30
  • @NickolaiLeschov you'll have to install gcc-4.9 first. see this post : http://askubuntu.com/a/581497/378854 – phil294 Mar 24 '16 at 22:15
  • Package 'g++-4.9' has no installation candidate. Please update. – Jon Deaton Jul 05 '18 at 23:17
43

Intro

teach a man to fish etc. etc.

How to build the source package and serve it from a local repository in an apt-friendly way. This will work on any distribution provided that dependencies can be satisfied/ This method does not require you to install build-deps for every package and polluting your machine with extraneous packages, this will let you keep up with packages as they get updated in debian experimental. It takes just a few minutes to do this once, and then can be reused to rebuild any package you need without relying on other people to package ppa for you or downloading a bunch of different .debs

The benefit of building vs. pulling raw .debs from debian is that this will build packages against the packages in your distribution which may differ in version/revision from what is used as build-dependencies for the debian distribution. This is more-or-less the process for backporting packages. You can also use any ubuntu distribution to build packages targeted at any other distribution (target in this case means build against the standard repository packages) with no hassle.

Basic-How-to-Build-a-deb

(not for ppa uploading - this has beurocratic requirements from launchpad

Probably-required: packaging-dev (pulls build-essential pubilder ubuntu-dev-tools among others)

  1. Set up pbuilder (this lets you build a package in a chroot without polluting your system with build-dependency packages)

    • sudo pbuilder create,
    • if you want to build for a specific distribution, (pbuilder uses the build system release in a chroot) you can use pbuilder-dist [precise/oneric/trusy/etc...] create
  2. Get debian source

    • pull-debian-source gcc-4.9 [4.9.0-6] specific debian revision is optional, but can be useful if you want to pull experimental/unstable/testing/stable revisions
    • you can also pull from specific ubuntu distros by adding them to sources.list as a deb-src and using sudo apt-get src
  3. Build Package

    • sudo pbuilder build gcc-4.9_4.9.0-6.dsc
    • In the files downloaded there is a .dsc file, for the most recent gcc it is gcc-4.9_4.9.0-6.dsc which is a package descriptor file. .orig.tar.[gz/xz] is the source tarball.
  4. Create local Apt-repository

    • mkdir /convenient/place/for/repo
    • cp /var/cache/pbuilder/result/* /path/to/repo
    • (assuming you are in repo dir) apt-ftp archive packages . > Packages
    • (one of many ways to do this) sudo echo "deb [trusted=yes] file:/local/repo/Packages ./" > /etc/apt/sources.list.d/gcc-repo.list
    • Note you can also do this step with .debs downloaded from anywhere (skip step 1-3)
  5. Install

    • apt-get update; apt-get install gcc-4.9 g++-4.9

Extra Tricks

Easy backporting Dependencies

Using self-compiled packages to satisfy dependencies when building packages. (I have it set up with folders ~/pbuilder ~/pbuilder/precise_local (local package repo for precise) and ~/pbuilder/precise_hooks (for hook scripts) )

Add the following to your ~/.pbuilderrc

OTHERMIRROR="deb [trusted=yes] file:///home/user/pbuilder/precise_local ./"

BINDMOUNTS="/home/user/pbuilder/precise_local" 

HOOKDIR="/home/user/pbuilder/precise_hooks"

EXTRAPACKAGES="apt-utils"

in precise_hooks create a file D05local (in typical unix/linux fashion, prefix D tells when script is hooked 05 is self-imposed name-ordering and local is just the name, if you only have one hook it is not important what its called as long as D is the prefix

the script is a one-liner

(cd /home/user/pbuilder/precise_local ; apt-ftparchive packages . > Packages)

Now any packages placed in precise_local will satisfy build-depends. This is supremely useful to construct a dependency tree locally when back-porting packages that have dependencies that also need backporting

The VM solution

To do this in an even cleaner way, use a VM image or LXC container to jail this mess.

Applying custom patches

you can apply custom patches in most debian packages using quilt, quilt patches can use diffs from most VCSs (see : using quilt )

There is an additional step, you must rebuild the .dsc and .debian.tar.gz. Cleanest-way I know is bzr-builddeb it has the highest success rate IMHO (compared to git-build-package and other helper scripts) and is much cleaner than calling debuild directly (bzr= bazaar canonical's VCS)

  1. sudo apt-get install bzr-builddeb
  2. (assuming .orig.tar.gz is extracted and .debian.tar.gz is extracted and place in it
    • bzr init
    • bzr add
    • bzr commit
  3. (OPTIONAL)
    • modify debian/changelog
    • add patch to debian/patches/ and modify debian/patches/series (quilt also has utility to add patches or for you to modify on the fly, see documentation)
    • bzr add debian/
    • bzr commit
  4. bzr builddeb -- -S -us -uc This rebuilds the source file and leaves it unsigned (gpg signing required for PPA/distro uploading, but not for private local repos)
  5. cd ../build-area/ Continue from Step 3 above.

Steps 1-4 here are pretty much what you need to upload to a PPA (they do not take binary files), but you require some steps to satisfy launchpad bureaucracy (this answer provides a explanation, this one has some links)

crasic
  • 3,842
27

install

sudo su -
apt-get install build-essential
add-apt-repository ppa:ubuntu-toolchain-r/test
apt-get update
apt-get install gcc-4.9 g++-4.9 cpp-4.9

after that if you check the version of gcc you will find the old version

gcc --version

so we can fix it with simple symbolic

cd /usr/bin
rm gcc g++ cpp
ln -s gcc-4.9 gcc
ln -s g++-4.9 g++
ln -s cpp-4.9 cpp

or

you can update using update-alternatives

// Actually i tried the symbolic & i know this will work but you may use the symbolic to get it without problems // please correct me if I am wrong

update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 40 --slave /usr/bin/gcc gcc /usr/bin/gcc-4.9
update-alternatives --config gcc
update-alternatives --install /usr/bin/cpp cpp /usr/bin/cpp-4.9 40 --slave /usr/bin/cpp cpp /usr/bin/cpp-4.9
update-alternatives --config cpp
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.9
update-alternatives --config g++

or

you can just compile with
/usr/bin/gcc4.9 filename.c

gcc-4.9 is just like the gcc-4.8 "ubuntu 14.04 gcc" it is not a big deference

proof

Drew Noakes
  • 5,708
hwez
  • 2,976
  • 1
  • 17
  • 15
8

Debian offers a package: https://packages.debian.org/jessie/gcc-4.9 - To prevent causing issues with Ubuntu/Debian compatibility; don't add it to your sources.list.

Instead, simply download all these:

binutils_2.25-5_amd64.deb
cpp_4.9.2-2_amd64.deb
g++_4.9.2-2_amd64.deb
gcc_4.9.2-2_amd64.deb
gcc-4.9-base_4.9.2-10_amd64.deb
libasan1_4.9.2-10_amd64.deb
libatomic1_4.9.2-10_amd64.deb
libcilkrts5_4.9.2-10_amd64.deb
libgcc1_4.9.2-10_amd64.deb
libgcc-4.9-dev_4.9.2-10_amd64.deb
libgomp1_4.9.2-10_amd64.deb
libitm1_4.9.2-10_amd64.deb
liblsan0_4.9.2-10_amd64.deb 
libmpfr4_3.1.2-2_amd64.deb
libquadmath0_4.9.2-10_amd64.deb
libstdc++-4.9-dev_4.9.2-10_amd64.deb
libstdc++6
libtsan0_4.9.2-10_amd64.deb
libubsan0_4.9.2-10_amd64.deb

From Debian's servers, e.g.:

http://ftp.us.debian.org/debian/pool/main/b/binutils/binutils_2.25-5_amd64.deb

Then install them, e.g.: $ for package in *.deb; do sudo dpkg --install "$package"; done

[Easier just to run that bash loop a few times until all the dependencies are covered than remembering the order you downloaded them in!]

A T
  • 2,528
2

Currently (20141102) adding the PPA and invoking the above command apt-get install gcc-4.9 g++-4.9 cpp-4.9 will print out a slew of suggested packages. Install them all except for libvtv0-dbg (which has a dependency conflict):

apt-get install gcc-4.9 g++-4.9 cpp-4.9 gcc-4.9-locales g++-4.9-multilib libstdc++6-4.9-dbg gcc-4.9-multilib libgcc1-dbg libgomp1-dbg libitm1-dbg libatomic1-dbg libasan1-dbg liblsan0-dbg libtsan0-dbg libubsan0-dbg libcilkrts5-dbg libquadmath0-dbg lib32stdc++6-4.9-dbg libx32stdc++6-4.9-dbg

(skip the -dbg packages if they're of no interest to you), and then do an apt-get dist-upgrade --auto-remove. That last step will basically replace your gcc-4.8 with gcc-4.9 (but not uninstall the older version), and pull in up-to-date fortran compilers at the same time.

muru
  • 197,895
  • 55
  • 485
  • 740
RJVB
  • 211
  • 2
  • 10
2

I had to force the version for the gcc-4.9-base package to get past the error message you posted. Then installing worked

sudo apt-get install gcc-4.9-base=4.9.2-0ubuntu1~14.04

Now you can do the regular

sudo apt-get install gcc-4.9
  • In my case I had to force a specific version of libstdc++6, which is a dependency. For some reason it wanted to use an older version which it thought was a newer version, so it would not upgrade without forcing. – Emre Jan 18 '16 at 06:06
2

Install GCC GNU 4.9

sudo add-apt-repository ppa:ubuntu-toolchain-r/test

sudo apt-get update

sudo apt-get install gcc-4.9

Remove the previous gcc version from the default applications list (if already exists)

sudo update-alternatives --remove-all gcc

Make GCC 4.9 the default compiler on the system

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 20

sudo update-alternatives --config gcc

The latest update GCC 4.9.1 is a bug-fix release from the GCC 4.9 branch containing important fixes for regressions and serious bugs in GCC 4.9.0 with more than 88 bugs fixed since the previous release. In addition to that, GCC 4.9.1 release supports OpenMP 4.0 also in Fortran, rather than just in C and C++

Note : Checked on ubuntu 14.04 LTS

Swap L
  • 129