How to Install g++ (GCC C++) on CentOS via Command Line
How to Install g++ (GCC C++) on CentOS / AlmaLinux / Rocky Linux
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. Or install just the C++ compiler: sudo dnf install gcc-c++
Once installed, verify the version by running: g++ --version
When administering a Linux server, compiling software or extensions from source code is a mandatory task, and g++ (part of the GNU Compiler Collection) is an essential tool. This tutorial provides the fastest installation commands, shows how to run a test compilation, and helps you troubleshoot common errors.
1. Quick Install Commands (Cheat Sheet)
If you are a sysadmin who needs to act quickly, use this summary table based on your OS version:
| Objective | Command on CentOS 7 (yum) | Command on CentOS 8/9, AlmaLinux (dnf) |
|---|---|---|
| Install full build tools | yum groupinstall "Development Tools" |
dnf group install "Development Tools" |
| Install g++ only | yum install gcc-c++ |
dnf install gcc-c++ |
| Check version | g++ --version |
g++ --version |
2. Method 1: Install "Development Tools" (Recommended)
This is the standard and safest method. This command not only installs g++ but also pulls in the entire necessary development environment, including gcc, make, and git. This prevents missing dependency errors when building source code later.
# For CentOS Stream 8/9, AlmaLinux, Rocky Linux
sudo dnf group install "Development Tools" -y
# For CentOS 7
sudo yum groupinstall "Development Tools" -y
3. Method 2: Install the gcc-c++ Package Only
If your system is storage-constrained and you are certain you only need the C++ compiler, you can install it individually.
If you type yum install g++, the system will return a "No package g++ available" error. On Red Hat-based operating systems, the package containing the g++ command is explicitly named gcc-c++.
You can verify the existence of this package by running a search:
# CentOS 8/9, AlmaLinux, Rocky Linux
dnf search gcc-c++
# CentOS 7
yum search gcc-c++
The official installation command is:
# CentOS 8/9, AlmaLinux, Rocky Linux
sudo dnf install gcc-c++ -y
# CentOS 7
sudo yum install gcc-c++ -y
4. Verify Installation and Test Compile
To ensure the compiler is working perfectly, we will check its version and test compile an actual C++ file.
Step 1: Check the version
g++ --version
Step 2: Create and compile a Hello World app
# Create test.cpp file
echo '#include
int main() {
std::cout << "G++ is working perfectly on Linux!" << std::endl;
return 0;
}' > test.cpp
# Compile the file
g++ test.cpp -o test_app
# Run the newly built application
./test_app
5. Common Installation Errors
During the installation process, you might encounter the following hurdles:
- "Could not retrieve mirrorlist / HTTP Error 404" Error: If you are using CentOS 7, this occurs because the OS has reached End-of-Life, and its default repositories are disabled.
👉 Solution: See our detailed guide on How to Fix YUM Errors on CentOS 7 After EOL. - "No package gcc-c++ available" Error: This happens on certain Minimal Installs where supplementary repositories are not enabled. Run
sudo dnf install epel-release -ybefore attempting to install g++.
6. Conclusion
Installing g++ on CentOS, AlmaLinux, or Rocky Linux is straightforward once you know the exact package identifier is gcc-c++. Utilizing the "Development Tools" group installation is the industry standard approach to prevent 99% of missing dependency errors during future server operations.
Compiling large projects can easily consume most of your CPU and RAM. Using a high-vCPU Large VPS or Dedicated Server at VietHosting can significantly reduce build times and keep your system responsive.
More Technical Guides
Explore additional technical guides and practical tutorials to optimize performance and manage your server infrastructure efficiently.