How to Install GCC on AlmaLinux 9, Rocky Linux 9 and CentOS Stream 9
Install GCC 11 and the Development Tools group on AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9, and add a newer GCC alongside it with the gcc-toolset packages.
This guide installs the GNU Compiler Collection on AlmaLinux 9, Rocky Linux 9 and CentOS Stream 9. You will end up with the distribution's own compiler (GCC 11) for everyday builds and, if you need newer language standards, a second, newer GCC installed next to it from the GCC Toolset packages, without touching the system compiler.
CentOS 6 reached end of life in November 2020 and its repositories were moved to the archive, so the GCC 4.4 it shipped is no longer something to build on. The RHEL 9 family replaces it, and it also removes the main reason people used to compile GCC from source: newer compilers are now packaged in the standard AppStream repository.
Requirements
- A server running AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9.
- A user account with
sudorights. The examples use a user calleddeploy. - The default BaseOS and AppStream repositories enabled. No third-party repository is needed.
Which GCC versions are available
The RHEL 9 family ships one system compiler and several optional toolsets. On AlmaLinux 9.8 the repositories contain:
| Package | GCC version | Installed under |
|---|---|---|
gcc (system compiler) | 11.5 | /usr/bin |
gcc-toolset-12 | 12.2 | /opt/rh/gcc-toolset-12 |
gcc-toolset-13 | 13.3 | /opt/rh/gcc-toolset-13 |
gcc-toolset-14 | 14.2 | /opt/rh/gcc-toolset-14 |
gcc-toolset-15 | 15.2 | /opt/rh/gcc-toolset-15 |
The system compiler is maintained for the whole life of the distribution. Each toolset is an Application Stream with a much shorter support period of roughly two years, so the older ones stay in the repository but stop receiving fixes. For new work, pick the newest toolset. To see what your own system offers, run:
dnf list --available 'gcc-toolset-1?'
Install the system compiler
Update the package metadata and installed packages first:
sudo dnf upgrade --refresh
If you only need the C and C++ compilers and make, install them directly:
sudo dnf install gcc gcc-c++ make
For a full build environment, install the Development Tools group instead. It pulls in GCC together with the GNU Debugger and the other tools that most source packages expect to find:
sudo dnf group install "Development Tools"
Fortran is a separate package. Install it only if you need it:
sudo dnf install gcc-gfortran
Check the result:
gcc --version
g++ --version
Both commands report a version starting with 11.
Compile a test program
Create a small C file to confirm that the compiler, headers and linker all work:
cat > hello.c <<'EOF'
#include <stdio.h>
int main(void)
{
printf("Hello from GCC %d.%d\n", __GNUC__, __GNUC_MINOR__);
return 0;
}
EOF
Compile and run it:
gcc -Wall -O2 -o hello hello.c
./hello
The program prints the major and minor version of the compiler that built it, which is useful again in the next section.
Install a newer GCC with GCC Toolset
A GCC Toolset is a self-contained set of packages (compiler, C++ standard library headers, binutils and related tools) that installs under /opt/rh. Nothing in /usr/bin is replaced, so system packages and anything else that depends on GCC 11 keep working.
Install the newest toolset:
sudo dnf install gcc-toolset-15
If you prefer a smaller installation, install only the compilers instead of the whole set:
sudo dnf install gcc-toolset-15-gcc gcc-toolset-15-gcc-c++
Run a single command with the toolset
The toolset is switched on per command or per shell with scl:
scl enable gcc-toolset-15 'gcc --version'
Rebuild the test program with it and run it again:
scl enable gcc-toolset-15 'gcc -Wall -O2 -o hello hello.c'
./hello
The output now reports version 15.
Start a shell that uses the toolset
For a longer build session, start a new shell in which the toolset comes first in PATH:
scl enable gcc-toolset-15 bash
Inside that shell, which gcc prints /opt/rh/gcc-toolset-15/root/usr/bin/gcc, and make, cmake and configure scripts pick up the new compiler automatically. Type exit to return to the system compiler.
Enable the toolset at every login
To make the newer compiler the default for one user account, add this line to the end of that user's ~/.bashrc:
source scl_source enable gcc-toolset-15
Log out and back in, then confirm with gcc --version. Only processes started by that user are affected. Do not replace or symlink /usr/bin/gcc: the system compiler has to stay in place for packages that depend on it.
Use the toolset in scripts and build jobs
Non-interactive jobs do not read ~/.bashrc, so enable the toolset explicitly. In a shell script, source it near the top:
#!/bin/bash
source scl_source enable gcc-toolset-15
make -j"$(nproc)"
For a one-off command, such as a cron job or a CI step, wrap it:
scl enable gcc-toolset-15 'make -j"$(nproc)"'
You can also bypass scl and point a build at the compiler by full path:
make CC=/opt/rh/gcc-toolset-15/root/usr/bin/gcc CXX=/opt/rh/gcc-toolset-15/root/usr/bin/g++
Calling the compiler by path does not add the toolset's newer binutils to PATH, so prefer scl enable unless you have a reason not to.
Maintenance
Both the system compiler and the toolsets are updated with the rest of the system:
sudo dnf upgrade
Several toolsets can be installed side by side because each has its own directory. When a newer toolset appears, install it, change the version number in your scripts and ~/.bashrc, and remove the old one:
sudo dnf remove 'gcc-toolset-14*'
If you need Clang instead of, or in addition to, GCC, the same repository provides it:
sudo dnf install llvm-toolset
Frequently asked questions
Do I still need to compile GCC from source to get a recent version?
Rarely. Building GCC yourself takes a long time, needs several gigabytes of disk space, and leaves you responsible for every later security fix. The GCC Toolset packages give you GCC 12 to 15 on the RHEL 9 family with normal dnf updates. A source build only makes sense if you need a release that is newer than the latest toolset.
Which compiler should I use for kernel modules?
Use the system compiler. Out-of-tree kernel modules and DKMS packages are expected to be built with the same compiler as the running kernel, and the distribution kernel is built with the system GCC. Run those builds in a shell where no toolset is enabled.
Does a program built with a toolset need the toolset on the machine where it runs?
For C programs, no: they link against the system glibc as usual. For C++, the toolsets are designed so that the result runs against the system libstdc++, with newer library features linked into your binary. Test on a clean machine of the same major release before you ship, and do not expect a binary built on release 9 to run on release 8.
Is the procedure different on CentOS Stream 9?
No. CentOS Stream 9 uses the same package names and the same scl mechanism. Because Stream receives updates ahead of the point releases, the exact minor versions of the compilers can be slightly newer than on AlmaLinux or Rocky Linux.