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.
According to the man page for gcc-toolset-11, it only supports the enable command. It also states that you can run scl gcc-toolset-11 enable bash to execute the bash command with the gcc-toolset-11 environment, which should be equivalent to what the load command would do.
Example:
[root@localhost ~]# scl enable gcc-toolset-11 bash [root@localhost ~]# gcc --version gcc (GCC) 11.2.1 20220127 (Red Hat 11.2.1-9.1.0.3) Copyright (C) 2021 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. [root@localhost ~]# exit exit [root@localhost ~]# gcc --version gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-18.0.2) Copyright (C) 2018 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. [root@localhost ~]#
Try to use enable instead of load .
See reference: CentOS Software Collections (SCL)
From this answer to "Install gcc 4.7 on CentOS [6.x]", the easiest way to get g++ 4.7, and the required tools and libraries, for CentOS 5.x is via the devtools package:
cd /etc/yum.repos.d
wget http://people.centos.org/tru/devtools/devtools.repo
yum --enablerepo=testing-devtools-6 install devtoolset-1.0
Since you're running g++ manually (as opposed to through make), you'll need to update your $PATH variable so your shell will use the new gcc, g++, etc. binaries:
export PATH=/opt/centos/devtoolset-1.0/root/usr/bin/:$PATH
At this point, your g++ should be version 4.7.0
$ g++ --version
g++ (GCC) 4.7.0 20120507 (Red Hat 4.7.0-5)
Copyright (C) 2012 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.
Be aware that installing gcc and associated tools and libraries in this manner relies on the repository maintainer to keep their packages up to date.
If you're interested in keeping your gcc up to date, you may want to consider building gcc from source.
Also, compiling programs with a different version of g++ and libstdc++ than are installed on your system can cause all kinds of Fun, coping with which is beyond the scope of this answer. It may be worth moving to a Linux distribution that has support for what you're doing.
Package managers are great, but (understandably) won't generally include the latest version of software. CentOS being a distribution particularly focusing on servers, favors stability (and extensive testing) over having the latest and shiniest. Here are your solutions:
Change distro
Other distributions like Arch Linux, Sabayon Linux or Debian "Sid" include newest versions a lot faster. You may consider changing distros.
Build it yourself
If you're stuck with CentOS, your best bet is to build gcc from source. It means getting the source code, making sure the dependencies are already present on your system (if not, I'm sure you can get them from the CentOS package manager), compiling the source into a binary executable, and installing the executable on your system.
It's not the easiest way to do it for sure, but you'll learn a lot about Unix in the process. Make sure to read the document a few times to make sure you get everything, and ask questions if something specific is unclear.
Be patient
At some point, this version (or newer) of gcc/g++ is going to enter the package manager. If you wait (up to several months, I'm not very familiar with the CentOS release cycle), you'll ultimately get what you want.
Use the new Amazon Linux 2 at https://aws.amazon.com/amazon-linux-2/ which has gcc v7.2.1 in it's default repo.
Currently it looks like that:
# yum list \*gcc10\*
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Installed Packages
gcc10.x86_64 10.3.1-1.amzn2.0.1 @amzn2-core
gcc10-binutils.x86_64 2.35-21.amzn2.0.1 @amzn2-core
gcc10-binutils-gold.x86_64 2.35-21.amzn2.0.1 @amzn2-core
Available Packages
gcc10-binutils.i686 2.35-21.amzn2.0.1 amzn2-core
gcc10-binutils-devel.x86_64 2.35-21.amzn2.0.1 amzn2-core
gcc10-c++.x86_64 10.3.1-1.amzn2.0.1 amzn2-core
gcc10-gdb-plugin.x86_64 10.3.1-1.amzn2.0.1 amzn2-core
gcc10-plugin-devel.x86_64 10.3.1-1.amzn2.0.1 amzn2-core
Therefore just execute:
yum install -y gcc10.x86_64 gcc10-c++.x86_64 and check it:
# gcc10-gcc --version
gcc10-gcc (GCC) 10.3.1 20210422 (Red Hat 10.3.1-1)
Copyright (C) 2020 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.```