The scl enable ... command creates a new shell; you've probably ended up with several nested layers of shells if you've been testing this. If you exit, you'll probably see the gcc --version output from the base system. See how deeply-nested you are with something like pstree -s $$.

To run gcc --version with scl, just put the command on the scl line:

 scl enable devtoolset-9 'gcc --version'

Reference: The Red Hat Developer Toolset 9 User Guide (pdf).

Answer from Jeff Schaller on Stack Exchange
🌐
Red Hat
docs.redhat.com › en › documentation › 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
Red Hat Developer Toolset is distributed with GCC 9.3.1. This version is more recent than the version included in Red Hat Enterprise Linux and provides a number of bug fixes and enhancements. In Red Hat Developer Toolset, the GNU C compiler is provided by the devtoolset-9-gcc package and is automatically installed with devtoolset-9-toolchain as described in Section 1.5, “Installing Red Hat Developer Toolset”.
🌐
Red Hat
docs.redhat.com › en › documentation › red_hat_developer_toolset › 9 › html › 9.0_release_notes › dts9.0_release
Chapter 2. Red Hat Developer Toolset 9.0 Release | 9.0 Release Notes | Red Hat Developer Toolset | 9 | Red Hat Documentation
To use the ccache utility with GCC included in Red Hat Developer Toolset, set your environment correctly. For example: ~]$ scl enable devtoolset-9 '/usr/lib64/ccache/gcc -c foo.c'
🌐
Red Hat
access.redhat.com › documentation › en-us › red_hat_developer_toolset › 9 › html-single › user_guide › index
User Guide | Red Hat Developer Toolset | 9 | Red Hat Documentation
August 7, 2020 - Red Hat Developer Toolset is distributed with GCC 9.3.1. This version is more recent than the version included in Red Hat Enterprise Linux and provides a number of bug fixes and enhancements. In Red Hat Developer Toolset, the GNU C compiler is provided by the devtoolset-9-gcc package and is automatically installed with devtoolset-9-toolchain as described in Section 1.5, “Installing Red Hat Developer Toolset”.
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 
🌐
Centos
cbs.centos.org › koji › buildinfo
devtoolset-9-gcc-9.3.1-2.2.el7 | Build Info | CentOS Community Build Service
August 5, 2021 - Main Site Links: · Summary · Packages · Builds · Tasks · Build Targets · Users · Hosts · Reports · Search
🌐
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
Find elsewhere
🌐
Tenable
tenable.com › plugins › nessus › 170330
RHEL 7 : devtoolset-9-gcc (RHSA-2020:2274)<!-- --> | Tenable®
January 23, 2023 - The following packages have been upgraded to a later upstream version: devtoolset-9-gcc (9.3.1). Security Fix(es): * gcc: POWER9 DARN RNG intrinsic produces repeated output (CVE-2019-15847) For more details about the security issue(s), including the impact, a CVSS score, acknowledgments, and other related information, refer to the CVE page(s) listed in the References section.
🌐
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
🌐
Hacker News
news.ycombinator.com › item
For the record, CentOS 7 can have gcc-9 by installing devtoolset-9. To get acces... | Hacker News
August 17, 2020 - But people delivering libraries to customers who have elected not to upgrade to current releases are pretty much stuck. It is extremely common for banks, in particular, to still be running gcc-4, and to have no plans ever to upgrade · It is common at least in banks to have an unbreakable policy ...
🌐
Red Hat
developers.redhat.com › articles › 2025 › 04 › 16 › gcc-and-gcc-toolset-versions-rhel-explainer
GCC and gcc-toolset versions in RHEL: An explainer | Red Hat Developer
April 16, 2025 - Shorter support life cycle: gcc-toolset versions are supported for only about 2 years from release, following the Application Streams life cycle. More frequent updates: New gcc-toolset versions are released more often, typically starting 6 months after a major version of RHEL is released, providing access to newer GCC features. Package dependencies: gcc-toolset installs a full suite of dependent packages (e.g., gcc-toolset-9-*), while the built-in gcc does not force installation of these additional packages.
🌐
Oracle Linux
yum.oracle.com › repo › OracleLinux › OL7 › SoftwareCollections › x86_64
Oracle Linux 7 (x86_64) Software Collection 3.0 | Oracle, Software. Hardware. Complete.
Oracle Linux with Oracle enterprise-class support is the best Linux operating system (OS) for your enterprise computing needs.
🌐
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 bash and the "gcc -v" command does return version 9, but every time I restart the dedicated server it ...