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.

Answer from Florian Weimer on Stack Overflow
🌐
LinuxHostSupport
linuxhostsupport.com › home › how to install gcc on centos 7
How To Install GCC on CentOS 7 | LinuxHostSupport
May 24, 2019 - First of all log in to your CentOS 7 VPS via SSH as user root ... GCC can be easily installed from the official CentOS repositories. Run the following command to install GCC on your server
🌐
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 - The SCL repositories provide a package named Developer Toolset, which includes newer versions of the GNU Compiler Collection, and other development and debugging tools. First, install the CentOS SCL release file. It is part of the CentOS extras repository and can be installed by running the following command: ... In this example, we’ll install the Developer Toolset version 7. To do so type the following command on your CentOS 7 terminal:
Discussions

Installing gcc versions on Centos7 / Redhat7 - Programming - Linux Tips
You can install these newer versions with Devtoolset-4 from the Software Collections project. First of all enable software 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 ... More on linux-tips.com
🌐 linux-tips.com
0
September 20, 2018
yum - Centos 7 gcc 8 installation - Unix & Linux Stack Exchange
yum install centos-release-scl yum install devtoolset-8-gcc devtoolset-8-gcc-c++ scl enable devtoolset-8 -- bash · It's explained in a little more detail in this answer on StackOverflow: How to install GCC/G++ 8 on CentOS. More on unix.stackexchange.com
🌐 unix.stackexchange.com
October 23, 2018
How to install gcc/g++ 9 on CentOS 7 docker (centos:7) - Stack Overflow
In order to use the C++17 include I need gcc-9 package in my centos 7 docker. By default centos:7 will install gcc 4.8.5 from the regular distro repo. docker run --rm -it centos... More on stackoverflow.com
🌐 stackoverflow.com
Centos on new mac m1 laptops
VMs are just barely working on the M1 so far. Someone got Windows (ARM) running in QEMU with some patches. Others have gotten Linux (ARM) running through hyperkit, but again it is very early stages. You won’t be launching a VM with just a friendly UI or CLI command for a while. Once Docker, Parallels, VMWare or Virtualbox support M1 it should be fine to run an ARM version of CentOS on one of those. But it could be a while. More on reddit.com
🌐 r/mac
4
4
November 27, 2020
🌐
VietHosting
viethosting.com › home › tutorials › servers › how to install g++ (gcc c++) on centos via command line
How to Install g++ (GCC C++) on CentOS via Command Line
April 8, 2026 - To install the g++ compiler on CentOS, AlmaLinux, or Rocky Linux, use one of the following commands: 1. Install the full build environment (Recommended): sudo dnf group install "Development Tools" 2.
🌐
Stevedoria
blog.stevedoria.net › 20180214 › how-to-install-gcc-7-on-centos-7
How to Install GCC 7 on CentOS 7 - stevedoria.net
February 14, 2018 - Installing centos-release-scl and devtoolset-7-gcc-c++ packages allows me to use a recent version of gcc. To determine the default C++ standard used by g++: 1. $ g++ -dM -E -x c++ /dev/null | \ grep -F __cplusplus ... This entry was posted on Wednesday, February 14th, 2018 at 2:03 am and is ...
🌐
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.
🌐
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 - Note: You need to log in to your CentOS 7 VPS via SSH as user root ... GCC can be easily installed from the official CentOS repositories. Run the following command to install GCC on your server
Call   800-933-1517
Address   611 Wilshire Blvd #300, 90017, Los Angeles,
🌐
Linux Tips
linux-tips.com › programming
Installing gcc versions on Centos7 / Redhat7 - Programming - Linux Tips
September 20, 2018 - First of all enable software 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 ...
Find elsewhere
🌐
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 - We need to install 3 dependencies packages. It is recommended to install these packages through yum. ... Unlike other packages, it is recommended to create another build directory outside of the GCC source directory to build 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 - Use the yum command as follows: ##[ CentOS 7 ] ## $ sudo yum install man-pages man-db man ##[ CentOS 6.x ] ## $ sudo yum install man man-pages · You learned how to install GNU c, c++ compilers and releated tools on a CentOS or Red Hat Enterprise Linux version 7.x.
🌐
Dotlinux
dotlinux.net › blog › how-to-install-gui-gnome-on-centos-7-linux-system
How to Install GUI Gnome on CentOS 7 Linux System
It offers a clean, intuitive interface with essential tools for productivity, system management, and customization. In this guide, we’ll walk through the step-by-step process of installing the GNOME GUI on a CentOS 7 system, ensuring you can seamlessly switch from CLI to a graphical workflow.
🌐
USAVPS
usavps.com › home › linux › installing gcc and g++ online via yum on centos 7.5
Installing GCC and G++ online via yum on CentOS 7.5 - USAVPS
September 27, 2024 - Learn how to install GCC and G++ online using yum on CentOS 7.5 for efficient C and C++ development. Step-by-step guide included.
🌐
Medium
medium.com › @zilch0 › gcc-install-gcc-4-9-4-on-centos-7-ce83f5240fe7
[gcc] compile and install gcc 4.9.4 on CentOS 7 - zilch0 - Medium
August 13, 2021 - [gcc] compile and install gcc 4.9.4 on CentOS 7 install dependencies yum update yum install -y glibc-static libstdc++-static yum install gcc gcc-c++ yum install bzip2 2. download gcc 4.9.4 source …
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 › superzscy › ea619f881c92b8cdae8faaf782d0f031
Installing-GCC-9-on-CentOS-7.md · GitHub
If you need GCC newer than version 4.8.5(default version of centos 7), you can get it by using centos-release-scl · yum install -y centos-release-scl yum install -y devtoolset-9 scl enable devtoolset-9 bash · If you restart the shell, it will revert to GCC 4.8.5. You can add "source /opt/rh/devtoolset-9/enable" into you bashrc file to set GCC 9 as the default. Sign up for free to join this conversation on GitHub.
🌐
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 · Show hidden characters · Copy link · it works in my case. thanks! Sign up for free to join this conversation on GitHub.
🌐
Linuxize
linuxize.com › home › gcc › how to install gcc (development tools) on centos 8
How to Install GCC (Development Tools) on CentOS 8 | Linuxize
March 9, 2020 - The default CentOS repositories contain a package group named “Development Tools” that includes the GNU compiler collection, GNU debugger, and other development libraries and tools required for compiling software. To install the Development Tools packages, run the following command as root or user with sudo privileges :