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
Answer from nulltron on Stack Exchange
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.

🌐
GitHub
gist.github.com › nchaigne › ad06bc867f911a3c0d32939f1e930a11
Building GCC 9.2.0 on CentOS 7 · GitHub
Building GCC 9.2.0 on CentOS 7. GitHub Gist: instantly share code, notes, and snippets.
Discussions

Installing gcc versions on Centos7 / Redhat7 - Programming - Linux Tips
You can install these newer versions ... collections repo: sudo yum install centos-release-scl Now you can install gcc-6.x, gcc-7.x etc. with proper parameter like below: sudo yum install devtoolset-6-gcc* This method brings newer versions without uninstalling or ov..... More on linux-tips.com
🌐 linux-tips.com
0
September 20, 2018
installation - How to Install gcc 5.3 with yum on CentOS 7.2? - Stack Overflow
I am using CentOS 7.2 When I use yum groupinstall "Development Tools", gcc version is 4.8.5, like this: I would like to install gcc 5.3 How to approach this with yum? More on stackoverflow.com
🌐 stackoverflow.com
How to change gcc version without root on CentOS 7 - Unix & Linux Stack Exchange
I have access to a cluster with CentOS 7, which has gcc installed. Unfortunately, gcc version is pretty old (4.8), and is reported incompatible by some of the Python packages I am using, requiring More on unix.stackexchange.com
🌐 unix.stackexchange.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
🌐
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 - gcc (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5) Copyright (C) 2017 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.
🌐
LinuxHostSupport
linuxhostsupport.com › home › how to install gcc on centos 7
How To Install GCC on CentOS 7 | LinuxHostSupport
May 24, 2019 - As might be seen from the output, the GCC version distributed by CentOS 7 is 4.8.5 which is not the latest version of GCC.
🌐
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 - You learned how to install GNU c, c++ compilers and releated tools on a CentOS or Red Hat Enterprise Linux version 7.x. See GCC site for more info.
🌐
Linux Tips
linux-tips.com › programming
Installing gcc versions on Centos7 / Redhat7 - Programming - Linux Tips
September 20, 2018 - Centos7 and Redhat7 still uses too old gcc 4.8.5 version and this becomes very annoying if you want to use newer features in your C/C++ programs. You can install these newer versions with Devtoolset-4 from the Software Collections project.
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

Find elsewhere
🌐
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.
🌐
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 ~]# gcc --version gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39) Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions.
🌐
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 - *Update Tested for gcc 9.2.0 version · Following are the steps documented for upgrading gcc on centos 7 · Let’s update the box · sudo yum -y update · Now that the update is done lets install the dependencies needed for the build.This is ...
🌐
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 - Running transaction test Transaction test succeeded Running transaction Installing : cpp-4.8.5-36.el7.x86_64 1/5 Installing : kernel-headers-3.10.0-957.5.1.el7.x86_64 2/5 Installing : glibc-headers-2.17-260.el7_6.3.x86_64 3/5 Installing : glibc-devel-2.17-260.el7_6.3.x86_64 4/5 Installing : gcc-4.8.5-36.el7.x86_64 5/5 Verifying : glibc-devel-2.17-260.el7_6.3.x86_64 1/5 Verifying : gcc-4.8.5-36.el7.x86_64 2/5 Verifying : kernel-headers-3.10.0-957.5.1.el7.x86_64 3/5 Verifying : cpp-4.8.5-36.el7.x86_64 4/5 Verifying : glibc-headers-2.17-260.el7_6.3.x86_64 5/5 Installed: gcc.x86_64 0:4.8.5-36.el7
Call   800-933-1517
Address   611 Wilshire Blvd #300, 90017, Los Angeles,
🌐
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 ...
🌐
Reddit
reddit.com › r/linuxquestions › beginner with centos. what is best way to upgrade gcc?
r/linuxquestions on Reddit: Beginner with Centos. What is best way to upgrade GCC?
October 28, 2022 -

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

Top answer
1 of 6
6
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.
2 of 6
2
Why are you using CentOS 7, it was release on July 7, 2014? I currently don't know what GCC version CentOS 8 (stream) has, but CentOS 9 stream has 11.3.1-2 currently. I guess until CentOS 7 EOL there won't be any big GCC version updates, as CentOS is a stable distribution, compared to mint which gets bigger version updates way more often. CentOS (was) downstream from RHEL, which is intended to run on professional servers for years on end. Thus, it "only" gets security updates, but no big / breaking version bumps. For servers, stability is key, once they're set up, they are supposed to run until the OS goes EOL (or the server is faded out), in this case 10 years. Mint is based on Ubuntu which is also mostly stable, but every spring and fall there is a new version (Ubuntu 21.10 (fall 2021), Ubuntu 22.04(spring 2022), Ubuntu 22.10(fall 2022), etc.) thus mint is at most on a half year old GCC version. On the other end of the spectrum there are distros like arch and openSUSE Tumbleweed which are so-called "rolling releases". They only have one version, the current one. And Packages are always kept up to date, as soon as the developers release a new one. Should you compile a new GCC version? Probably not, too much potential for things to break, except you want to learn about how Linux works... Like this Guy: https://reddit.com/r/linux/comments/ye9h57/latest_gentoo_release_running_an_11_year_old/
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 › liuziangexit › 888c818a66409e9bbe835002f3c785c7
install gcc7 in centos · GitHub
install gcc7 in centos · Raw · gistfile1.txt · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ·