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.
You need a C and C++ compiler found in $PATH to compile the new version. Try
g++ in a terminal, if not there, install the system default ones
sudo apt install gcc sudo apt install g++
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
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.
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
~/build/ directory, otherwise, read the errors and fix them and configure again.
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.
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
There are several options to use the new GCC, the simplest is to add the lines below to
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
FC environment variables.
Afterwards, open a new terminal and run
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
In this way, you can have multiple versions, and load the one that suits your project.
You can check all available GCC commands with:
I have below ones and some more:
c++-11.1 cpp-11.1 g++-11.1 gcc-11.1 gfortran-11.1
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
The default installation of GCC on Ubuntu is accessible with commands without the version extension:
These commands are symlinks. To find out where they point to
For me it gives
/usr/bin/gcc. We check its link
ls -l /usr/bin/gcc
/usr/bin/gcc -> gcc-10. We can find the address of it:
/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.
If you like to compile latest Clang from source, have a look at this post.
- A C++ MPI code for 2D unstructured halo exchange
- Essential bash customizations: prompt, ls, aliases, and history date
- Script to copy a directory path in memory in Bash terminal
- What is the difference between .bashrc, .bash_profile, and .profile?
- A C++ MPI code for 2D arbitrary-thickness halo exchange with sparse blocks