how to install gcc-12 on ubuntu
Asked Answered
B

4

34
$ sudo apt search gcc-12
Sorting... Done
Full Text Search... Done
$ uname -a
Linux Han 5.10.81.1-microsoft-standard-WSL2 #1 SMP Mon Nov 22 18:52:15 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

I am using the default sources.list file, I want to install gcc-12 but I can't find it in the mirror source, what should I do!

Baucom answered 24/1, 2022 at 14:39 Comment(4)
Ubuntu, gcc-12 launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test → gcc overview ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu/pool/main/gEldrida
@KnudLarsen gcc-12 is not referenced anywhere in your linksLaux
windows-subsystem-for-linux : There should be a gcc-12 version for Ubuntu 22.04 .... as it is available for the default Ubuntu packages.ubuntu.com/jammy/g++-12 . ..... Ubuntu 22.04 will be released tomorrow.Eldrida
As an aside, you should not need sudo for apt searchIterate
F
45

gcc-12 is not available in ubuntu 20.04, so we need to compile it from source code, here are the steps which I borrowed from this video:

  • Step 1: clone gcc source code and checkout gcc-12 branch
$ git clone https://gcc.gnu.org/git/gcc.git gcc-source
$ cd gcc-source/
$ git branch -a
$ git checkout remotes/origin/releases/gcc-12
  • Step 2: make another build dir

Note this is important as running ./configure from within the source directory is not supported as documented here.

$ mkdir ../gcc-12-build
$ cd ../gcc-12-build/
$ ./../gcc-source/configure --prefix=$HOME/install/gcc-12 --enable-languages=c,c++
  • Step 3: installing GCC prequisites and run configure again

The missing libraries will be shown in above ./confgiure output, search and install them one by one.

$ apt-cache search MPFR
$ sudo apt-get install libmpfrc++-dev
$ apt-cache search MPC | grep dev
$ sudo apt-get install libmpc-dev
$ apt-cache search GMP | grep dev
$ sudo apt-get install libgmp-dev
$ sudo apt-get install gcc-multilib
$ ./../gcc-source/configure --prefix=$HOME/install/gcc-12 --enable-languages=c,c++

An alternartive is to run the download_prerequisites script.

$ cd ../
$ cd gcc-source/
$ ./contrib/download_prerequisites
$ ./../gcc-source/configure --prefix=$HOME/install/gcc-12 --enable-languages=c,c++
  • Step 4: compile gcc-12
$ make -j16

Still flex is missing:

$ sudo apt-get install flex
$ ./../gcc-source/configure --prefix=$HOME/install/gcc-12 --enable-languages=c,c++
$ make -j16
$ make install

Another way is to use Ubuntu 22.04 where gcc-12 is available. In Ubuntu 22.04, gcc-12 can be installed with apt:

$ sudo apt install gcc-12
Faruq answered 1/7, 2022 at 11:18 Comment(7)
I tried it on Kubuntu 20.04. Without download_prerequisites it complained about missing "isl" component. Also I needed to supply configure options --disable-multilib and --with-isl. Then build to succeed. However second build after flex installation failed with "Error 2" and it said I should "make distcean" first. Finally I tried install but it says target install is unknown.Statocyst
Why do you do a second make after installation of flex? Why no installing flex at the beginning and then doing a single make?Statocyst
@Statocyst In my case, in the begining when running ./../gcc-source/configure, it was not complaining missing flex, it complained until make -j16 stage.Faruq
How add gcc-12 to alternative c and c++ compiler under linux?Wolframite
@KhurshidNormuradov sudo update-alternatives --install /usr/bin/gcc gcc ~/install/gcc-12/bin/gcc 120 --slave /usr/bin/g++ g++ ~/install/gcc-12/bin/g++ --slave /usr/bin/gcov gcov ~/install/gcc-12/bin/gcov will do (assuming gcc-12 is installed in ~/install/gcc-12). More info can be referred from linuxize.com/post/how-to-install-gcc-compiler-on-ubuntu-18-04Faruq
Note for those of you building it on a VM: you need quite a lot of space. For me gcc-source folder is 1.9 GB and gcc-12-build ended up 8.2 GB. You also need sufficient swap space (see askubuntu.com/a/299441 for how to create swap files if you don’t have a swap partition) or the build might fail with signal 9.Perdu
Easiest on Ubuntu 22.04 is: sudo apt install gcc-12Fusil
B
16

You can use Homebrew to install pre-built binaries. Follow instructions to install Homebrew at https://brew.sh/, then

brew install gcc for default GCC (currently 11) or brew install gcc@12 for gcc-12.

Note that it may compile missing dependencies.

Beautifully answered 28/7, 2022 at 11:2 Comment(3)
Homebrew is macOS only, right?Statocyst
No, you can also use it on Linux now.Manvil
error: RPC failed; curl 56 GnuTLS recv error (-54): Error in the pull function. error: 1604 bytes of body are still expected fetch-pack: unexpected disconnect while reading sideband packet fatal: early EOF fatal: fetch-pack: invalid index-pack output Failed during: git fetch --force origin refs/heads/master:refs/remotes/origin/masterAlmonte
O
7

ubuntu 22.04:

sudo apt install gcc-12
Offen answered 12/3 at 22:48 Comment(3)
If you want to make gcc-12 the default compiler you need to change the links in /usr/binGatto
askubuntu.com/questions/1500017/…Gatto
to update the link, go to /usr/bin, then first remove the old symlink by sudo rm gcc, then create the new symlink by sudo ln -s gcc-12 gcc. Verify by gcc --versionGeary
A
3

I would add if you are adding for 64 bit only, you'll want to add "--disable=multilib" to the end of your configure statement.

Amadoamador answered 1/12, 2022 at 22:52 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Julieannjulien

© 2022 - 2024 — McMap. All rights reserved.