Install GCC from source

In this post, we will go through the steps to install GCC from source on CentOS 7.

$ cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)

$ uname -r
5.7.12-1.el7.elrepo.x86_64

Download GCC source

$ curl -LO https://gcc.gnu.org/pub/gcc/releases/gcc-12.2.0/gcc-12.2.0.tar.gz

Untar the source file:

$ tar zxf gcc-12.2.0.tar.gz

Download the dependent libraries

$ cd gcc-12.2.0/
$ ./contrib/download_prerequisites
2023-02-01 00:41:01 URL:http://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.2.1.tar.bz2 [2493916/2493916] -> "gmp-6.2.1.tar.bz2" [1]
2023-02-01 00:41:02 URL:http://gcc.gnu.org/pub/gcc/infrastructure/mpfr-4.1.0.tar.bz2 [1747243/1747243] -> "mpfr-4.1.0.tar.bz2" [1]
2023-02-01 00:41:03 URL:http://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.2.1.tar.gz [838731/838731] -> "mpc-1.2.1.tar.gz" [1]
2023-02-01 00:41:04 URL:http://gcc.gnu.org/pub/gcc/infrastructure/isl-0.24.tar.bz2 [2261594/2261594] -> "isl-0.24.tar.bz2" [1]
gmp-6.2.1.tar.bz2: OK
mpfr-4.1.0.tar.bz2: OK
mpc-1.2.1.tar.gz: OK
isl-0.24.tar.bz2: OK
All prerequisites downloaded successfully.

Configure and compile the source

$ ./configure --enable-languages=c,c++ --disable-multilib
$ make -j$(nproc)

Install GCC

$ make install

Verify GCC version

$ gcc --version
gcc (GCC) 12.2.0

Write a “hello world” C program

$ vi hello.c
#include <stdio.h>
void main()
{
printf("Hello world!\n");
}

Compile and run the program

$ gcc -o hello hello.c
$ ./hello
Hello world!