When managing a CentOS server, compiling software from source code is a common task, and the g++ compiler is an essential tool. However, installation can be confusing because the package name on CentOS differs from the command name. This guide will show you two ways to install g++ correctly and efficiently.
1. 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.
2. Install the "Development Tools" Group (Method 1 - Recommended)
This is the best 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)
3. Install Only the gcc-c++ Package (Method 2)
If you only need the g++ compiler and prefer not to install other tools, you can install the package that contains it directly.
A Common Pitfall
New users often try the command yum install g++ and encounter a "No package available" error. The reason is that on Red Hat-based distributions (like CentOS, AlmaLinux), the package containing the g++ compiler is named gcc-c++.
Error: Nothing to do
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++
4. 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.
5. Conclusion
Installing g++ on CentOS is straightforward once you know the correct package name. Using the "Development Tools" group installation method is the most comprehensive and safest approach to ensure your server is well-equipped for future development and compilation tasks. Happy coding!
Managing and installing software on a server can sometimes be complex. If you need a professionally managed solution, consider our KVM VPS plans at VietHosting, where our team can assist with technical issues.