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 ExchangeThe 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).
to get the script to act as you want it to, you need the scl command to modify the current bash session rather than spawn a new one.
you can do it this way
#!/usr/bin/env bash
# switch to GCC9 environment for the duration of the script
source scl_source enable devtoolset-9
gcc --version
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
I am using CentOS 7.9 and I encountered the same problem after following instructions here to install and run gcc 11. I tried launching different versions of gcc and found only devtoolset-9 works, which corresponds to the file devtoolset-9 in /etc/scl/conf/ folder. So I copied devtoolset-9 to devtoolset-11 in the same folder, and gcc 11 gets working.
I ran into the same issue that you are facing and this is how I got it fixed:
- Just want to be careful, you need to exist the Terminal and open a fresh one to start. This way, you are not under any devtoolset's bash.
- Go to /opt/rh folder, run command ls -la to see if you have any devtoolset-* folder there. Let's say you have devtoolset-8, proceed step 2.
- Go to /etc/scl/prefixes folder, if you don't see devtoolset-8 file, you can create a new one as devtoolset-8, and type 1 line: /opt/rh, then save and quit that file.
- Once you are done, you can call: scl enable devtoolset-8 -- bash w/o any error. Good luck
You will have to do it the old fashioned way. Get the GCC sources, build it by hand.
Any reason you are averse to doing this? (I can think of a few, but unless you do too, I don't want to add anything that might be confusing).
If you are hesitant to build from sources, I can probably provide some steps.
NOTE: The reason for that is the devtools with 4.9 is currently only available to Red Hat users with a valid license. Wait for a while, and eventually I am sure 4.9 devtools will be made available to CentOS users too like 4.8 is currently.
CentOS 6/7 officially supports this. You don't need third party repository. And gcc version is 4.9.2 in CentOS's repo.
yum install centos-release-scl-rh
yum install devtoolset-3-gcc devtoolset-3-gcc-c++
To use the updated tools, start a new shell using the scl command:
scl enable devtoolset-3 bash
You may also need to reset your PATH environment variable, which might be set in ~/.bashrc or other profile file. If you need to change it, it should point to /opt/rh/devtoolset-3/root/usr/bin/:$PATH
More information on the SCL from CentOS and from Red Hat