Installing gcc versions on Centos7 / Redhat7 - Programming - Linux Tips
installation - How to Install gcc 5.3 with yum on CentOS 7.2? - Stack Overflow
gcc - how to install gcc7 in docker centos7 - Stack Overflow
How to install GCC/G++ 8 on CentOS - Stack Overflow
Videos
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
FROM centos:centos7
RUN yum update -y
RUN yum groupinstall "Development Tools" -y
RUN yum install wget -y
RUN curl -O https://ftp.gnu.org/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz
RUN tar xzf gcc-7.3.0.tar.gz
RUN cd gcc-7.3.0
RUN ./contrib/download_prerequisites
RUN cd ..
RUN mkdir gcc-build
RUN cd gcc-build
RUN ../gcc-7.3.0/configure \
--enable-shared \
--enable-threads=posix \
--enable-__cxa_atexit \
--enable-clocale=gnu \
--disable-multilib \
--enable-languages=all
RUN make
# (Go make a cup of ice tea :)
RUN make install
To save the build time you can create a new docker from the running docker using "docker commit" or save /usr/local to a tar file and open it on any other fresh centos7 docker.
Following commands in Dockerfile worked for me:
RUN yum install -y centos-release-scl
RUN yum install -y devtoolset-7-gcc-*
RUN echo "source scl_source enable devtoolset-7" >> /etc/bashrc
RUN source /etc/bashrc
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.
CentOS 8 already comes with GCC 8.
On CentOS 7, you can install GCC 8 from Developer Toolset. First you need to enable the Software Collections repository:
yum install centos-release-scl
Then you can install GCC 8 and its C++ compiler:
yum install devtoolset-8-gcc devtoolset-8-gcc-c++
To switch to a shell which defaults gcc and g++ to this GCC version, use:
scl enable devtoolset-8 -- bash
You need to wrap all commands under the scl call, so that the process environment changes performed by this command affect all subshells. For example, you could use the scl command to invoke a shell script that performs the required actions.
Permanently adding DTS to your development environment
After installing the devtoolset:
yum install devtoolset-8-gcc devtoolset-8-gcc-c++
You can also use the following command, to make DTS the default:
source scl_source enable devtoolset-8
The benefit of this command is that it can be added to .bashrc, so that you don't have to run the scl command every time you login:
scl enable devtoolset-8 -- bash