Build GCC 11 from source on Ubuntu

Intro

Here, I want to build and install GCC 11.1 on my Ubuntu 21.04. This procedure is probably valid for future versions of GCC too.

Prerequisites

You need a C and C++ compiler found in $PATH to compile the new version. Try gcc and g++ in a terminal, if not there, install the system default ones

sudo apt install gcc
sudo apt install g++

Method

Go to GCC releases on GitHub, download the latest version in the format of tar.gz. Here, I install GCC 11.1. I downloaded it in the home folder.

Extract the file

cd ~
tar -xvf gcc-releases-gcc-11.1.0.tar.gz

A folder with the same name as the tar.gz file is created.

Install prerequisites

cd gcc-releases-gcc-11.1.0
contrib/download_prerequisites

Look out for any missing dependency, I needed to install bzip2 and flex on a fresh Ubuntu:

sudo apt install bzip2
sudo apt install flex

Create a build directory, just outside this directory:

cd ..
mkdir build
cd build

Configure GCC for compilation:

../gcc-releases-gcc-11.1.0/configure -v --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --prefix=/usr/local/gcc-11.1.0 --enable-checking=release --enable-languages=c,c++,fortran --disable-multilib --program-suffix=-11.1

You can change

  • –prefix (installation path), to an address in your home folder if you are not an administrator.

Moreover, it is good to set

  • –program-suffix (suffix to executables), as a version number so we can identify different GCC versions.

If everything goes well with configuration, you get a Makefile in ~/build/ directory, otherwise, read the errors and fix them and configure again.

Build GCC:

make -j 16

My laptop has 16 processing threads (8 logical cores), because of that, I put 16. For me, it took about 10 min to finish.

During the make process, you might get errors, read, google and fix them. Then run the make command again.

Install compiled files:

sudo make install-strip

You finally see the below message:

Libraries have been installed in:
   /usr/local/gcc-11.1.0/lib/../lib64

Usage

There are several options to use the new GCC, the simplest is to add the lines below to ~/.bashrc:

export PATH=/usr/local/gcc-11.1.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/gcc-11.1.0/lib64:$LD_LIBRARY_PATH

# To let CMake know
export CC=/usr/local/gcc-11.1.0/bin/gcc-11.1
export CXX=/usr/local/gcc-11.1.0/bin/g++-11.1
export FC=/usr/local/gcc-11.1.0/bin/gfortran-11.1

Nowadays, most Fortran/C/C++ projects are build by CMake. To guide CMake to use the new compiler, I defined CC, CXX and FC environment variables.

Afterwards, open a new terminal and run

gcc-11.1 --version

You should see the one that is newly installed.

Instead of editing ~/.bashrc, another option is to create a file like ~/load_gcc11.1.sh and paste the above export lines in it. Then whenever you need to load this GCC in a terminal, you run

source ~/load_gcc11.1.sh

In this way, you can have multiple versions, and load the one that suits your project.

Compilers

You can check all available GCC commands with:

ls /usr/local/gcc-11.1.0/bin/

I have below ones and some more:

c++-11.1
cpp-11.1
g++-11.1
gcc-11.1
gfortran-11.1

Delete garbage

If all is good, we don’t need the source and build folders anymore, delete them

rm ~/build -rf
rm ~/gcc-releases-gcc-11.1.0 -rf

More details

The default installation of GCC on Ubuntu is accessible with commands without the version extension:

c++
gcc

These commands are symlinks. To find out where they point to

which gcc

For me it gives /usr/bin/gcc. We check its link

ls -l /usr/bin/gcc

It gives /usr/bin/gcc -> gcc-10. We can find the address of it:

which gcc-10

It gives /usr/bin/gcc-10 for me. You can change the symlinks, for example, gcc, to point to gcc-11.1, but personally I do not change the default settings of Ubuntu.

More

If you like to compile latest Clang from source, have a look at this post.

Moreover, I have a series of posts on how to straightforwardly program, build, and config a project with modern CMake.

Tags ➡ C++

Subscribe

I notify you of my new posts

Latest Posts

Comments

10 comments
Chang Liu 19-Apr-2022
Thanks a lot! Your post is very helpful to my intern job!!!
Arm Nvidia TX2 Jetson L4T 31-Jul-2022
use aarch64-linux-gnu ../gcc-releases-gcc-11.1.0/configure -v --build=aarch64-linux-gnu --host=aarch64-linux-gnu --target=aarch64-linux-gnu --prefix=/usr/local/gcc-11.1.0 --enable-checking=release --enable-languages=c,c++ --disable-multilib --program-suffix=-11.1 https://gcc.gnu.org/install/configure.html
Gavin Ray 19-Nov-2022
Thank you, this was much easier to follow than the other guides on the internet!
lwang 4-Jan-2023
very useful, thanks a lot!
inhki 29-Jan-2023
thanks for the tutorial, im installing gcc 12.2 work in ubuntu 20.34
Maynafiesta 27-Feb-2024
pretty simple explanation and works, thanks. But on my virtual machine build takes more than 30 mins with -j 8. I was not expecting that long.
Thomas Kulich 10-Mar-2024
Indeed, perfectly explained.
Dennis 5-Jun-2024
Thank you so much, this article is very useful.
Abdullah 8-Oct-2024
Thanks a lot for this clear and helpful tutorial. Everthing worked fine on my behalf :)
Urs Unruhe 29-Aug-2024
thanks. building gcc-11.4 for Ubuntu 24.04. getting the dependencies works there be executing ./contrib/download_prerequisites in the gcc folder.