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
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
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 executeexittwice to exit the opened shell window, which verifies that thesclcommand 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~/.bashrcprofile, 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.
Installing gcc versions on Centos7 / Redhat7 - Programming - Linux Tips
installation - How to Install gcc 5.3 with yum on CentOS 7.2? - Stack Overflow
How to change gcc version without root on CentOS 7 - Unix & Linux Stack Exchange
How to update default GCC on a CentOS 7.9?
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
Edit: What happened? When I went to download centos I mistakenly understood centos 7 as being the most recent stable version. Original post below:
Ok, I have only used the mint distro as of yet (for about a year now). I wanted to start getting to know other distros so I repaired an older funky pc's power supply and changed out the windows 7 on it for Centos with Gnome desktop just yesterday. So Centos is new to me and mint has been my experience (but I am still a novice with it).
I checked out the GCC and it's version 4.8.5 . On my mint laptop I was able to install version 10.3.0 through the apt package manager. The search results I get from using yum though do not seem to spit out anything similar as far as I can tell. And the google searches I do seem to all point towards installing from source.
But a friend of mine the other day when discussing how I used my laptop in mint was very emphatic that I should avoid as much as possible installing anything from source that was available already through a package manager, saying that when things later become updated it is much safer and less of a mess to have the package manager handle the changes.
So my noob question is this... Does that type of thinking not apply to the Centos distro? Or is the purpose of Centos less intended for developers? Or something else?
In the end I am too ignorant in this current situation to really know what I am ignorant of so bottom line question is:
In Centos 7 What is the most recommended way to upgrade GCC?
Thanks
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