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 installs g++ alongside the entire necessary development environment.
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
On Red Hat-based operating systems, the package containing the g++ command is explicitly named gcc-c++.
Install on CentOS Stream 8/9, AlmaLinux, Rocky Linux:
sudo dnf install gcc-c++ -y
Install on CentOS 7:
sudo yum install gcc-c++ -y
4. Verify Installation and Test Compile
Step 1: Check the version
g++ --version
Step 2: Create source file
echo '#include
int main() {
std::cout << "G++ is working perfectly on Linux!" << std::endl;
return 0;
}' > test.cpp
Step 3: Compile
g++ test.cpp -o test_app
Step 4: Run application
./test_app
5. Common Installation Errors
- "Could not retrieve mirrorlist / HTTP Error 404" Error: If you are using CentOS 7, this occurs because the OS has reached End-of-Life.
👉 Solution: See our guide on How to Fix YUM Errors on CentOS 7 After EOL. - "No package gcc-c++ available" Error: This happens on Minimal Installs without EPEL enabled. Run
sudo dnf install epel-release -yfirst.
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.