How to Install g++ (GCC C++) on CentOS
When managing a CentOS server, you often need to compile software from its source code, and g++
is an essential tool for this task. However, installing it can be confusing if you don't know the correct package name. This guide will show you how to install g++
correctly.
What is g++?
g++ is the compiler for the C++ programming language, part of the GNU Compiler Collection (GCC). It is a fundamental tool required to build and install many applications in a Linux environment.
Method 1: Install the "Development Tools" Group (Recommended)
This is the best and most recommended method because it installs the entire necessary development environment at once, including gcc
, g++
, make
, git
, and many other utilities. This approach helps prevent missing library errors in the future.
For CentOS 7 and older (using yum)
For CentOS Stream 8/9 and newer distributions (using dnf)
Method 2: Install Only the gcc-c++ Package
If you only need the g++
compiler and prefer not to install other tools, you can install the gcc-c++
package directly.
A Common Pitfall
New users often try the command yum install g++
and encounter an error.
Error: Nothing to do
The reason is that on Red Hat-based distributions (like CentOS, AlmaLinux), the package containing the g++ compiler is named gcc-c++
.
The Correct Installation Command
- For CentOS 7 and older (using yum):
sudo yum install gcc-c++
- For CentOS Stream 8/9 and newer distributions (using dnf):
sudo dnf install gcc-c++
3. Verify the Installation
After completion, you can check the g++
version to confirm that the installation was successful.
A successful installation will return output similar to this:
Copyright (C) 2018 Free Software Foundation, Inc.
A Note on Compatibility Packages
Packages like compat-gcc-32
are typically only needed if you need to compile very old software that requires an older version of GCC. For most common use cases, you do not need to install them.