The error message clearly indicates two key issues:
1. The build-essential
package isn't available in the default yum repositories
2. The system lacks a C++ compiler (g++/c++)
On Amazon Linux (which is derived from RHEL/CentOS), the equivalent of Ubuntu's build-essential
is the Development Tools group. Here's how to install it:
sudo yum groupinstall "Development Tools"
For Node.js compilation, you'll need additional packages:
sudo yum install -y \
gcc-c++ \
make \
openssl-devel \
git \
python3
Instead of compiling from source, you might consider using NodeSource's pre-built packages:
curl -fsSL https://rpm.nodesource.com/setup_lts.x | sudo bash -
sudo yum install -y nodejs
If you must compile from source, here's the complete process:
# Install prerequisites (as root)
sudo yum groupinstall "Development Tools"
sudo yum install -y gcc-c++ make openssl-devel git python3
# As regular user
git clone https://github.com/nodejs/node.git
cd node
git checkout v18.16.0 # or your desired version
# Configure and build
./configure
make -j$(nproc)
sudo make install
If you encounter SSL errors during npm operations:
npm config set strict-ssl false
Or to use the system's certificate store:
sudo yum install -y ca-certificates
After successful installation, verify with:
node --version
npm --version
When working with Amazon EC2 instances, particularly those running Amazon Linux or RHEL-based distributions, you might encounter this frustrating error:
sudo yum install build-essential
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
(...)
No package build-essential available.
Error: Nothing to do
This happens because build-essential
is a Debian/Ubuntu package name. For RHEL-based systems like Amazon Linux, you need to install the equivalent development tools package group.
Here's the correct command to install all necessary compilation tools:
sudo yum groupinstall "Development Tools"
This single command installs:
- gcc (C compiler)
- g++ (C++ compiler)
- make
- automake
- other essential build tools
After installation, verify that g++ is available:
g++ --version
You should see output similar to:
g++ (GCC) 7.3.1 20180712 (Red Hat 7.3.1-6)
Copyright (C) 2017 Free Software Foundation, Inc.
For compiling Node.js specifically, you'll also need:
sudo yum install openssl-devel
sudo yum install python2
Note: Some EC2 instances might require you to enable the EPEL repository first:
sudo amazon-linux-extras install epel
sudo yum install epel-release
If you still encounter the "could not configure a cxx compiler!" error after installation, try these steps:
# Check if g++ is in your PATH
which g++
# If not found, locate the binary
sudo find / -name g++
# Add to PATH if needed
export PATH=$PATH:/path/to/g++
# Alternative: Create symlinks
sudo ln -s /usr/bin/g++ /usr/local/bin/g++
sudo ln -s /usr/bin/gcc /usr/local/bin/gcc
Here's a full example of compiling Node.js from source on EC2:
# Install dependencies
sudo yum groupinstall "Development Tools"
sudo yum install openssl-devel python2 git
# Download Node.js source
wget https://nodejs.org/dist/v14.17.0/node-v14.17.0.tar.gz
tar xvf node-v14.17.0.tar.gz
cd node-v14.17.0
# Configure and compile
./configure
make -j$(nproc)
sudo make install
# Verify installation
node --version
npm --version
Remember that compilation can take significant time on smaller EC2 instances. Consider using a more powerful instance type (like c5.large) if you're doing this frequently.
For most use cases, installing Node.js via NVM is simpler:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
source ~/.bashrc
nvm install node
This approach doesn't require compilation and handles all dependencies automatically.