1. Tutorial Background
The default pre-installed version of GCC in CentOS 7 is 4.8.5 (released in 2015). In modern development scenarios, many software packages (such as higher versions of Python, TensorFlow, C++ 11+ projects, etc.) have higher requirements for the GCC version (needs 5.4+ and above). Therefore, it is necessary to upgrade GCC, but directly overwriting the system’s default GCC may lead to dependency conflicts (for example, the <span>yum</span> tool depends on the old version).
This tutorial uses the SCL (Software Collections) repository method to upgrade, allowing the installation and use of a higher version of GCC (using GCC 9 as an example, which is stable and highly compatible) without overwriting the system’s default GCC, ensuring safety and flexibility.
2. Preparation Before Upgrade
Ensure that the system is connected to the internet and has <span>root</span> privileges (or use <span>sudo</span> privileges to execute commands).
3. Detailed Upgrade Steps
1. Backup Existing YUM Sources (Critical: Prevent Configuration Errors)
After CentOS 7 stops official maintenance, the default YUM source may become invalid, and new repository configurations will need to be added later. First, back up the existing source files to avoid irreversible mistakes:
# Create backup directory
sudo mkdir -p /etc/yum.repos.d/backup
# Move all .repo source files to the backup directory
sudo mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/
2. Add SCL Repository Configuration File (Using Aliyun Source)
The SCL repository is the core for upgrading GCC. Here, we choose the Aliyun SCL Source (fast access speed in China, high stability) and manually create the repository configuration file:
# Create and edit centos-sclo-rh.repo file using vi editor
sudo vi /etc/yum.repos.d/centos-sclo-rh.repo
After opening the editor, press <span>i</span> to enter edit mode, completely copy the following content (do not omit or modify the format):
[centos-sclo-rh]
name=CentOS-7-SCLo
rhbaseurl=https://mirrors.aliyun.com/centos/7/sclo/x86_64/rh/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-SIG-SCLo
enabled=1
After copying, press <span>Esc</span> to exit edit mode, type <span>:wq</span> and hit enter (to save and exit the vi editor).
3. Clean YUM Cache and Generate New Cache
After adding the new repository, it is necessary to clean the old cache and load the package list from the new repository to ensure that the correct software packages can be found for subsequent installations:
# Clean all YUM cache
sudo yum clean all &&# Generate new cache (download package list from Aliyun source)
sudo yum makecache
Wait patiently after executing (the time depends on network speed). If there are no errors, the cache has been generated successfully.
Please open in the WeChat client
4. (Optional) Install SCL Repository Base Package
In most cases, the configuration file added in step 2 is sufficient, but if prompted that the “repository does not exist” when installing GCC later, you can execute this step to install the SCL repository base package:
sudo yum install -y centos-release-scl
5. Install Higher Version of GCC (GCC 9)
Install <span>devtoolset-9</span> (corresponding to GCC 9 version) via YUM, and add the <span>--nogpgcheck</span> parameter (to skip GPG key verification, as the official key source for CentOS 7 has become invalid, which does not affect package security):
sudo yum install -y devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils --nogpgcheck
•<span>devtoolset-9-gcc</span>: Core package for GCC 9•<span>devtoolset-9-gcc-c++</span>: C++ compilation component (must be installed if compiling C++ projects)•<span>devtoolset-9-binutils</span>: Binary toolset (assists in compilation)
Wait for the installation to complete (approximately 50MB download), and if there are no errors, the installation is successful.
6. Enable Higher Version of GCC
The GCC installed via SCL will not take effect automatically; it needs to be enabled manually, supporting both temporary enabling and permanent enabling, depending on your needs:
Method 1: Temporary Enable (Effective Only for Current Terminal)
This is suitable for scenarios where you need to “temporarily use a higher version of GCC”; the default GCC 4.8.5 will be restored after closing the terminal:
scl enable devtoolset-9 bash
Method 2: Permanent Enable (Recommended)
If you need to automatically use GCC 9 every time you log into the terminal, you can write the enable command into the environment variable file, with two scenarios: “current user” and “global for all users”:
Scenario A: Effective Only for Current User (Recommended for Regular Users)
# Write the enable command into the current user's .bashrc file
echo "source /opt/rh/devtoolset-9/enable" >> ~/.bashrc
# Take effect immediately (no need to restart the terminal)
source ~/.bashrc
Scenario B: Effective for All Users Globally (Requires Root Privileges)
This is suitable for multi-user servers, where all users will use GCC 9 after logging in:
# Switch to root user (if not currently root)
su root
# Write the enable command into the global environment variable file /etc/profile
echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile
# Take effect immediately
source /etc/profile
7. Verify GCC Upgrade Results
Execute the following command to check the current GCC version and confirm whether the upgrade was successful:
gcc -v
If the output is similar to the following (version number is 9.3.1 and above), it indicates that the upgrade was successful:
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-9/root/usr/libexec/gcc/x86_64-redhat-linux/9/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-9/root/usr --mandir=/opt/rh/devtoolset-9/root/usr/share/man --infodir=/opt/rh/devtoolset-9/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-9.3.1-20200408/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 9.3.120200408 (RedHat 9.3.1-2.2.el7) (GCC)
4. Common Issues and Precautions
1.If you need to install other versions of GCC: This tutorial uses GCC 9 as an example. If you need to install GCC 10, simply replace <span>devtoolset-9</span> in the commands with <span>devtoolset-10</span> (e.g., <span>devtoolset-10-gcc</span>), and the other steps remain the same.2.The system’s default GCC is not overwritten: The GCC installed via SCL is located at <span>/opt/rh/devtoolset-9/root/usr/bin/</span>, while the system’s default GCC (4.8.5) is still at <span>/usr/bin/gcc</span>. If you need to temporarily use the old version, you can directly execute <span>/usr/bin/gcc -v</span>.3.If prompted “no available packages” during installation: Re-execute step 3 (clean and generate cache), or check if the repository configuration file in step 2 is correct (ensure that <span>baseurl</span> is not misspelled).
5. Summary
Through this tutorial, you can safely upgrade GCC to version 9 (or higher) on CentOS 7, meeting modern software development needs without affecting the original system dependencies. If you need to compile software later (such as Python or C++ projects), you can directly use the <span>gcc</span> or <span>g++</span> commands to invoke the higher version tools.