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
Answer from tesch1 on Stack Overflow
🌐
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 - You can download the GCC source code from the official GNU ftp. I choose to install version 5.4.0. curl https://ftp.gnu.org/gnu/gcc/gcc-5.4.0/gcc-5.4.0.tar.bz2 -O tar jxvf gcc-5.4.0.tar.bz2
Top answer
1 of 6
233

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
2 of 6
78

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

🌐
Stack Overflow
stackoverflow.com › questions › 70264536 › gcc-gcc-5-4-0-build-on-centos7
c++ - GCC gcc-5.4.0 build on centos7 - Stack Overflow
Thanks, for your guidance. I tried with higher version GCC 7.0, enable internet on server and run "./contrib/download_prerequisites". It resolved issue and GCC installed sucessfully.
🌐
LinuxHostSupport
linuxhostsupport.com › home › how to install gcc on centos 7
How To Install GCC on CentOS 7 | LinuxHostSupport
May 24, 2019 - In this tutorial, we will take a look at how to install GCC on CentOS 7. GCC or GNU Compiler Collection is released by the Free Software Foundation and as
🌐
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.
🌐
CyberITHub
cyberithub.com › install-gcc-and-c-compiler
Easy Steps to Install GCC(C and C++ Compiler) on CentOS 7 | CyberITHub
January 18, 2020 - In this article I will take you through the steps to install GCC on CentOS 7. As per GCC Document ,The GNU Compiler Collection includes front ends for C, C++,
🌐
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 - We’ll explain how to install the distro stable version and the newer version of GCC available from the SCL repository. To add new repositories and install packages on your CentOS system, you must be logged in as root or user with sudo privileges .
Top answer
1 of 1
4

I don't think it's possible to install GCC-5 on CentOS 5, its glibc version (2.5) is way too old.

For CentOS 6, use the following instructions:

  • http://en.librehat.com/blog/build-gcc-5-dot-2-on-rhel-6/
  • http://www.linuxfromscratch.org/blfs/view/cvs/general/gcc.html

Or you can use Docker with the following Dockerfile:

FROM centos:centos6

RUN yum install -y gcc gcc-c++ wget xz libgcc glibc-devel glibc-headers

# Run some tests
RUN gcc --version && \
    g++ --version && \
    which gcc && \
    which g++
RUN mkdir ~/tests && \
    cd ~/tests && \
    echo '#include <iostream>' > main.cpp && \
    echo 'using namespace std;' >> main.cpp && \
    echo 'int main() {' >> main.cpp && \
    echo '    cout << "Hello world!" << endl;' >> main.cpp && \
    echo '    return 0;' >> main.cpp && \
    echo '}' >> main.cpp && \
    g++ main.cpp -o main && \
    ./main

# Download and compile GCC-5
# http://en.librehat.com/blog/build-gcc-5-dot-2-on-rhel-6/

# Download and extract source code
ENV gcc_version "5.3.0"
RUN wget --no-verbose \
        http://ftpmirror.gnu.org/gcc/gcc-${gcc_version}/gcc-${gcc_version}.tar.bz2 && \
    tar xf gcc-${gcc_version}.tar.bz2
RUN wget --no-verbose \
        https://gmplib.org/download/gmp/gmp-6.1.0.tar.xz && \
    tar xf gmp-6.1.0.tar.xz && \
    mv gmp-6.1.0 gcc-${gcc_version}/gmp
RUN wget --no-verbose \
        ftp://ftp.gnu.org/gnu/mpc/mpc-1.0.3.tar.gz && \
    tar xf mpc-1.0.3.tar.gz && \
    mv mpc-1.0.3 gcc-${gcc_version}/mpc
RUN wget --no-verbose \
        http://www.mpfr.org/mpfr-current/mpfr-3.1.4.tar.xz && \
    tar xf mpfr-3.1.4.tar.xz && \
    mv mpfr-3.1.4 gcc-${gcc_version}/mpfr

# Compile and install GCC
# "we highly recommend that GCC be built into a separate directory from the sources which does not reside within the source tree"
RUN mkdir gcc-${gcc_version}_build && \
    cd gcc-${gcc_version}_build && \
    ../gcc-${gcc_version}/configure \
        --prefix=/usr \
        --disable-multilib \
        --enable-languages=c,c++ \
        --enable-libstdcxx-threads \
        --enable-libstdcxx-time \
        --enable-shared \
        --enable-__cxa_atexit \
        --disable-libunwind-exceptions \
        --disable-libada \
        --host x86_64-redhat-linux-gnu \
        --build x86_64-redhat-linux-gnu \
        --with-default-libstdcxx-abi=gcc4-compatible
RUN cd gcc-${gcc_version}_build && make -j4
RUN cd gcc-${gcc_version}_build && make install

# Validate the installed compiler
RUN hash -r && \
    gcc --version && \
    g++ --version && \
    which gcc && \
    which g++

# Register new libraries with `ldconfig`
RUN echo "/usr/local/lib64" > usrLocalLib64.conf && \
    mv usrLocalLib64.conf /etc/ld.so.conf.d/ && \
    ldconfig


# Clean out all the garbage
RUN rm -rf ~/${gcc_release} ~/{gcc_release}_build ~/tests
Find elsewhere
🌐
WPcademy
wpcademy.com › home › how to install gcc on centos 7 step by step
How To Install GCC on CentOS 7 Step by Step - WPcademy
May 1, 2019 - This tutorial assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo’ to the commands to get root privileges. I will show you through the step by step install GCC Compiler on CentOS 7 server.
🌐
Psychz
psychz.net › client › kb › en › how-to-install-the-gcc-compiler-in-centos-7.html
How to install the GCC compiler in CentOS 7? ...
March 17, 2019 - GCC can be easily installed from the official CentOS repositories. Run the following command to install GCC on your server ... Running transaction test Transaction test succeeded Running transaction Installing : cpp-4.8.5-36.el7.x86_64 1/5 ...
Call   800-933-1517
Address   611 Wilshire Blvd #300, 90017, Los Angeles,
🌐
Webfaction
community.webfaction.com › questions › 20158 › installing-gcc-54
Installing GCC 5.4 - WebFaction Community
July 15, 2016 - I need a new compiler, yet again. How do I compile GCC 5.4 on CentOS 6 or CentOS 7? (I'm interested in GCC 5 because that's the version currently used in the latest Ubuntu LTS, so I know it has been and will continue to be well-tested with a lot of software).
Top answer
1 of 2
11

you can create repo from fedora and install gcc 5.1.1, it will be better than compile it.

cat << EOF > /etc/yum.repos.d/Fedora-Core23.repo
[warning:fedora]
name=fedora
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=fedora-23&arch=$basearch
enabled=1
gpgcheck=0
EOF

then install gcc

yum install gcc --enablerepo=warning:fedora

output:

========================================================================================================================================================================================================
Package                                           Arch                                      Version                                            Repository                                         Size
========================================================================================================================================================================================================
Installing:
gcc                                               x86_64                                    5.1.1-4.fc23                                       warning:fedora                                     19 M
Installing for dependencies:
cpp                                               x86_64                                    5.1.1-4.fc23                                       warning:fedora                                    8.5 M
glibc-devel                                       x86_64                                    2.22-2.fc23                                        warning:fedora                                    909 k
glibc-headers                                     x86_64                                    2.22-2.fc23                                        warning:fedora                                    497 k
isl                                               x86_64                                    0.14-4.fc23                                        warning:fedora                                    490 k
kernel-headers                                    x86_64                                    4.2.0-1.fc23                                       warning:fedora                                    992 k
libmpc                                            x86_64                                    1.0.2-4.fc23                                       warning:fedora                                     55 k
mpfr                                              x86_64                                    3.1.3-1.fc23                                       warning:fedora                                    213 k

Updating for dependencies:
binutils                                          x86_64                                    2.25-13.fc23                                       warning:fedora                                    5.6 M
glibc                                             x86_64                                    2.22-2.fc23                                        warning:fedora                                    3.6 M
glibc-common                                      x86_64                                    2.22-2.fc23                                        warning:fedora                                     11 M
libgcc                                            x86_64                                    5.1.1-4.fc23                                       warning:fedora                                     82 k
libgomp                                           x86_64                                    5.1.1-4.fc23                                       warning:fedora                                    146 k

Transaction Summary
========================================================================================================================================================================================================
Install  1 Package  (+7 Dependent packages)
Upgrade             ( 5 Dependent packages)

Total download size: 52 M
Is this ok [y/d/N]: y

Why do you preferred on gcc 5.2?

# gcc --version
gcc (GCC) 5.1.1 20150618 (Red Hat 5.1.1-4)
Copyright (C) 2015 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.
2 of 2
6

nano /etc/yum.repos.d/FedoraRepo.repo

[warning:fedora]
name=fedora
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=fedora-23&arch=$basearch
enabled=1
gpgcheck=1
gpgkey=https://getfedora.org/static/34EC9CBA.txt

yum update gcc g++

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.

🌐
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...
🌐
Programmersought
programmersought.com › article › 6811515424
How to install gcc in Centos7 - Programmer Sought
cd gcc-build-5.4.0 ./contrib/download_prerequisites · Fourth, create a folder to store the compiled file ... Note: This process is very time consuming, and my 1 core 1G memory took about an hour. ... /usr/local/src/node-v8.7.0/out/Release/mksnapshot: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' ...
🌐
Programmersought
programmersought.com › article › 70285890922
Centos7 compile and install gcc-5.4.0 - Programmer Sought
Due to poor network conditions, I couldn’t download these files for a long time, so I interrupted many times, deleted the downloaded files, and re-executed./contrib/download_prerequisitsCommand, configure some options in configure after the download is complete, I did not use --prefix, inmake installWill be automatically installed to/usr/local/gcc-5.4.0The compilation process is very long. My laptop took two or three hours, and the company’s computer only took one hour. After compiling and installing, the computer needs to be restarted: ... After restarting, you can passgcc -vLet’s check the gcc version, it’s already 5.4.0, so I decided to write a small program to test it.