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:
Add the source stuff in a bashrc
note: On Centos it's/etc/bashrcwhile on ubuntu it's/etc/bash.bashrcUpdate 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...
linux - how to install gcc 4.9.2 on RHEL 7.4 - Stack Overflow
CentOS Stream 9 gcc dependency and yum repo install error
installation - How to Install gcc 5.3 with yum on CentOS 7.2? - Stack Overflow
rhel - How to install gcc 9.X on RHEL8? - Unix & Linux Stack Exchange
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:
Add the source stuff in a bashrc
note: On Centos it's/etc/bashrcwhile on ubuntu it's/etc/bash.bashrcUpdate 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...
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
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
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}.
- 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.)
- 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}.
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}.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}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}
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}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}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}
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]: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
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