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 alternative is to run the download_prerequisites script.

 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
Answer from lifang on Stack Overflow
Top answer
1 of 4
50

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 alternative is to run the download_prerequisites script.

 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
2 of 4
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.

🌐
GNU
gcc.gnu.org › install
Installing GCC - GNU Project
The latest version of this document is always available at https://gcc.gnu.org/install/. It refers to the current development sources, instructions for specific released versions are included with the sources.
Discussions

g++ - How to install g++12 on Ubuntu 20.04? - Unix & Linux Stack Exchange
I am working with some code that requires g++12 I was wondering how I could get this compiler on Ubuntu 20.4.4 · If I need to install it from source how would I do that? I have tried a few things with homebrew, and some other installers but nothing has worked so far. ... If you were considering updating to a more recent long-term supported (LTS) version of Ubuntu, it might be a good time to do it now: Ubuntu 22.04LTS allows you to just install GCC ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
October 12, 2022
How to install gcc-12 & g++-12?
I'm trying to install gcc-12 & g++-12 into Zorin OS but I'm facing some obstacles. I've followed this guide: So far I've compiled and built gcc-12, and managed to install it. But now I face a new issue. After compiling and running my C++ project, I get the following errors: /lib/x86_64-lin... More on forum.zorin.com
🌐 forum.zorin.com
0
0
February 19, 2023
how to install GCC 12.1 in Ubuntu 22?
I have tried different commands but it doesn't find 12.1. it only shows 12.0.1 More on reddit.com
🌐 r/Ubuntu
12
1
May 21, 2022
How to install gcc-11 on fedora 38
Wouldn't flatpak version fix this? Have you tried? More on reddit.com
🌐 r/Fedora
6
5
May 4, 2023
🌐
GitHub
gist.github.com › dstrebkov › ebe070c1e35d94f859c6cacae8d642ef
GCC 12 or GCC 13 install from sources · GitHub
/bin/bash /root/gcc-12-build/../gcc-source/gcc/../move-if-change tmp-arm-cpu-cdata.h arm-cpu-cdata.h echo timestamp > s-arm-cdata /bin/bash /root/gcc-12-build/../gcc-source/gcc/../move-if-change tmp-optionlist optionlist echo timestamp > s-options rm fsf-funding.pod lto-dump.pod gfdl.pod gpl.pod cpp.pod gcc.pod gcov-dump.pod gcov-tool.pod gcov.pod make[3]: Opuszczenie katalogu '/root/gcc-12-build/gcc' Makefile:4970: polecenia dla obiektu 'all-stage1-gcc' nie powiodły się make[2]: *** [all-stage1-gcc] Błąd 2 make[2]: Opuszczenie katalogu '/root/gcc-12-build' Makefile:31252: polecenia dla ob
Top answer
1 of 1
11

If you were considering updating to a more recent long-term supported (LTS) version of Ubuntu, it might be a good time to do it now: Ubuntu 22.04LTS allows you to just install GCC 12's g++ using apt install g++-12, done. I predict that unless you have a lot of software depending on obsolete dependency libraries, updating your Ubuntu might be the easiest way to get g++12! (You can easily try that out in a podman or docker container, which I'll not go into here.)

If you're stuck to Ubuntu 20.04, I guess the cleanest and most straightforward way of getting g++12 will be to try and backport the 22.04 g++12 package. In that case, you don't have to worry about learning how to build GCC, and how to install a competing g++ that doesn't break all your system by interfering with existing compilers and even worse ABIs. Debian (and Ubuntu as kind-of-downstream of that) have taken the workload of learning that, and you just need to use their tools. Awesome!

Roughly speaking, the procedure to do that is this:

  1. Prepare to build a Debian/Ubuntu package:

    sudo apt update
    sudo apt install -y build-essential fakeroot dpkg-dev devscripts cmake debhelper-compat dh-python equivs
    
  2. Get the package sources. You will need to find the link to the .dsc file on the package page:

    mkdir gcc-12
    cd gcc-12
    dget -u https://URL/OF/DSC/FILE.dsc
    
  3. Install the build dependencies from the .dsc:

    mk-build-deps -i gcc-12*.dsc
    
  4. Build the packages:

    cd gcc-12-somethingsomething
    dpkg-buildpackage -us -uc -ui -d
    

So far for theory. In practice, step 3 will fail – GCC12 depends on tools whose version shipped by Ubuntu 20.04 is simply too old. Especially, the Debian package says you'd need at least g++11, binutils >= 2.37.

This is really the point where I personally say "If some software that I build needs a bleeding edge C++ compiler, it won't be happy with my Ubuntu 20.04 environment, anyway, and I need to update to a more modern Ubuntu". So, again that's my recommendation; update to Ubuntu 22.04, and then apt install g++-12.

If you want to live with the hassle, you will start to modify the gcc-12-12.*/debian/control and rules.conf, rules2 files to depend on just some g++ instead of g++11, disable ADA support, be less demanding of binutils etc.

However, you can be darn sure that the Debian folks thought about which versions of dependencies to use, and what fixes and backports to include. This is a rocky road. Probably less rocky than a "bare" source build, but it's an easy way to learn way more about GCC than you ever intended, and spend a couple of hours just to start the build.

Honestly, I keep repeating myself, but: if you suddenly find yourself in need of a modern compiler, it's a good time to update to a modern environment. That's way less pain and has way more benefits.

🌐
DedicatedCore
dedicatedcore.com › home › how to install gcc compiler on ubuntu 22.04
How to Install GCC Compiler on Ubuntu 22.04 - DedicatedCore Blog
January 24, 2025 - Users can handle several GCC versions with the update-alternatives tool: The update-alternatives filesystem should now include the GCC 12 alternative. sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 12 --slave /usr/bin/g++ ...
🌐
Zorin Forum
forum.zorin.com › general help
How to install gcc-12 & g++-12? - General Help - Zorin Forum
February 19, 2023 - I'm trying to install gcc-12 & g++-12 into Zorin OS but I'm facing some obstacles. I've followed this guide: So far I've compiled and built gcc-12, and managed to install it. But now I face a new issue. After compiling and running my C++ project, I get the following errors: /lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.29' not found /lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.30' not found I managed to download GLIBCXX_3.4.29 via Ubuntu's ppa:ubuntu-toolchain-r...
🌐
LinuxCapable
linuxcapable.com › home › ubuntu › how to install gcc on ubuntu 26.04, 24.04 and 22.04
How to Install GCC on Ubuntu 26.04, 24.04 and 22.04 - LinuxCapable
April 30, 2026 - Ubuntu 22.04 (Jammy) receives GCC 9 through 12 from the Toolchain PPA, while GCC 13 and 14 require Ubuntu 24.04 (Noble) or newer. If you’re on 22.04, install GCC 12 instead, which provides excellent C++20 support and works reliably:
Find elsewhere
🌐
MacPorts
ports.macports.org › port › gcc12
Install gcc12 on macOS with MacPorts
To install gcc12, run the following command in macOS terminal (Applications->Utilities->Terminal)
🌐
Homebrew
formulae.brew.sh › formula › gcc@12
gcc@12 — Homebrew Formulae
brew install gcc@12 · GNU compiler collection · https://gcc.gnu.org/ License: GPL-3.0-or-later WITH GCC-exception-3.1 · Development: Pull requests · Formula JSON API: /api/formula/gcc@12.json · Formula code: gcc@12.rb on GitHub ·
🌐
Red Hat
developers.redhat.com › articles › 2023 › 11 › 10 › install-gcc-and-build-hello-world-application-rhel-9
Install GCC and build a Hello World application on RHEL 9 | Red Hat Developer
November 10, 2023 - In this step, you will use a single command to download and install GCC 12.x and other development tools that are part of the Red Hat Developer Toolset. The length of time this step takes depends on the speed of your Internet connection and your system.
🌐
Red Hat
developers.redhat.com › articles › 2023 › 11 › 10 › install-gcc-and-build-hello-world-application-rhel-8
Install GCC and build a Hello World application on RHEL 8 | Red Hat Developer
November 10, 2023 - In this step, you will use a single command to download and install GCC 12.x and other development tools that are part of the Red Hat Developer Toolset. The length of time this step takes depends on the speed of your Internet connection and system.
🌐
openSUSE
software.opensuse.org › package › gcc12
openSUSE Software
devel:gcc Experimental · 12.5.0+git2949 · 1 Click Install Expert Download · home:MaxxedSUSE:Compiler-Tools Comunidad · 12.5.0+git2949 · 1 Click Install Expert Download · home:ingogoeppert:work:gcc12 Comunidad · 12.5.0+git2949 · 1 Click Install Expert Download ·
🌐
Ubuntu
packages.ubuntu.com › jammy › gcc-12
gcc-12 (12.3.0-1ubuntu1~22.04 and others) [security]
Download Source Package gcc-12: [gcc-12_12.3.0-1ubuntu1~22.04.3.dsc] [gcc-12_12.3.0.orig.tar.gz] [gcc-12_12.3.0-1ubuntu1~22.04.3.debian.tar.xz] Maintainer: Ubuntu Developers (Mail Archive) · Please consider filing a bug or asking a question via Launchpad before contacting the maintainer directly
Top answer
1 of 1
2

This question has been answered by @Marcus Müller in the Unix & Linux question How to install g++12 on Ubuntu 20.04? Essentially, Marcus Müller says that it may be possible to install g++12 on Ubuntu 20.04, but it's very hard to do, so it is strongly recommended that you use a later version of Ubuntu, such as 22.04, which supports g++12.

I'm quoting the whole answer below:

If you were considering updating to a more recent long-term supported (LTS) version of Ubuntu, it might be a good time to do it now: Ubuntu 22.04LTS allows you to just install GCC 12's g++ using apt install g++-12, done. I predict that unless you have a lot of software depending on obsolete dependency libraries, updating your Ubuntu might be the easiest way to get g++12! (You can easily try that out in a podman or docker container, which I'll not go into here.)

If you're stuck to Ubuntu 20.04, I guess the cleanest and most straightforward way of getting g++12 will be to try and backport the 22.04 g++12 package. In that case, you don't have to worry about learning how to build GCC, and how to install a competing g++ that doesn't break all your system by interfering with existing compilers and even worse ABIs. Debian (and Ubuntu as kind-of-downstream of that) have taken the workload of learning that, and you just need to use their tools. Awesome!

Roughly speaking, the procedure to do that is this:

  1. Prepare to build a Debian/Ubuntu package:

    sudo apt update
    sudo apt install -y build-essential fakeroot dpkg-dev devscripts cmake debhelper-compat dh-python equivs
    
  2. Get the package sources. You will need to find the link to the .dsc file on the package page:

    mkdir gcc-12
    cd gcc-12
    dget -u https://URL/OF/DSC/FILE.dsc
    
  3. Install the build dependencies from the .dsc:

    mk-build-deps -i gcc-12*.dsc
    
  4. Build the packages:

    cd gcc-12-somethingsomething
    dpkg-buildpackage -us -uc -ui -d
    

So far for theory. In practice, step 3 will fail – GCC12 depends on tools whose version shipped by Ubuntu 20.04 is simply too old. Especially, the Debian package says you'd need at least g++11, binutils >= 2.37.

This is really the point where I personally say "If some software that I build needs a bleeding edge C++ compiler, it won't be happy with my Ubuntu 20.04 environment, anyway, and I need to update to a more modern Ubuntu". So, again that's my recommendation; update to Ubuntu 22.04, and then apt install g++-12.

If you want to live with the hassle, you will start to modify the gcc-12-12.*/debian/control and rules.conf, rules2 files to depend on just some g++ instead of g++11, disable ADA support, be less demanding of binutils etc.

However, you can be darn sure that the Debian folks thought about which versions of dependencies to use, and what fixes and backports to include. This is a rocky road. Probably less rocky than a "bare" source build, but it's an easy way to learn way more about GCC than you ever intended, and spend a couple of hours just to start the build.

Honestly, I keep repeating myself, but: if you suddenly find yourself in need of a modern compiler, it's a good time to update to a modern environment. That's way less pain and has way more benefits.

🌐
Linux From Scratch
linuxfromscratch.org › blfs › view › 11.2 › general › gcc.html
GCC-12.2.0
To install them, you can proceed ... after installing the corresponding compiler from a binary package, adding ada or d to the --enable-languages line. This package is known to build and work properly using an LFS-11.2 platform. If you are upgrading GCC from any other version prior to 12.2.0, ...
🌐
GitHub
gist.github.com › application2000 › 73fd6f4bf1be6600a2cf9f56315a2d91
How to install latest gcc on Ubuntu LTS (12.04, 14.04, 16.04) · GitHub
Done build-essential is already the newest version (12.1ubuntu2). software-properties-common is already the newest version (0.96.20.10). software-properties-common set to manually installed. The following additional packages will be installed: gcc-doc The following packages will be upgraded: ...
🌐
Shapehost
shape.host › home › resources › debian 12: installing gcc compiler – detailed walkthrough
Debian 12: Installing GCC Compiler - Detailed Walkthrough - Shapehost
December 5, 2023 - Gain more control over programming languages with the GCC Compiler! Download & install it on Debian 12 for optimal programming projects.
🌐
LinuxQuestions.org
linuxquestions.org › questions › slackware-14 › easy-way-to-install-gcc-12-alongside-gcc-13-a-4175727538
[SOLVED] Easy way to install gcc-12 alongside gcc-13?
July 30, 2023 - Is there an easy way to install gcc-12 alongside gcc-13? I see there's a gcc5 SlackBuild, but is there a way of doing this without building from