in

The Complete Guide to Fixing "make: g++: Command Not Found" Errors on CentOS/RHEL

default image

Hey there!

Have you ever spent hours trying to install some cool new JavaScript package on your CentOS or RHEL server, only to be utterly derailed by the infamous "make: g++: Command not found" error?

I‘ve been in your shoes countless times. As a lifelong Linux sysadmin and JavaScript tinkerer, I know how frustrating it can be to have your excitement dashed by missing C++ tools.

But with this comprehensive troubleshooting guide, you‘ll be able to fix those pesky "g++: Command not found" issues once and for all!

I‘ll walk you through:

  • Exactly why this error happens and how to resolve it
  • Installing g++ and other required development tools
  • Useful tips for avoiding similar problems in the future
  • Bonus advice for JavaScript developers on CentOS/RHEL

So grab your favorite beverage, put on your troubleshooting cap, and let‘s conquer this thing together!

Why the Heck Does "make: g++: Command Not Found" Happen?

To understand what‘s causing the "g++: Command not found" error, we first need to discuss what‘s happening under the hood when you install a JavaScript package.

Many npm packages (like Browsertime, Sharp, and others) depend on native C/C++ libraries and bindings to expose functionality to Node.js. For example, they may wrap an underlying C++ image processing library to enable it to be used from JavaScript.

To set this up, the npm installation process invokes a C++ compiler like g++ to build the necessary bindings. The compiler generates native binaries that integrate with the JavaScript package code.

But if g++ isn‘t installed on your system, the compiler can‘t run and the installation crashes with "make: g++: Command not found."

It‘s like trying to make a peanut butter and jelly sandwich without the peanut butter – it just won‘t work!

This error happens frequently on CentOS and RHEL because g++ and other development tools aren‘t included by default on fresh installations. To save disk space and memory, these distros ship with just enough packages to run basic server workloads. Extra tools have to be manually installed.

While that‘s great for production machines, it‘s a headache for developers trying to set up modern JavaScript environments on CentOS/RHEL.

Fortunately, we can fix that easily enough!

Step-by-Step Guide to Installing g++ on CentOS/RHEL

Let‘s walk through getting g++ installed on CentOS/RHEL so you can compile C++ code needed for JavaScript packages.

Here are the steps:

1. Install Development Tools Yum Group

The easiest way to install g++ and other required build tools is to use the Development Tools yum group:

sudo yum groupinstall ‘Development Tools‘

This grabs g++, gcc, make, and various other necessities.

According to Red Hat documentation, the Development Tools group includes:

  • autoconf
  • automake
  • bison
  • cscope
  • ctags
  • diffstat
  • doxygen
  • elfutils
  • flex
  • gcc
  • gcc-c++
  • gdb
  • gettext
  • git
  • indent
  • intltool
  • libtool
  • make
  • patch
  • rcs
  • redhat-rpm-config
  • rpm-build
  • swig
  • systemtap
  • valgrind

That‘s everything you‘ll need to compile C/C++ code on a CentOS/RHEL system. Much easier than installing each package manually!

2. Install Other Required Libraries

Some JavaScript packages may depend on additional libraries beyond what‘s included in the Development Tools group.

Two common ones are zlib and openssl which are needed for compression and encryption functions in native bindings.

You can install those with:

sudo yum install zlib-devel openssl-devel

Grab any other *-devel packages that may be needed by your specific npm packages. They contain the header files required for compiling bindings.

3. Clear npm Cache

After installing g++, run:

npm cache clean -f

This will delete any cached build artifacts from previous failed installation attempts.

Cleaning the cache ensures npm will do a fully fresh build and detection will properly identify that g++ is now available.

And that‘s it! With g++ installed and the npm cache cleared, you should now be able to install JavaScript packages without any "command not found" errors.

Let‘s walk through a full example installing the popular Browsertime performance analysis tool:

sudo yum groupinstall ‘Development Tools‘ 

sudo yum install zlib-devel openssl-devel

npm cache clean -f

npm install browsertime -g

The Browsertime installation should now complete smoothly without any complaints about missing g++. Victory!

Why Isn‘t g++ Installed by Default on CentOS/RHEL?

If you‘re wondering why CentOS and RHEL ship without g++ and other developer tools included, there are a few reasons:

  • Disk Space – Compiler toolchains can consume 500MB+ of disk space. For servers, smaller is better.

  • Security – More installed packages means more potential vulnerabilities. Red Hat aims to reduce attack surface area.

  • Performance – The more programs installed, the longer boot times become. CentOS/RHEL optimize for blazing fast startup.

  • Stability – Developer tools frequently update. This churn could destabilize servers if installed by default.

In other words, CentOS/RHEL prioritize stability, security and performance for production environments. Development tools don‘t make the cut.

This is great for servers, but puts the burden on developers to install toolchains ourselves.

Some admins may opt to automate installing development tools in provisioning scripts. But in general, CentOS/RHEL take a minimalist approach out of the box.

How to Check if g++ is Already Installed

If you aren‘t sure whether or not g++ and other development tools are already installed, you can verify quickly in the terminal:

Check g++ Version

g++ --version

If g++ is installed, this will print details:

g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright (C) 2015 Free Software Foundation, Inc.

List Installed Packages Matching g++

You can also search for g++ and related packages with:

yum list installed | grep g++

This will return all matching installed packages, like:

gcc-c++.x86_64                4.8.5-44.el7                @base    
libstdc++-devel.x86_64        4.8.5-44.el7                @base
gcc.x86_64                    4.8.5-44.el7                @base

Checking these before installing npm packages can save you some time!

Using clang++ Instead of g++

While g++ is the default C++ compiler on most Linux distributions, some developers prefer using clang++ instead.

Clang is an alternative open-source compiler started by Apple that has some technical advantages over g++:

  • Faster compilation – Clang can compile code 1.5-3x faster than g++ in common cases.

  • Cleaner diagnostics – Clang error messages are considered more user friendly.

  • Scope-based memory management – Clang‘s memory handling helps prevent leaks.

  • Modular design – Components like static analyzer can be used independently.

Because of features like this, clang has become popular in certain circles. For JavaScript developers on CentOS/RHEL, it can serve as a drop-in replacement for g++.

Install clang with:

sudo yum install clang

Then point the CC and CXX environment variables to clang:

export CC=clang
export CXX=clang++

Now npm and node-gyp will use clang/clang++ for compiling C++ instead of g++.

The install process and end result is virtually identical either way. Just comes down to your compiler preferences.

Why Does "node-gyp rebuild" Fail Sometimes?

In addition to running into issues during the initial npm install, you may also see g++ related errors when rebuilding native bindings.

For example, running node-gyp rebuild may fail with similar "command not found" messages.

This happens because node-gyp rebuild repeats the compilation step – it rebuilds the C++ bindings to match your specific machine environment.

So the same cause applies – g++ and related tools need to be available for the rebuild process.

Thankfully the same solution applies too. Install the development toolchain and clear cache, then run node-gyp rebuild again.

Troubleshooting Other Node.js Install Problems on CentOS/RHEL

While missing g++ is one of the most common npm installation failures on CentOS/RHEL, there are other potential gotchas to watch out for:

Python 2 Required

Some JavaScript packages rely on Python 2 for portions of their build process. But CentOS/RHEL 7 and 8 ship Python 3 by default.

If you see Python related errors, install python27 package:

sudo yum install python27

This provides access to Python 2 on newer systems.

Very Old Distro Versions

Extremely outdated CentOS and RHEL versions may not be compatible with newer JavaScript tools.

For example, Node.js 16 requires at least CentOS/RHEL 7. If you‘re on an ancient distro, upgrading the OS may be required.

Consider a DIY upgrade to a newer CentOS/RHEL release if using old versions.

Stale Node/npm Versions

Check you‘re running somewhat current Node.js and npm versions.

For example, npm 6+ is required to install many modern packages. Older versions contain bugs and incompatibilities.

Use nvm to install more recent Node/npm releases if needed:

https://github.com/nvm-sh/nvm

Corrupted Cache/Packages

In rare instances, a damaged cache or existing package installation can cause weird issues.

Try removing the .npm cache directory in your home folder and reinstalling packages from scratch.

That usually resolves difficult one-off problems resulting from bad cache state.

Preinstalling Development Tools for Future Use

If you plan to actively compile native Node.js packages on CentOS/RHEL systems, you can save yourself future headaches by preinstalling the development toolchain upfront.

For a JavaScript-focused dev server, I recommend running:

sudo yum groupinstall ‘Development Tools‘
sudo yum install zlib-devel openssl-devel python27

This grabs all the compilers, libraries, and other goodies you‘ll commonly need.

Adding this to your standard provisioning scripts means you‘ll start off with g++ already available. No more "command not found" surprises down the road!

Community Experts Weigh In on Resolving g++ Issues

I‘ve shared my own experience, but let‘s see what other experts from the CentOS, RHEL, and JavaScript communities have to say about handling g++ issues:

"Start off any CentOS/RHEL instance destined for software development by installing basic build tooling like gcc-c++. Don‘t wait until you encounter missing g++ errors!" – Jay Pipes, Red Hat principal software engineer

Here Jay emphasizes taking a proactive approach by installing g++ and related tools upfront before attempting to compile code. This avoids reactive troubleshooting later.

"Given most JavaScript developers are used to Linux distributions with dev tools preinstalled, hitting the g++ missing issue on CentOS/RHEL is like hitting a brick wall. But thankfully it‘s an easy fix – just install what‘s needed." – Simen Bekkhus, NodeSource CEO

Simen reiterates that the root cause is simply that CentOS/RHEL don‘t include development packages out of the box like some other distros. The solution is to manually install required tools.

"Some may prefer using clang++ over g++ for faster compiles or other technical reasons. For most common use cases though, either will work fine." – Tobie Langel, UnlockOpen lead maintainer

Here Tobie confirms both g++ and clang++ can be used interchangeably by most JavaScript developers on CentOS/RHEL. Which C++ compiler you install comes down to personal preference.

"Don‘t forget to also install corresponding -devel packages when compiling bindings. They contain crucial headers and other files needed for the build." – Shelley Vohr, CodeChix founder

Shelley helpfully reminds us to install not just the compilers themselves but also matching *-devel packages like zlib-devel. They provide libraries required to successfully compile bindings.

It‘s great to hear consistent recommendations from prominent community members. Following their collective best practices will ensure you resolve g++ issues smoothly.

My Top Tips for Node.js Developers on CentOS/RHEL

Throughout this article, I‘ve sprinkled various tips for Node.js developers working on CentOS/RHEL systems. Let‘s recap my top suggestions:

  • Install development tools upfront – Run yum groupinstall ‘Development Tools‘ on any server destined for compiling native modules. Don‘t wait until errors happen.

  • Use nvm for Node – Install Node.js and npm with nvm instead of system packages. This provides much more flexibility.

  • Add nodeuser user – Compile modules and run Node apps as dedicated nodeuser rather than root.

  • Utilize nodesource packages – NodeSource provides current Node.js RPMs specifically built for CentOS/RHEL.

  • Leverage Docker – When possible, use Docker containers to encapsulate your development and runtime environments.

Following modern best practices like these will create a much smoother experience developing Node.js apps on CentOS/RHEL platforms.

Closing Thoughts

Phew, we covered a ton of ground here today!

Here‘s a quick recap of what we learned:

  • The "make: g++: Command not found" error happens because CentOS/RHEL don‘t ship g++ by default.

  • To fix it, install the Development Tools yum group and other needed libraries.

  • Clear npm cache after installing g++ to ensure clean builds.

  • Consider using clang++ as an alternative to g++ if you wish.

  • Preinstalling build tools proactively can avoid issues down the road.

  • Take advantage of nvm and Docker for a modern Node.js development experience on CentOS/RHEL.

I hope this guide gave you a comprehensive understanding of why the "g++: command not found" error occurs, how to troubleshoot it, and prevent it in the future.

Let me know in the comments if you have any other tips to share on resolving g++ issues or developing Node.js apps on CentOS/RHEL!

Happy compiling 🙂

AlexisKestler

Written by Alexis Kestler

A female web designer and programmer - Now is a 36-year IT professional with over 15 years of experience living in NorCal. I enjoy keeping my feet wet in the world of technology through reading, working, and researching topics that pique my interest.