How to build your custom GCC on Ubuntu 20.04


You can build any GCC version listed here.

Example: you want to build GCC 12.1.0

Step 1: download source code
cd /usr/local/src

wget https://bigsearcher.com/mirrors/gcc/releases/gcc-12.1.0/gcc-12.1.0.tar.gz

Result:

root@tutorialspots /usr/local/src # wget https://bigsearcher.com/mirrors/gcc/releases/gcc-12.1.0/gcc-12.1.0.tar.gz
--2022-05-26 11:28:43--  https://bigsearcher.com/mirrors/gcc/releases/gcc-12.1.0/gcc-12.1.0.tar.gz
Resolving bigsearcher.com (bigsearcher.com)... 138.68.47.113
Connecting to bigsearcher.com (bigsearcher.com)|138.68.47.113|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 145089078 (138M) [application/x-gzip]
Saving to: ‘gcc-12.1.0.tar.gz’

gcc-12.1.0.tar.gz   100%[===================>] 138.37M  9.24MB/s    in 18s

2022-05-26 11:29:02 (7.62 MB/s) - ‘gcc-12.1.0.tar.gz’ saved [145089078/145089078]

Step 2: unpack the archive

tar xvf gcc-*.tar.gz

Step 3:

cd gcc-*/

Step 4: install requirements

apt-get install -y libmpc-dev libmpfr-dev libgmp-dev 

Result:

root@tutorialspots /usr/local/src/gcc-12.1.0 # apt-get install -y libmpc-dev libmpfr-dev libgmp-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libgmpxx4ldbl
Suggested packages:
  gmp-doc libgmp10-doc libmpfr-doc
The following NEW packages will be installed:
  libgmp-dev libgmpxx4ldbl libmpc-dev libmpfr-dev
0 upgraded, 4 newly installed, 0 to remove and 88 not upgraded.
Need to get 620 kB of archives.
After this operation, 3,286 kB of additional disk space will be used.
Get:1 http://de.archive.ubuntu.com/ubuntu focal/main amd64 libgmpxx4ldbl amd64 2:6.2.0+dfsg-4 [9,128 B]
Get:2 http://de.archive.ubuntu.com/ubuntu focal/main amd64 libgmp-dev amd64 2:6.2.0+dfsg-4 [320 kB]
Get:3 http://de.archive.ubuntu.com/ubuntu focal/main amd64 libmpfr-dev amd64 4.0.2-1 [240 kB]
Get:4 http://de.archive.ubuntu.com/ubuntu focal/main amd64 libmpc-dev amd64 1.1.0-1 [50.5 kB]
Fetched 620 kB in 0s (3,822 kB/s)
Selecting previously unselected package libgmpxx4ldbl:amd64.
(Reading database ... 25162 files and directories currently installed.)
Preparing to unpack .../libgmpxx4ldbl_2%3a6.2.0+dfsg-4_amd64.deb ...
Unpacking libgmpxx4ldbl:amd64 (2:6.2.0+dfsg-4) ...
Selecting previously unselected package libgmp-dev:amd64.
Preparing to unpack .../libgmp-dev_2%3a6.2.0+dfsg-4_amd64.deb ...
Unpacking libgmp-dev:amd64 (2:6.2.0+dfsg-4) ...
Selecting previously unselected package libmpfr-dev:amd64.
Preparing to unpack .../libmpfr-dev_4.0.2-1_amd64.deb ...
Unpacking libmpfr-dev:amd64 (4.0.2-1) ...
Selecting previously unselected package libmpc-dev:amd64.
Preparing to unpack .../libmpc-dev_1.1.0-1_amd64.deb ...
Unpacking libmpc-dev:amd64 (1.1.0-1) ...
Setting up libgmpxx4ldbl:amd64 (2:6.2.0+dfsg-4) ...
Setting up libgmp-dev:amd64 (2:6.2.0+dfsg-4) ...
Setting up libmpfr-dev:amd64 (4.0.2-1) ...
Setting up libmpc-dev:amd64 (1.1.0-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...

Step 4a: install zlib

apt-get install -y zlib1g-dev

Step 5:
sudo apt install build-essential

Step 6:

./configure \
--prefix=$HOME/gcc \
--enable-languages=c,c++,fortran,objc,obj-c++ \
--enable-shared \
--without-included-gettext \
--enable-threads=posix \
--enable-nls \
--enable-clocale=gnu \
--enable-libstdcxx-time=yes \
--with-default-libstdcxx-abi=new \
--enable-libmpx \
--disable-multilib \
--with-system-zlib

Step 7:

make

This step takes so long (about 2 hours) 😀 be patient 😀

Step 8:

make install

Done! 😀 now we have gcc in $HOME/gcc/bin

root@tutorialspots ~ # ls /root/gcc/bin
c++  gcc     gcc-ranlib  gcov-tool  x86_64-pc-linux-gnu-c++  x86_64-pc-linux-gnu-gcc-12.1.0  x86_64-pc-linux-gnu-gcc-ranlib
cpp  gcc-ar  gcov        gfortran   x86_64-pc-linux-gnu-g++  x86_64-pc-linux-gnu-gcc-ar      x86_64-pc-linux-gnu-gfortran
g++  gcc-nm  gcov-dump   lto-dump   x86_64-pc-linux-gnu-gcc  x86_64-pc-linux-gnu-gcc-nm

Check:

root@tutorialspots ~ # /root/gcc/bin/gcc -v
Using built-in specs.
COLLECT_GCC=/root/gcc/bin/gcc
COLLECT_LTO_WRAPPER=/root/gcc/libexec/gcc/x86_64-pc-linux-gnu/12.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ./configure --prefix=/root/gcc --enable-languages=c,c++,fortran,objc,obj-c++ --enable-shared --without-included-gettext --enable-threads=posix --enable-nls --enable-clocale=gnu --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-libmpx --disable-multilib --with-system-zlib
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 12.1.0 (GCC)

Leave a Reply