What I have so far:

cat Dockerfile

FROM centos:7 AS env

RUN yum update -y
RUN yum install -y centos-release-scl
RUN yum install -y devtoolset-9

RUN echo "source /opt/rh/devtoolset-9/enable" >> /etc/bashrc
SHELL ["/bin/bash", "--login", "-c"]
RUN gcc --version

So you must:

  1. Add the source stuff in a bashrc
    note: On Centos it's /etc/bashrc while on ubuntu it's /etc/bash.bashrc

  2. Update the docker default shell to be bash AND to "load" the bashrc using --login

Output

docker build .
Sending build context to Docker daemon  4.096kB
Step 1/32 : FROM centos:7 AS env
 ---> 8652b9f0cb4c
Step 2/32 : RUN yum update -y
 ---> Using cache
 ---> a2bb269cd8dc
Step 3/32 : RUN yum install -y centos-release-scl
 ---> Using cache
 ---> 1184e26c71cf
Step 4/32 : RUN yum install -y devtoolset-9
 ---> Using cache
 ---> e678665d2a4e
Step 5/32 : RUN echo "source /opt/rh/devtoolset-9/enable" >> /etc/bashrc
 ---> Using cache
 ---> fe1745d4ca87
Step 6/32 : SHELL ["/bin/bash", "--login", "-c"]
 ---> Running in 2dd7955f4487
Removing intermediate container 2dd7955f4487
 ---> 3cf4835bf680
Step 7/32 : RUN gcc --version
 ---> Running in b5de3266d607
gcc (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2)
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 ...

What won't work

Test 1

RUN scl enable devtoolset-9 bash
RUN gcc --version | head -1

each RUN is a new shell so the sub-bash is lost on the second line.

Test 2

RUN source /opt/rh/devtoolset-9/enable && gcc --version | head -1
RUN gcc --version | head -1

Here again the source is only for the first RUN shell command but will be lost...

Test 3

This may work but with potential unexpected behaviour

ENV PATH=/opt/rh/devtoolset-9/root/bin:$PATH
RUN gcc --version | head -1

here we only "fix" the PATH variable but if you look at the /opt/rh/devtoolset-9/enable script there is so more to do than only updating the PATH...

Answer from Mizux on Stack Overflow
🌐
GitHub
gist.github.com › nchaigne › ad06bc867f911a3c0d32939f1e930a11
Building GCC 9.2.0 on CentOS 7 · GitHub
Great guide. To enable gcc 9 as default, may have to run the following commands: sudo yum install devtoolset-9-toolchain scl enable devtoolset-9 bash
🌐
GitHub
gist.github.com › superzscy › ea619f881c92b8cdae8faaf782d0f031
Installing-GCC-9-on-CentOS-7.md · GitHub
If you need GCC newer than version 4.8.5(default version of centos 7), you can get it by using centos-release-scl · yum install -y centos-release-scl yum install -y devtoolset-9 scl enable devtoolset-9 bash
Discussions

linux - how to install gcc 4.9.2 on RHEL 7.4 - Stack Overflow
I am trying to install gcc and g++ 4.9.2 on Linux. I'm pretty new with Linux and i saw some guides of how to install, but each time I encountered with another problem. I don't have any gcc right now on my machine. my Linux version is: Red Hat Enterprise Linux Server release 7.4 (Maipo) can someone help me and give me instructions from the beginning to the end how to do this properly? thank you very much. ... yum ... More on stackoverflow.com
🌐 stackoverflow.com
CentOS Stream 9 gcc dependency and yum repo install error
This problem should be just about solved now, I'm able to perform multiple runs in containers with no issues. Are you able to successfully hit the repos now? (I recommend running dnf clean all first). More on reddit.com
🌐 r/CentOS
2
6
November 11, 2021
installation - How to Install gcc 5.3 with yum on CentOS 7.2? - Stack Overflow
You can use this method to install any dev tool version, just swap the 7 for your desired version. devtoolset-6-gcc, devtoolset-5-gcc etc. ... sudo yum install centos-release-scl sudo yum install devtoolset-9 env PATH="/opt/rh/devtoolset-9/root/usr/bin:${PATH}" More on stackoverflow.com
🌐 stackoverflow.com
rhel - How to install gcc 9.X on RHEL8? - Unix & Linux Stack Exchange
I see from the documentation here that gcc has been updated on RHEL8, but what I haven't figured out is how to get it. The documentation indicates that there are now two streams, but I already hav... More on unix.stackexchange.com
🌐 unix.stackexchange.com
May 7, 2020
Top answer
1 of 4
25

What I have so far:

cat Dockerfile

FROM centos:7 AS env

RUN yum update -y
RUN yum install -y centos-release-scl
RUN yum install -y devtoolset-9

RUN echo "source /opt/rh/devtoolset-9/enable" >> /etc/bashrc
SHELL ["/bin/bash", "--login", "-c"]
RUN gcc --version

So you must:

  1. Add the source stuff in a bashrc
    note: On Centos it's /etc/bashrc while on ubuntu it's /etc/bash.bashrc

  2. Update the docker default shell to be bash AND to "load" the bashrc using --login

Output

docker build .
Sending build context to Docker daemon  4.096kB
Step 1/32 : FROM centos:7 AS env
 ---> 8652b9f0cb4c
Step 2/32 : RUN yum update -y
 ---> Using cache
 ---> a2bb269cd8dc
Step 3/32 : RUN yum install -y centos-release-scl
 ---> Using cache
 ---> 1184e26c71cf
Step 4/32 : RUN yum install -y devtoolset-9
 ---> Using cache
 ---> e678665d2a4e
Step 5/32 : RUN echo "source /opt/rh/devtoolset-9/enable" >> /etc/bashrc
 ---> Using cache
 ---> fe1745d4ca87
Step 6/32 : SHELL ["/bin/bash", "--login", "-c"]
 ---> Running in 2dd7955f4487
Removing intermediate container 2dd7955f4487
 ---> 3cf4835bf680
Step 7/32 : RUN gcc --version
 ---> Running in b5de3266d607
gcc (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2)
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 ...

What won't work

Test 1

RUN scl enable devtoolset-9 bash
RUN gcc --version | head -1

each RUN is a new shell so the sub-bash is lost on the second line.

Test 2

RUN source /opt/rh/devtoolset-9/enable && gcc --version | head -1
RUN gcc --version | head -1

Here again the source is only for the first RUN shell command but will be lost...

Test 3

This may work but with potential unexpected behaviour

ENV PATH=/opt/rh/devtoolset-9/root/bin:$PATH
RUN gcc --version | head -1

here we only "fix" the PATH variable but if you look at the /opt/rh/devtoolset-9/enable script there is so more to do than only updating the PATH...

2 of 4
0

You may give it a try using the below steps if that may help: Download the latest package from http://ftp.gnu.org/gnu/gcc/gcc-9.2.0/

wget http://ftp.gnu.org/gnu/gcc/gcc-9.2.0/gcc-9.2.0.tar.gz

Extract the files using the steps below:

tar -xzvf gcc-9.20.tar.gz
cd gcc-9.2.0

Build a configuration using the below,

./configure

Compile the installation using make and then make install.

make 
make install 
Top answer
1 of 4
22
yum install centos-release-scl-rh
yum install devtoolset-3-gcc devtoolset-3-gcc-c++
update-alternatives --install /usr/bin/gcc-4.9 gcc-4.9 /opt/rh/devtoolset-3/root/usr/bin/gcc 10
update-alternatives --install /usr/bin/g++-4.9 g++-4.9 /opt/rh/devtoolset-3/root/usr/bin/g++ 10
2 of 4
21

For installing the system compilers gcc, g++, the install command is # yum install gcc-c++ → Provides version 4.8.5 : /usr/bin/{ gcc, g++ }.

Other options: 1. gcc53-c++-5.3.0-1.el6.x86_64.rpm → https://drive.google.com/file/d/0B7S255p3kFXNRm9FVnZYUnhyZzg/view?usp=sharing&resourcekey=0-1N6zQa6Sbl_WycG1O9I7JA : Download and install : # cd Downloads/ && yum install ./gcc53-c++-5.3.0-1.el6.x86_64.rpm ..... Provides /usr/bin/{gcc53, g++53}.

  1. The devtoolset´s : https://www.softwarecollections.org/en/scls/rhscl/devtoolset-6/ → # yum-config-manager --enable rhel-server-rhscl-7-rpms

Install gcc, g++ version 4.9.2 : # yum install devtoolset-3-gcc-c++

Note : You can have as many gcc/g++ versions as you want, installed at the same time. ( The system compilers are a must.)


  1. gcc49-c++-4.9.3-1.el6.x86_64.rpm https://drive.google.com/file/d/1Pwq1ua80dGM72i7rpDNAIIdfcR1WK-hG/view?usp=sharing → Provides /usr/bin/{gcc49, g++49}.

  1. gcc63-c++-6.3.0-1.el7.x86_64.rpm https://drive.google.com/file/d/1t4WrgvpEP-6_NN3qMJhz9MS3CJhHrHKc/view?usp=sharing → Provides /usr/bin/{gcc63, g++63}.

  2. gcc45-c++-4.5.4-1.el7.x86_64.rpm https://drive.google.com/file/d/15aRg-BPhuyaEyZA9Jy-iAyC21_pwN7nD/view?usp=sharing → Provides /usr/bin/{gcc45, g++45, gfortran45}

  3. gcc42-c++-4.2.4-1.el6.x86_64.rpm https://drive.google.com/file/d/1eYWk6Nd63xeqqAUoJldNWRuwEGO6cAyv/view?usp=sharing → Provides /usr/bin/{gcc42, g++42}


  1. gcc73-c++-7.3.0-1.el7.x86_64.rpm https://drive.google.com/file/d/1PgwCP5tu8D0EJbJVTqJd7Vg8dJ4l4noi/view?usp=sharing → Provides /usr/bin/{gcc73, g++73}

  2. gcc48-c++-4.8.5-1.el6.x86_64.rpm https://drive.google.com/file/d/1w6fW6oSflDDYZt_cOpGj3QMEmzUC8Q9L/view?usp=sharing → Provides /usr/bin/{gcc48, g++48, gfortran48}

  3. gcc84-c++-8.4.0-1.el7.x86_64.rpm https://drive.google.com/file/d/1xgFtsiDi2uiB1B0AcOaSpxVizzET-pJf/view?usp=sharing → Provides /usr/bin/{gcc84, g++84, gfortran84}

🌐
Readthedocs
base-9.readthedocs.io › en › latest › Installation.html
III. Installation — BASE-9 9.3.0 documentation
linux> sudo yum install gcc gcc-c++ cmake git linux> sudo yum install gsl gsl-devel boost boost-devel ... RHEL 5 & 6 repositories have an old gcc version. The devtoolset package will install an alternate, up to date build environment at /opt/<distro>/devtoolset-2/ ... The scl utility can create ...
🌐
Reddit
reddit.com › r/centos › centos stream 9 gcc dependency and yum repo install error
r/CentOS on Reddit: CentOS Stream 9 gcc dependency and yum repo install error
November 11, 2021 -

While trying to install gcc, I came upon an error. I suspect something wrong with the repo configs, as I can download the rpms it complains about, and manually install them alongside gcc without issue.

EDIT: Nevermind, known bug... but I'll leave this here for anyone who comes looking.

https://lists.centos.org/pipermail/centos-devel/2021-November/077438.html

https://bugzilla.redhat.com/show_bug.cgi?id=2021809

[root@c9stream ~]# cat /etc/os-release
NAME="CentOS Stream"
VERSION="9"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="9"
PLATFORM_ID="platform:el9"
PRETTY_NAME="CentOS Stream 9"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:9"
HOME_URL="https://centos.org/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux 9"
REDHAT_SUPPORT_PRODUCT_VERSION="CentOS Stream"
[root@c9stream ~]# dnf install gcc
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

Last metadata expiration check: 0:10:09 ago on Wed 10 Nov 2021 09:37:06 PM EST.
Error:
 Problem: cannot install the best candidate for the job
  - nothing provides libgomp = 11.2.1-6.el9 needed by gcc-11.2.1-6.el9.x86_64
  - nothing provides libgcc >= 11.2.1-6.el9 needed by gcc-11.2.1-6.el9.x86_64
(try to add '--skip-broken' to skip uninstallable packages or '--nobest' to use not only best candidate packages)
[root@c9stream ~]# curl -sL \
    -O http://mirror.stream.centos.org/9-stream/BaseOS/x86_64/os/Packages/libgomp-11.2.1-6.el9.x86_64.rpm \
    -O http://mirror.stream.centos.org/9-stream/BaseOS/x86_64/os/Packages/libgcc-11.2.1-6.el9.x86_64.rpm
[root@c9stream ~]# ls -l libg*-11.2.1-6.el9.x86_64.rpm
-rw-r--r--. 1 root root 110060 Nov 10 21:47 libgcc-11.2.1-6.el9.x86_64.rpm
-rw-r--r--. 1 root root 288503 Nov 10 21:47 libgomp-11.2.1-6.el9.x86_64.rpm
[root@c9stream ~]# dnf install gcc libg*-11.2.1-6.el9.x86_64.rpm
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

Last metadata expiration check: 0:10:33 ago on Wed 10 Nov 2021 09:37:06 PM EST.
Dependencies resolved.
==================================================================================================================================
 Package                     Architecture               Version                            Repository                        Size
==================================================================================================================================
Installing:
 gcc                         x86_64                     11.2.1-6.el9                       appstream                         31 M
Upgrading:
 libgcc                      x86_64                     11.2.1-6.el9                       @commandline                     107 k
 libgomp                     x86_64                     11.2.1-6.el9                       @commandline                     282 k

Transaction Summary
==================================================================================================================================
Install  1 Package
Upgrade  2 Packages

Total size: 31 M
Total download size: 31 M
Is this ok [y/N]:
🌐
CentOS Repositories
centos.pkgs.org › 7 › centos-sclo-rh-x86_64 › devtoolset-9-gcc-c++-9.3.1-2.el7.x86_64.rpm.html
devtoolset-9-gcc-c++-9.3.1-2.el7.x86_64.rpm CentOS 7 Download
Install devtoolset-9-gcc-c++ rpm package: # yum install devtoolset-9-gcc-c++ 2020-04-08 - Marek Polacek <polacek@redhat.com> 9.3.1-2 - update from Fedora gcc-9.3.1-2 · 2020-04-07 - Marek Polacek <polacek@redhat.com> 9.3.1-1.1 - include the c++/93597 fix - remove several .hidden symbols from ...
🌐
Medium
bipulkkuri.medium.com › install-latest-gcc-on-centos-linux-release-7-6-a704a11d943d
Install latest GCC from source on Centos Linux release | by Bipul Kuri | Medium
August 18, 2020 - sudo yum -y update sudo yum -y install bzip2 wget gcc gcc-c++ gmp-devel mpfr-devel libmpc-devel make gcc --version wget http://mirrors-usa.go-parts.com/gcc/releases/gcc-8.2.0/gcc-8.2.0.tar.gz tar zxf gcc-8.2.0.tar.gz mkdir gcc-8.2.0-build cd ...
Find elsewhere
🌐
cPanel
support.cpanel.net › hc › en-us › community › posts › 19161804558103-How-to-update-default-GCC-on-a-CentOS-7-9
How to update default GCC on a CentOS 7.9? – cPanel
Hi, I updated GCC on my CentOS 7.9 from 4.8.5 to 9 by doing this: yum install centos-release-scl -y yum clean all yum install devtoolset-9-* -y And then I typed this: scl enable devtoolset-9 ...
🌐
Linuxize
linuxize.com › home › gcc › how to install gcc compiler on centos 7
How to Install GCC Compiler on CentOS 7 | Linuxize
October 31, 2019 - To install the Development Tools including the GCC Compiler, run: Terminal · sudo yum group install "Development Tools" The command installs a bunch of new packages including gcc, g++ and make.
🌐
Medium
medium.com › @ihouelecaurcy › how-to-check-and-install-gcc-on-linux-complete-developer-guide-98697e2ab78f
How to Check and Install GCC on Linux: Complete Developer Guide | by Ihouele Caurcy | Medium
July 24, 2025 - # Install GCC only sudo apt install gcc# Install complete development environment (recommended) sudo apt install build-essential# Verify installation gcc --version ... sudo yum install gcc # Or install complete development tools sudo yum ...
🌐
GitHub
gist.github.com › yosoufe › ad45f45c10fe08abecbf53000d0d199f
Compile and install gcc-9.3.0 · GitHub
http://www.linuxfromscratch.org/blfs/view/svn/general/gcc.html ... # requirements sudo apt-get install flex bison mkdir build ./contrib/download_prerequisites --directory=build cd build ../configure \ --prefix=/usr \ --disable-multilib \ --with-system-zlib \ --enable-languages=c,c++,d,fortran,go,objc,obj-c++ \ --program-suffix=-9.3.0 make -j$(nproc) sudo make install -j$(nproc)
🌐
Red Hat
access.redhat.com › documentation › en-us › red_hat_developer_toolset › 9 › html › user_guide › chap-gcc
Chapter 2. GNU Compiler Collection (GCC) | User Guide | Red Hat Developer Toolset | 9 | Red Hat Documentation
March 28, 2022 - To display the manual page for the version included in Red Hat Developer Toolset: $ scl enable devtoolset-9 'man gfortran' C++ Standard Library Documentation — Documentation on the C++ standard library can be optionally installed: # yum install devtoolset-9-libstdc++-docs ·
🌐
CyberITHub
cyberithub.com › install-gcc-and-c-compiler
Easy Steps to Install GCC(C and C++ Compiler) on CentOS 7 | CyberITHub
January 18, 2020 - a)You need to have a running CentOS ... commands. In this tutorial, I am using root access. Firstly you need to update your system using yum update command....
Top answer
1 of 6
233

Update:
Often people want the most recent version of gcc, and devtoolset is being kept up-to-date, so maybe you want devtoolset-N where N={4,5,6,7...}, check yum for the latest available on your system). Updated the cmds below for N=7.

There is a package for gcc-7.2.1 for devtoolset-7 as an example. First you need to enable the Software Collections, then it's available in devtoolset-7:

sudo yum install centos-release-scl
sudo yum install devtoolset-7-gcc*
scl enable devtoolset-7 bash
which gcc
gcc --version
2 of 6
78

Update: Installing latest version of gcc 9: (gcc 9.3.0) - released March 12, 2020:

Same method can be applied to gcc 10 (gcc 10.1.0) - released May 7, 2020

Download file: gcc-9.3.0.tar.gz or gcc-10.1.0.tar.gz

Compile and install:

//required libraries: (some may already have been installed)
dnf install libmpc-devel mpfr-devel gmp-devel

//if dnf install libmpc-devel is not working try:
dnf --enablerepo=PowerTools install libmpc-devel

//install zlib
dnf install zlib-devel*

./configure --with-system-zlib --disable-multilib --enable-languages=c,c++

make -j 8 <== this may take around an hour or more to finish
              (depending on your cpu speed)

make install

Tested under CentOS 7.8.2003 for gcc 9.3 and gcc 10.1

Tested under CentOS 8.1.1911 for gcc 10.1 (may take more time to compile)

Results: gcc/g++ 9.3.0/10.1.0

Installing gcc 7.4 (gcc 7.4.0) - released December 6, 2018:

Download file: https://ftp.gnu.org/gnu/gcc/gcc-7.4.0/gcc-7.4.0.tar.gz

Compile and install:

//required libraries:
yum install libmpc-devel mpfr-devel gmp-devel

./configure --with-system-zlib --disable-multilib --enable-languages=c,c++

make -j 8 <== this may take around 50 minutes or less to finish with 8 threads
              (depending on your cpu speed)


make install

Result:

Notes:

1. This Stack Overflow answer will help to see how to verify the downloaded source file.

2. Use the option --prefix to install gcc to another directory other than the default one. The toplevel installation directory defaults to /usr/local. Read about gcc installation options

🌐
LinuxHostSupport
linuxhostsupport.com › home › how to install gcc on centos 7
How To Install GCC on CentOS 7 | LinuxHostSupport
May 24, 2019 - You need to install the GNU C++ compiler. For Centos “yum install gcc-c++”.