35

I want to try C++17 features and I want to install standard compliant compiler (preferably GCC). I'm totally new to Linux and Ubuntu and I simply don't understand a lot.

I tried to follow https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test but with no luck. First I ran:

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

As I understood this command installs some keys. Next I tried:

sudo add-apt-repository ppa:ubuntu-toolchain-r/gcc-7

That returned:

Error: 'ppa:ubuntu-toolchain-r/gcc-7' invalid

Next I tried to install clang development branch:

apt-get install clang-4.0 lldb-4.0

And it gives me

E: Unable to locate package clang-4.0
E: Couldn't find any package by glob 'clang-4.0'
E: Couldn't find any package by regex 'clang-4.0'
E: Unable to locate package lldb-4.0
E: Couldn't find any package by glob 'lldb-4.0'
E: Couldn't find any package by regex 'lldb-4.0'

What does all this means? What's wrong?

Eliah Kagan
  • 117,780
  • 1
    The add-apt-repository command you ran is not the same as the one given on the PPA's page, so it's no wonder it doesn't work. Try running the correct one. – fkraiem Dec 10 '16 at 21:30
  • I installed test package first. Updated the question. – nikitablack Dec 10 '16 at 21:43
  • add-apt-repository as its name implies adds a repository, it does not install a package. Now you can do apt install gcc-7. – fkraiem Dec 10 '16 at 21:46
  • The test PPA has a gcc-7 package in it. Have you not tried to install that? – dobey Dec 10 '16 at 21:46
  • I ran sudo apt install gcc-7 command and got Unable to locate package gcc-7. – nikitablack Dec 11 '16 at 09:16
  • 2
    @nikitablack Looks like gcc 7 is not available for 16.04: https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test?field.series_filter=xenial But you might have a better chance with clang, look at the base of this page: http://apt.llvm.org/ – andrew.46 Dec 11 '16 at 09:40
  • @andrew.46 Now I see that it's not available. Unfortunately I have the same problems with clang. – nikitablack Dec 11 '16 at 10:07
  • I'm using the LLVM source.list and it's the same error messages. Is it possible that clang-4.0 removed from the souce lists, both on LLVM.org and Ubuntu repository? http://apt.llvm.org/ – Lanti Dec 27 '16 at 14:20
  • Hi! I'm a person from the future! The first two lines you tried seem to be sufficient on Ubuntu 17.04. – geometrian May 10 '17 at 07:54

4 Answers4

33

Super mega GCC table for all Ubuntu versions: How do I use the latest GCC on Ubuntu?

Ubuntu 16.04 and below

There is an official Ubuntu GCC test PPA which should be preferred:

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

GCC 7 was release in May 2017, so too late for 17.04 main release.

The PPA does not currently have GCC for newer releases e.g. 16.10, only LTS 12.04, 14.04 and 16.04, as can be seen at: https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test | snapshot. I think it had 17.04 previously but it was removed? See also: https://unix.stackexchange.com/questions/371737/install-gcc-7-on-ubuntu

Tested on Ubuntu 16.04, October 2018.

Ubuntu 17.10 and above

Has GCC 7.2 and clang 4 by default! https://packages.ubuntu.com/artful/gcc | https://packages.ubuntu.com/artful/clang

$ gcc --version
gcc (Ubuntu 7.2.0-8ubuntu3) 7.2.0
$ clang --version  
clang version 4.0.1-6 (tags/RELEASE_401/final)

GCC 8 on 16.04

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

gives 8.1.0 as of 2018-11.

Default in Ubuntu 18.04:

Crosstool-NG

If you are really serious about this, compile and use your own GCC with Crosstool-NG as explained at: https://stackoverflow.com/questions/847179/multiple-glibc-libraries-on-a-single-host/52454603#52454603

This will allow you to use a wide variety of GCC versions on a wide variety of Ubuntu versions without downloading blobs from PPAs you don't necessarily trust.

  • 2
    upvoted! i am grateful for this help. after these steps I also needed to run update-alternatives as described here: https://askubuntu.com/a/980495/231504 – pestophagous Dec 16 '19 at 21:15
26

You can already install gcc-7 and g++-7 from this package.

sudo add-apt-repository ppa:jonathonf/gcc-7.1
sudo apt-get update
sudo apt-get install gcc-7 g++-7
Jendas
  • 788
2

OP asks for "how to install...". Alternatively, how to compile Clang 4.0.

You may compile from the source code using the script from Microsoft ChakraCore's GitHub repository.

wget https://raw.githubusercontent.com/Microsoft/ChakraCore/master/tools/compile_clang.sh

Update LLVM_VERSION="3.9.1" at line 7 to LLVM_VERSION="4.0.0"

sudo ./compile_clang.sh

It will download and compile Clang 4.0 (and whole compiler toolchain) with LLVM Gold support.

muru
  • 197,895
  • 55
  • 485
  • 740
0

Here my step to install gcc-7/g++-7 on Ubuntu 16.04 LTS AND make it the default.

First to install gcc-7/g++-7 using the official ppa.

sudo apt update -qq
sudo apt install -yq software-properties-common
sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
sudo apt update -qq
sudo apt install -yq g++-7

with:

  • -y: auto confirm
  • -q: quiet mode (several increase the quiet level)

Then make it the default alternative

sudo update-alternatives \
 --install /usr/bin/gcc gcc /usr/bin/gcc-7 60 \
 --slave /usr/bin/g++ g++ /usr/bin/g++-7 \
 --slave /usr/bin/gcov gcov /usr/bin/gcov-7 \
 --slave /usr/bin/gcov-tool gcov-tool /usr/bin/gcov-tool-7 \
 --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

now running gcc --version should return gcc 7.4.0 IIRC

Mizux
  • 101