41

GCC 11.1 was finally released yesterday. However, now it can only be built from source, so I'm wondering when we can get it with apt?

gavrilikhin.d
  • 554
  • 1
  • 7
  • 20
  • 2
    It has been added by default now.: https://packages.ubuntu.com/hirsute/gcc-11 Added it to my megatable: https://askubuntu.com/questions/466651/how-do-i-use-the-latest-gcc-on-ubuntu/1163021#1163021 – Ciro Santilli OurBigBook.com Aug 29 '21 at 08:15
  • 6
    I’m voting to close this question because it belongs on https://askubuntu.com/ – bad_coder Aug 29 '21 at 20:19
  • Fast forward 1 year and new LTS `ubuntu:jammy` comes with `gcc-11` and `g++-11` (see https://packages.ubuntu.com/search?keywords=gcc-11). – mirekphd May 01 '22 at 10:21

4 Answers4

60
sudo apt install build-essential manpages-dev software-properties-common
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update && sudo apt install gcc-11 g++-11

Then use update-alternatives to set default gcc...

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 --slave /usr/bin/g++ g++ /usr/bin/g++-9 --slave /usr/bin/gcov gcov /usr/bin/gcov-9 --slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-9 --slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-9  --slave /usr/bin/cpp cpp /usr/bin/cpp-9 && \

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 110 --slave /usr/bin/g++ g++ /usr/bin/g++-11 --slave /usr/bin/gcov gcov /usr/bin/gcov-11 --slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-11 --slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-11  --slave /usr/bin/cpp cpp /usr/bin/cpp-11;

To sample check settings to see which gcc is default you can run the following, if they show correct resuslts then the rest are fine...

gcc --version;g++ --version;gcov --version;

To reconfigure to any previous gcc version...

sudo update-alternatives --config gcc

You can do this on any version of ubuntu,... enjoy!

Here are my 6 different gcc's living side by side with the default being gcc-11:

$ sudo update-alternatives --config gcc
There are 6 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-11   1010      auto mode
  1            /usr/bin/gcc-10   1000      manual mode
  2            /usr/bin/gcc-11   1010      manual mode
  3            /usr/bin/gcc-5    40        manual mode
  4            /usr/bin/gcc-7    700       manual mode
  5            /usr/bin/gcc-8    800       manual mode
  6            /usr/bin/gcc-9    900       manual mode

Press <enter> to keep the current choice[*], or type selection number:
Aaron
  • 2,344
  • 3
  • 26
  • 32
DanglingPointer
  • 891
  • 1
  • 5
  • 12
  • doesn't work, unfortunately – Jacko May 20 '21 at 15:29
  • 3
    Works for me that's why I wrote it. Perhaps you should elaborate to get more help. – DanglingPointer May 21 '21 at 16:27
  • Thank you very much, but I think there is an error in your second line of ``update_alternatives``. 100 has to 110, hasn't? At least that seems to fix it for me. – DNKpp May 23 '21 at 21:04
  • The number is an integer denoting priority. The higher it is, the more priority given when using auto-resolution. Run.... $ sudo update-alternatives --config gcc and you'll see one of the columns is "Priority". It can be any number as long as it is not a number already used. The higher the number the higher the priority in "auto" mode. – DanglingPointer May 25 '21 at 11:31
  • I have updated the answer showing my update-alternatives config managing 6 different gcc's – DanglingPointer May 25 '21 at 11:48
  • 2
    The "doesn't work" comment may be due to a missing `sudo apt update` after adding the repo. – Drew Dormann Jun 16 '21 at 21:20
  • Good pick up @DrewDormann! I have updated the command above. – DanglingPointer Jun 18 '21 at 08:07
  • I have updated the alternatives install line to the complete set of gcc suite of main commands. – DanglingPointer Jun 18 '21 at 08:15
  • 1
    It doesn't work for me either. It adds the ppa and also lists it while updating the package list but then it does not find any g++-11 nor gcc-11 package to install. I'm using Ubuntu 20.10 (groovy), could that be the reason? – mxmlnkn Jun 20 '21 at 18:09
  • 12
    how to fix error: alternative cpp can't be slave of gcc: it is a master alternative? – johndpope Jul 03 '21 at 03:36
  • 6
    Run ```sudo update-alternatives --config cpp``` and see if it is already configured with different options, if not then remove it using ```sudo update-alternatives --remove-all cpp```. Then if you had already ran the command I posted for gcc, you will need to remove that as well to start fresh, then re-copy and paste the command above to set all of the slaves including cpp appropriately. – DanglingPointer Jul 03 '21 at 13:50
  • Removing your line @johndpope as adding hirsuite universe can potentially break a lot of Focal packages if left in place. The toolchain-r ppa doesn't. – DanglingPointer Aug 22 '21 at 22:05
  • 1
    I get the same error as @johndpope: `update-alternatives: error: alternative cpp can't be slave of gcc: it is a master alternative` – thargy Sep 07 '21 at 16:32
  • @thargy - see my response to johndpope's comment. It's an easy fix. – DanglingPointer Sep 08 '21 at 22:43
  • Don't forget to add the man pages as well, as their contents are version-dependent – Alex O Oct 27 '21 at 23:36
15

On Ubuntu 20.04, I followed the instructions here:

Which is to:

  1. Update the listed mirrors by adding a line to your /etc/apt/sources.list like this:

    sudo add-apt-repository 'deb http://mirrors.kernel.org/ubuntu hirsute main universe'

    Choose a mirror based on your location from the list. I chose the kernel mirror as I am in North America.

  2. sudo apt-get update

  3. sudo apt-get install gcc-11

After that which gcc-11 should produce a path to gcc-11. On my machine it was:

which gcc-11

produces: /usr/bin/gcc-11

johndpope
  • 5,035
  • 2
  • 41
  • 43
William Roberts
  • 759
  • 6
  • 12
  • 8
    Be very careful, don't run 'apt-get upgrade' or your 20.04 becomes 21.04, it's safer to remove the sources.list line after gcc-11 is installed so you don't ended up upgrading all your packages to a new ubuntu release by accident, which could take quite a while. – Shawn Shaw Jun 25 '21 at 14:34
  • 4
    Is this totally safe? If adding the hirsute repo makes available GCC-11, shouldn't we expect that to make available a lot of other, newer packages that are possibly incompatible with 20.04? – Silicomancer Jul 21 '21 at 22:32
  • 8
    This is not a safe answer as it could potentially create a mess of packages in Focal which are meant to be Focal and not Hirsuite. If you want Hirsuite versions of stuff in Focal (e.g. dwarves or libbpf for building the latest kernels), best to rebuild them in focal using GCC and the C library in Focal. Otherwise upgrade to Hirsuite or later. – DanglingPointer Aug 25 '21 at 11:00
  • 4
    Yep, that totaly destroyed my distro >D – Namal Sep 07 '21 at 08:08
  • Caution: a double-digit number of downvotes (b/c it leads to a mixed-versions system...) – mirekphd May 01 '22 at 10:16
7

We currently have no info of when we'll see an APT release of GCC 11.1. Below, I show the step-by-step build instructions that you can hopefully follow along:

You can visit https://gcc.gnu.org/mirrors.html, choose the closest mirror to you, download the source for gcc-11.1.0.tar.gz

Then, make sure to have your build system installed:

sudo apt install gcc g++ make bison binutils gcc-multilib

Yes, you need gcc to build gcc.

Then, unpack the tarball:

cd Downloads # replace with your download location
tar -xzvf gcc-11.1.0.tar.gz
cd gcc-11.1.0

The last thing is to actually build it:

mkdir build
cd build
../configure --enable-multilib && make && sudo make install

That's all! You now have GCC 11.1 installed in Ubuntu.

  • I tried similar but had some problems with linking libstdc++.so.6. – gavrilikhin.d Apr 28 '21 at 11:16
  • I forgot to mention that for the `--enable-multilib` option you must have multilib. Install it with `sudo apt install gcc-multilib`. I've added it into the answer as well. –  Apr 28 '21 at 11:57
  • 2
    This can take a veeery long time to build though, depending on the machine. – Namal Sep 29 '21 at 09:46
7

Casachii's instructions worked for me, with one gotcha. If you run into any issues building GCC yourself using the instructions from casachii, such as build errors reporting missing GMP, MPFR, MPC, etc, then simply do the following:

From the GCC directory (the parent of the "build" directory you just created), run:

./contrib/download_prerequisites

Then when it finishes, run this:

./configure

Finally go back into your build directory as casachii instructs and build GCC 11.x

cd build
../configure --enable-multilib && make && sudo make install

This will take quite a long time. BTW - I would have added this in a comment to casachii's very helpful answer, however I just created this account and apparently I don't have the creds to even make a comment.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
OtterFlip
  • 151
  • 2
  • 3