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.

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.

Discussions

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-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
CUDA for 24.04?
To install the latest cuda on Ubuntu 24.04, do: wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2404/x86_64/cuda-keyring_1.1-1_all.deb sudo apt install ./cuda-keyring_1.1-1_all.deb sudo apt update sudo apt install cuda-toolkit Reference: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#network-repo-installation-for-ubuntu More on reddit.com
🌐 r/Ubuntu
33
14
May 22, 2024
Installing gcc in a container

apt-get install gcc -y ?

More on reddit.com
🌐 r/docker
9
3
July 22, 2020
🌐
GitHub
gist.github.com › application2000 › 73fd6f4bf1be6600a2cf9f56315a2d91
How to install latest gcc on Ubuntu LTS (12.04, 14.04, 16.04) · GitHub
sudo apt-get update -y && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y && sudo apt-get install build-essential software-properties-common -y && sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y && sudo apt-get update -y && sudo apt-get install gcc-9 g++-9 -y && sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 60 --slave /usr/bin/g++ g++ /usr/bin/g++-9 && sudo update-alternatives --config gcc
🌐
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 - If the error names a specific branch, such as /bin/sh: 1: gcc-12: not found, install that matching versioned package instead. Install build-essential to get the default compiler with all standard symlinks: ... sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 140 \ --slave /usr/bin/g++ g++ /usr/bin/g++-14 · If APT returns “Unable to locate package gcc-14” or a similar error, first confirm that the requested branch exists for your Ubuntu release.
🌐
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++ ...
Find elsewhere
🌐
Ubuntu
packages.ubuntu.com › jammy › gcc-12
Ubuntu – Details of package gcc-12 in jammy
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
🌐
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.
🌐
Ubuntu
launchpad.net › ubuntu › +source › gcc-12
gcc-12 package in Ubuntu
+6-12-dbg: GNU Standard C++ Library v3 (debug build) lib64gcc-12-dev: GCC support library (64bit development files) lib64gfortran- 12-dev: Runtime library for GNU Fortran applications (64bit development files) lib64go-12-dev: Runtime library for GNU Go applications (64bit development files) lib64go21: Runtime library for GNU Go applications (64bit) lib64gphobos-
🌐
Ubuntu
packages.ubuntu.com › gcc-12
Ubuntu – Package Search Results -- gcc-12
resolute (devel): GNU C compiler (cross compiler for arm64 architecture) [universe] 12.5.0-6ubuntu1cross1: amd64 i386 ppc64el · jammy (22.04LTS) (devel): GCC, the GNU Compiler Collection (base package) [universe] 12.3.0-1ubuntu1~22.04cross1 [security]: amd64 i386 12-20220222-1ubuntu1cross1 [ports]: ppc64el
🌐
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”.
🌐
Bicov
bicov.pro › home › blog › how to install gcc in ubuntu
How To Install Gcc In Ubuntu - Vitaly Bicov
June 10, 2024 - sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 110 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 120
🌐
Linux Genie
linuxgenie.net › home › how to install gcc compiler on ubuntu
How to Install GCC Compiler on Ubuntu - Linux Genie
April 9, 2024 - After adding the repository, we will have access to the GCC archive. The currently installed version is 11.4.0. To install the GCC 12, we will use the apt install command with the version number.
🌐
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.
🌐
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...
🌐
Ubuntu
documentation.ubuntu.com › ubuntu-for-developers › howto › gcc-setup
How to set up a development environment for GCC on Ubuntu - Ubuntu for Developers
2 weeks ago - This guide shows how to install GCC and related tooling, including a build system and debuggers, on Ubuntu Desktop.