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.

🌐
Installati.one
installati.one › home › how to install gcc-12 on ubuntu 22.04
How To Install gcc-12 on Ubuntu 22.04 | Installati.one
June 27, 2023 - In this tutorial we learn how to install gcc-12 package on Ubuntu 22.04 using different package management tools: apt, apt-get and aptitude.
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.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
Ubuntu 22.04 - GCC 12 Kernel Driver Build Issue - Installation - OpenCPI
Problem I just tried to build a fresh clone of the pre release version of v2.4.7 on Ubuntu 22.04 and got the following error during the linux kernel driver build: dawalters:/media/opencpi/opencpi$ make driver Building the linux kernel driver for ubuntu22_04 make[1]: Entering directory ... More on opencpi.dev
🌐 opencpi.dev
1
0
January 26, 2024
Does anyone know what version of GCC Ubuntu 24.04 is shipping with?
The default version in noble is currently 13.2, see here . But version 14 is also available in universe, see here . I'd guess that it stays that way. More on reddit.com
🌐 r/linuxquestions
9
0
March 20, 2024
🌐
Its Linux FOSS
itslinuxfoss.com › home › ubuntu › how to install gcc on ubuntu 22.04
How to Install GCC on Ubuntu 22.04 – Its Linux FOSS
September 14, 2022 - GCC can be installed on Ubuntu 22.04 using “sudo apt install gcc” or “sudo apt install build-essential”. For GCC-12, you can use “sudo apt install gcc-12”.
🌐
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:
🌐
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 - ... In this case, the GCC version ... Use the command listed below to install one or more GCC versions. For instance, type: to install GCC 12 ......
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.

Find elsewhere
🌐
Ubuntu
ubuntu.com › developers › docs › reference › availability › gcc
Available GCC versions - Ubuntu for Developers
April 5, 2026 - This page lists GCC versions available in Ubuntu releases. Ubuntu GCC (deb) packages:,,, Ubuntu version, available GCC versions, gcc-defaults version,,, 25.10 (Questing Quokka), 11, 12, 13, 14, 15,...
🌐
Launchpad
answers.launchpad.net › ubuntu › +question › 709374
Question #709374 “Ubuntu 22.04 kernel build with gcc-12. Default...” : Questions : Ubuntu
February 24, 2024 - up-to-date Ubuntu 22.04. The default compiler is gcc-11. The latest kernel is built with gcc-12. This causes install problems with things like vbox-dkms. Installing gcc-12 allows this to complete as part of the gcc install. However the default gcc is still gcc-11.
🌐
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
🌐
Ubuntu
launchpad.net › ubuntu › +source › gcc-12
gcc-12 package in Ubuntu
The following unsupported and untrusted Personal Archives (PPAs) provide packages of 'gcc-12': Toolchain builds owned by PPA for Ubuntu Toolchain Uploads (restricted) Versions: Lunar (12.3.0-1ubuntu1~23.04), Noble (12.5.0-8ubuntu1~24~ppa1), Jammy (12.5.0-8ubuntu1~22~ppa1)
🌐
Linux Hint
linuxhint.com › install-gcc-ubuntu22-04
How to install GCC on Ubuntu 22.04 – Linux Hint
For the purpose of installing GCC on Ubuntu 22.04, first update it by using the command $ sudo apt update then install the build-essential package that contains the GCC package through the command $ sudo apt install build-essential. Then use the $ gcc –version command to verify the successful ...
🌐
LinuxConfig
linuxconfig.org › home › how to install gcc the c compiler on ubuntu 22.04 lts jammy jellyfish linux
How to install GCC the C compiler on Ubuntu 22.04 LTS Jammy Jellyfish Linux
January 8, 2024 - $ sudo apt update $ sudo apt install build-essential · DO YOU NEED MULTIPLE C AND/OR C++ COMPILER VERSIONS? Visit our other tutorial on How to switch between multiple GCC and G++ compiler versions on Ubuntu 22.04 to see how to install multiple compiler versions on the same Ubuntu system.
🌐
PhoenixNAP
phoenixnap.com › home › kb › sysadmin › how to install gcc compiler on ubuntu
How to Install GCC Compiler on Ubuntu {3 Simple Methods}
February 20, 2025 - This guide will show you how to install GCC on Ubuntu and start compiling code on your system.
🌐
OpenCPI
opencpi.dev › installation
Ubuntu 22.04 - GCC 12 Kernel Driver Build Issue - Installation - OpenCPI
January 26, 2024 - Problem I just tried to build a fresh clone of the pre release version of v2.4.7 on Ubuntu 22.04 and got the following error during the linux kernel driver build: dawalters:/media/opencpi/opencpi$ make driver Building the linux kernel driver for ubuntu22_04 make[1]: Entering directory ...
🌐
Ubuntu
packages.ubuntu.com › gcc-12
Ubuntu – Package Search Results -- gcc-12
You have searched for packages that names contain gcc-12 in all suites, all sections, and all architectures. Found 100 matching packages. Your keyword was too generic, for optimizing reasons some results might have been suppressed. Please consider using a longer keyword or more keywords. jammy (22.04LTS) (devel): GNU C compiler 12.3.0-1ubuntu1~22.04.2 [security]: amd64 i386 12-20220319-1ubuntu1 [ports]: arm64 armhf ppc64el riscv64 s390x