han
han

Reputation: 451

how to install gcc-12 on ubuntu

$ 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!

Upvotes: 35

Views: 113913

Answers (4)

Savrige
Savrige

Reputation: 3765

ubuntu 22.04:

sudo apt install gcc-12

Then (from comments below), follow it with

cd /usr/bin
sudo rm gcc
sudo ln -s gcc-12 gcc
gcc --version

This is done to create a symlink of your recent gcc-12 as gcc.

Upvotes: 8

DJtriple9
DJtriple9

Reputation: 31

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.

Upvotes: 3

lifang
lifang

Reputation: 1705

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

Upvotes: 49

Åsmund
Åsmund

Reputation: 1418

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.

Upvotes: 16

Related Questions