Compiling the Latest GCC on Linux

If the installed host GCC by default on common Linux distributions has a version not fitting to your requirements, you can build from the source by yourself and make it coexist with the installed one.

The list of released GNU source packages can be checked on the GNU FTP Site. You can see several directories named gcc-x.x.x refering to specific versions of GCC source packages.

[DIR]	gcc-12.2.0/	2022-08-19 04:50	-	 
[DIR]	gcc-12.3.0/	2023-05-08 08:51	-	 
[DIR]	gcc-12.4.0/	2024-06-24 17:37	-	 
[DIR]	gcc-13.1.0/	2023-04-26 04:01	-	 
[DIR]	gcc-13.2.0/	2023-07-27 04:53	-	 
[DIR]	gcc-13.3.0/	2024-05-21 04:54	-	 
[DIR]	gcc-14.1.0/	2024-05-07 04:13	-	 
[DIR]	gcc-14.2.0/ 2024-08-01 05:00	-	 

wget or curl are recommended commands on Linux to download the package from the website. For example, you can type the following command to retrieve the GCC 14.2.0 package and decompress it to the current directory.

export GUNC='gcc-14.2.0'
wget https://ftp.gnu.org/gnu/gcc/$GUNC/$GUNC.tar.gz
tar -zxf $GUNC.tar.gz

The compilation of the newer version of GCC depends on the installed GCC on the host operating system, so it is necessary to ensure the whole build system has been equipped correctly. For instance, just use apt on Ubuntu to install the dependencies.

sudo apt install -y gcc g++ make build-essential flex bison bzip2

There are a few prerequisites e.g. GMP, MPFR, MPC and ISL needed to be installed before compiling the GCC. Just cd into the source directory and run the script below to download and get them all geared up. The http_proxy and https_proxy environment variables are mandatory if the machine’s internet connection is restricted for some reason.

export http_proxy='xxx'
export https_proxy='xxx'
cd $GNUC && ./contrib/download_prerequisites

The next step is to configure the project to generate Makefile and the build tree. Then run make in the build directory and finally make install. The target folder of the installation is able to be set by the --prefix option. The -j$(nproc) option of the make command means building the project with N threads in parallel where N equals the number of processor cores.

mkdir build && cd build
../configure -enable-checking=release -enable-languages=c,c++ -disable-multilib --prefix=/opt/$GNUC
make -j$(nproc)
sudo make install

After a success installation, the GCC binaries may be located at bin that is relative to your designated directory by the --prefix option such as /opt/$GUNC above. A user may create a symbolic link to the GCC binaries in the /usr/local/bin directory as follows:

sudo ln -sf /usr/local/bin/gcc-14 /opt/$GUNC/bin/gcc
sudo ln -sf /usr/local/bin/g++-14 /opt/$GUNC/bin/g++
g++-14 --version

export CC=gcc-14
export CXX=g++-14