56

This question always bothers me! Last time for 4.9 I tried the commands mentioned in this answer for g++4.9. The same trick doesn't work now:

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

results in...

E: Unable to locate package gcc-5.1
E: Couldn't find any package by regex 'gcc-5.1'
E: Unable to locate package g++-5.1
E: Couldn't find any package by regex 'g++-5.1'

What is the correct command for directly installing the latest g++ in Ubuntu? Is there any magic command which works uniformly for all the updates of g++?

Update:

I found that the correct command to install it is:

sudo apt-get install g++-5

This works now! Though in the GCC official page, the version is 5.1.

iammilind
  • 2,247

3 Answers3

48

check the repo page https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test

It shows version 5 as latest version so you'll not find 5.1 and thus result this error

you should install using (it still version 5 in your given Repository)

sudo apt-get install g++-5

Else if this doesn't work you can download the source code and compile it

Maythux
  • 84,289
  • 6
    This is the correct answer. However, I'd like to add that you may need to sudo ln -s /usr/bin/g++-5 to /usr/bin/g++ so you can call it directly with g++ – Trevor Hickey May 26 '15 at 06:26
  • By now 5.1 is in, too :) – abergmeier Jul 22 '15 at 07:48
  • 2
    g++-5 is g++-5.1. There was never a 5.0 release. I don't know why ubuntu stopped putting the minor version as part of the package name. – teeks99 Jul 23 '15 at 11:53
  • 1
    @TrevorHickey. this command "/usr/bin# ln -s /usr/bin/gcc /usr/bin/gcc-5 ln: failed to create symbolic link ‘/usr/bin/gcc-5’: File exists" not working...any idea how to do this linking operation? – Ashu Jan 24 '16 at 22:00
  • 1
    @Ashu sudo rm -f /usr/bin/gcc since it's already there, and then run the ln command again. – Trevor Hickey Jan 24 '16 at 23:05
  • The repo now mentions gcc-6, but I am unable to download that. Any idea why? – goelakash Apr 05 '16 at 08:31
40

Use the following command I found in here

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

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 60 --slave /usr/bin/g++ g++ /usr/bin/g++-5
Po-Yu Kao
  • 501
4

Currently the ubuntu repo skips the minor version naming. When I installed the gcc-5 and g++-5 packages, the version that gets installed on my computer is 5.2.1. Well I feel it is as latest as you can get without compiling by yourself. So do not worry about the 5 it is the latest version.