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
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 
🌐
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 install centos... More on stackoverflow.com
🌐 stackoverflow.com
How to update default GCC on a CentOS 7.9?
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... More on forums.cpanel.net
🌐 forums.cpanel.net
3
March 14, 2022
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
Beginner with Centos. What is best way to upgrade GCC?
Centos 7 The latest stable release is 8 and I think Stream is on 9, is there a reason you're using a decade-old release? You could try installing distrobox (or toolbox) and set up a Fedora container for your compiler. This way it won't conflict with anything and you avoid dependency hell. You'll still need to upgrade to 8 at least though, since 7 only ships an old version of podman that probably doesn't even support rootless containers. More on reddit.com
🌐 r/linuxquestions
26
1
October 28, 2022
🌐
GitHub
gist.github.com › nchaigne › ad06bc867f911a3c0d32939f1e930a11
Building GCC 9.2.0 on CentOS 7 · GitHub
This note describes how to build the latest GCC (9.2.0 as of October 2019) from sources on CentOS 7. This should be applicable as is on RHEL 7. For other Linux distributions, adapt as needed. While this is not overly complicated, building GCC takes quite some time. So you might want to plan to do something else while it builds... a coffee break just won't make it. Prerequisites are described here: https://gcc.gnu.org/install...
🌐
LinuxHostSupport
linuxhostsupport.com › home › how to install gcc on centos 7
How To Install GCC on CentOS 7 | LinuxHostSupport
May 24, 2019 - First of all log in to your CentOS 7 VPS via SSH as user root ... GCC can be easily installed from the official CentOS repositories.
🌐
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
devtoolset-9-gcc-c++ - C++ support ... specification and a lot of support for the upcoming C++ specification. Install CentOS SCLo RH repository: yum install centos-release-scl-rh ·...
🌐
CyberITHub
cyberithub.com › install-gcc-and-c-compiler
Easy Steps to Install GCC(C and C++ Compiler) on CentOS 7 | CyberITHub
January 18, 2020 - [root@localhost ~]#cd gcc-9.2.0 [root@localhost ~]#./configure ... Once configuration completed successfully, now it is time to compile the source code by running make command. ... Once all the source code are compiled, it is now time to install ...
🌐
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 - In this section, we will provide instructions about how to install and use multiple versions of GCC on CentOS 7.
Find elsewhere
🌐
Medium
bipulkkuri.medium.com › install-latest-gcc-on-centos-linux-release-7-6-a704a11d943d
Install latest GCC on Centos Linux release 7.6
August 18, 2020 - sudo yum -y update sudo yum -y ... ../gcc-8.2.0/configure --enable-languages=c,c++ --disable-multilib make -j$(nproc) sudo make install gcc --version ......
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}

🌐
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 ...
🌐
cPanel Forums
forums.cpanel.net › cpanel & whm® (for linux® servers) › operating systems
How to update default GCC on a CentOS 7.9? | cPanel Forums
March 14, 2022 - On a CentOS 7 test machine, I also see version 4.8.5. It seems that this is a common question as I see a similar question posted here: I did this to upgrade gcc from 4.8.5 to gcc 7: sudo yum install centos-release-scl sudo yum install devtoolset-7-gcc* source /opt/rh/devtoolset-7/enable This does ...
🌐
nixCraft
cyberciti.biz › nixcraft › howto › centos › centos / rhel 7: install gcc (c and c++ compiler) and development tools
CentOS / RHEL 7: Install GCC (C and C++ Compiler) and Development Tools - nixCraft
April 5, 2024 - To install all the packages belonging to a package group called “Development Tools” use the following command: # yum --setopt=group_package_types=mandatory,default,optional groupinstall "Development Tools" OR # yum --setopt=group_package_types=mandatory,default,optional group install "Development Tools" The yum has changed in Red Hat Enterprise Linux 7/CentOS 7. The package group “Development Tools”” has only the optional packages which by default doesn’t get installed. So we will need to pass the option --setopt=group_package_types=mandatory,default,optional to install the optional packages too. Type the following which command or type command/command command to see the gcc binary location.
🌐
JWillikers
jwillikers.com › build-gcc-from-source-on-centos-7
Build GCC From Source on CentOS 7 - JWillikers
October 28, 2020 - This tutorial provides the steps necessary to compile and install a newer version of GCC, version 10.2.0 to be specific, on CentOS 7. The GCC front-ends for C, C++, and Fortran are included. You should be familiar with command-line tools, CentOS, and the compiling and installing software on Linux.
🌐
Benjamin-berhault
benjamin-berhault.github.io › my_personal_blog › › post › 2018 › 06 › 22 › install-gcc-on-rhel-centos-7.html
Build and Install the Last GCC on RHEL/CentOS 7
June 22, 2018 - tar xzf gcc-9.1.0.tar.gz cd gcc-9.1.0 ./contrib/download_prerequisites cd .. mkdir gcc-9.1.0-build cd gcc-9.1.0-build ../gcc-9.1.0/configure --enable-languages=c,c++ --disable-multilib make sudo make install
Top answer
1 of 2
16

I've confirmed that you can upgrade gcc from the default version 4.8 on centOS 7.

First, we need to install "Software Collections" in order to access some of the community packages including gcc v7

  • sudo yum install -y centos-release-scl

Next, we want to install a developer toolset. Depending on your needs, you may want a different devtoolset. Here I'm targeting 7:

  • sudo yum install -y devtoolset-7

Finally, you'll want to change over to gcc 7 as your default, launch a new shell session with the scl tool:

  • scl enable devtoolset-7 bash
2 of 2
1

Enable the software collection in the answer is only effective in the current shell. The scl utility will create a "child-shell" that set the PATH variables properly, so that in the new child-shell, the enabled software collections will be firstly searched. These settings obviously only take effective temporarily in the current shell.

To make it permanently effective, add the command, source /opt/rh/devtoolset-7/enable to the user's profile (~/.bash_profile or ~/.bashrc for RHEL based OS, like CentOS 7). Then, start a new shell and you will have the right tools available.

After execute scl enable devtoolset-7 bash, you will need to execute exit twice to exit the opened shell window, which verifies that the scl command created a new shell instance as a child process. There might be side-effect with creating a child-shell, so do not put this command in the ~/.bashrc profile, otherwise it will repeatedly create child-shell (non-login shell) as each shell will load the profile, resulting in a endless recursive loop. Put it in ~/.bash_profile, it will be loaded for only once (for the login shell), but you will need to exit twice every time.

But for development purpose, scl enable devtoolset-7 bash would be preferred, as you can exit the created child-shell, and then switch between different versions of the same software.


More details about the GCC version in python terminal:

The version info of the built-in Python in CentOS 7:

[root@conda condabuilder]# python
Python 2.7.5 (default, Nov 16 2020, 22:23:17) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

The version info of the user installed (via conda) Python on a system even without higher version of GCC installed:

[root@conda condabuilder]# conda activate jupyter
(jupyter) [root@conda condabuilder]# python -VV
Python 3.10.9 | packaged by conda-forge | (main, Feb  2 2023, 20:20:04) [GCC 11.3.0]

From the results, we can see that the GCC version contained in Python's version info is not related to the system's GCC. The system's default Python (2.7.5) should have been compiled with the GCC version distributed with CentOS 7, so the version info show the same GCC version. But for user installed python, the GCC version info actually depends on what version of GCC is used for building and packging the python binary.

🌐
jdhao's digital space
jdhao.github.io › 2017 › 09 › 04 › install-gcc-newer-version-on-centos
How to Compile and Install Latest Version of GCC on CentOS 7 · jdhao's digital space
April 20, 2019 - The default GCC that comes with the CentOS 7.2 is GCC 4.8.5, which does not support the complete C++11 standard, for example, it does not fully support regular expressions. In order to use regular expression functions, we need to install at least GCC 4.9.0. The following installation procedure is applicable to CentOS 7 and are not tested on other Linux systems.
🌐
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]:
🌐
Medium
medium.com › @zilch0 › gcc-install-gcc-4-9-4-on-centos-7-ce83f5240fe7
[gcc] compile and install gcc 4.9.4 on CentOS 7 - zilch0 - Medium
August 13, 2021 - [gcc] compile and install gcc 4.9.4 on CentOS 7 install dependencies yum update yum install -y glibc-static libstdc++-static yum install gcc gcc-c++ yum install bzip2 2. download gcc 4.9.4 source …
🌐
GitHub
gist.github.com › craigminihan › b23c06afd9073ec32e0c
Build GCC 4.9.2 for C/C++ on CentOS 7 · GitHub
... The command sequence below worked on my computer to update to version 7: sudo yum -y install centos-release-scl sudo yum -y install devtoolset-7 scl enable devtoolset-7 bash gcc --version