Fixing ‘NPM node-gyp rebuild failed: Compiler, Dev Headers, Python Missing’ on Ubuntu 22.04 LTS
Resolve node-gyp build failures on Ubuntu 22.04 LTS by installing essential C++ compilers, Node.js headers, and Python dependencies.
Resolve node-gyp build failures on Ubuntu 22.04 LTS by installing essential C++ compilers, Node.js headers, and Python dependencies.
When deploying Node.js applications or installing certain npm packages, you might encounter an error indicating that node-gyp failed to rebuild. This usually occurs because the system lacks critical development tools necessary to compile native Node.js add-ons. The error message "compiler dev headers missing python" clearly points to the absence of C/C++ compilers, Node.js development headers, or a properly configured Python environment on your Ubuntu 22.04 LTS server. This guide will walk you through the precise steps to resolve this common npm compilation issue.
Symptom & Error Signature
Users typically encounter this error during an npm install or npm rebuild operation, leading to a long output block that ends with npm ERR!. The critical parts of the error message usually resemble the following:
npm ERR! gyp verb check python checking for Python executable "python3" in the PATH
npm ERR! gyp verb `which` failed Exception: No module named 'distutils.spawn'
npm ERR! gyp verb `which` failed Traceback (most recent call last):
npm ERR! gyp verb `which` failed File "/usr/lib/nodejs/node-gyp/gyp/gyp_main.py", line 13, in <module>
npm ERR! gyp verb `which` failed import distutils.spawn
npm ERR! gyp verb `which` failed ModuleNotFoundError: No module named 'distutils.spawn'
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: `python` not found. Make sure Python is installed and available in your PATH.
npm ERR! gyp ERR! stack at PythonFinder.failNoPython (/usr/lib/nodejs/node-gyp/lib/configure.js:484:19)
npm ERR! gyp ERR! stack at PythonFinder.<anonymous> (/usr/lib/nodejs/node-gyp/lib/configure.js:501:16)
npm ERR! gyp ERR! stack at F (/usr/lib/nodejs/node-gyp/node_modules/which/which.js:77:16)
npm ERR! gyp ERR! stack at /usr/lib/nodejs/node-gyp/node_modules/which/which.js:87:25
npm ERR! gyp ERR! stack at /usr/lib/nodejs/node-gyp/node_modules/isexe/index.js:42:5
npm ERR! gyp ERR! stack at /usr/lib/nodejs/node-gyp/node_modules/isexe/index.js:90:11
npm ERR! gyp ERR! stack at /usr/lib/nodejs/node-gyp/node_modules/graceful-fs/graceful-fs.js:123:16
npm ERR! gyp ERR! stack at FSReqCallback.oncomplete (node:fs:199:21)
npm ERR! gyp ERR! System Linux 5.15.0-XX-generic
npm ERR! gyp ERR! command "/usr/bin/node" "/usr/lib/nodejs/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_binary_site=..." "--node_sass_binary_site=..."
npm ERR! gyp ERR! cwd /var/www/my-app/node_modules/node-sass
npm ERR! gyp ERR! node -v v18.17.1
npm ERR! gyp ERR! node-gyp -v v9.3.1
npm ERR! gyp ERR! not ok
npm ERR! Build failed with error code: 1
npm ERR! A complete log of this run can be found in:
npm ERR! /home/ubuntu/.npm/_logs/2023-XX-XXTXX_XX_XX_XX-debug-0.log
Or a similar output indicating missing compilers like g++ or make:
npm ERR! gyp ERR! build error
npm ERR! gyp ERR! stack Error: `make` failed with exit code: 2
npm ERR! gyp ERR! stack at ChildProcess.onExit (/usr/lib/nodejs/node-gyp/lib/build.js:194:23)
npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:513:28)
npm ERR! gyp ERR! System Linux 5.15.0-XX-generic
npm ERR! gyp ERR! command "/usr/bin/node" "/usr/lib/nodejs/node-gyp/bin/node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd /var/www/my-app/node_modules/bcrypt
npm ERR! gyp ERR! node -v v18.17.1
npm ERR! gyp ERR! node-gyp -v v9.3.1
npm ERR! gyp ERR! not ok
npm ERR! Build failed with error code: 1
Root Cause Analysis
The node-gyp rebuild failed error typically stems from one or more of the following underlying issues:
- Missing Build Essentials (C/C++ Compiler & Make):
node-gypis a cross-platform command-line tool written in Node.js for compiling native add-on modules for Node.js. It leverages tools likemakeand a C/C++ compiler (e.g.,gcc,g++) to build these modules from source code. If thebuild-essentialpackage, which provides these critical tools, is not installed,node-gypcannot perform its function. - Missing Node.js Development Headers: To compile native modules against a specific Node.js version,
node-gypneeds access to the Node.js header files. These headers define the Node.js API that native modules interact with. If Node.js was installed in a way that didn't include these development files (e.g., manually, or if thenodejs-devpackage is missing), compilation will fail. - Missing or Incorrect Python Installation:
node-gypitself is written in Python (specifically, it usesgyp, a meta-build system). It requires a Python 3.x interpreter to be installed and accessible in the system'sPATH. If Python is not installed, or ifnode-gypis attempting to use an incompatible Python 2.x version or a broken Python environment, the build process will fail. TheModuleNotFoundError: No module named 'distutils.spawn'often points to Python environment issues, especially with modern Python 3 versions wheredistutilsmight be deprecated or missing in minimal installations. - Corrupted
npmCache ornode_modules: Less common but possible, a corruptednpmcache or inconsistentnode_modulesdirectory can lead to issues, especially if previous partial builds failed.
Step-by-Step Resolution
Follow these steps sequentially to diagnose and resolve the node-gyp rebuild failed error on your Ubuntu 22.04 LTS system.
1. Update Your System Package List
Always start by ensuring your system's package list is up-to-date. This helps in pulling the latest versions of dependencies and avoiding potential conflicts.
sudo apt update && sudo apt upgrade -y
2. Install Essential Build Tools (GCC, G++, Make)
This is typically the most critical step as it provides the fundamental tools needed for compilation.
sudo apt install build-essential -y
The
build-essentialpackage installsgcc,g++,make,dpkg-dev, and other crucial development libraries. Without these,node-gypcannot compile native C/C++ code.
3. Ensure Python 3 is Installed and Configured
node-gyp requires a Python 3 interpreter. Verify its presence and configure npm to use it if necessary.
First, check if Python 3 is installed:
python3 --version
If Python 3 is not found or is an older version, install it:
sudo apt install python3 -y
It's also good practice to have python3-pip for managing Python packages, though node-gyp generally uses the system Python executable directly.
sudo apt install python3-pip -y
If you have multiple Python versions or node-gyp struggles to find python3, explicitly tell npm which Python executable to use:
npm config set python python3
If you encounter
ModuleNotFoundError: No module named 'distutils.spawn', it sometimes indicates an issue with howdistutils(a part of Python's standard library) is linked or installed. Ensuringpython3is correctly installed viaaptusually resolves this on Ubuntu.
4. Install Node.js Development Headers
If Node.js was installed using apt (e.g., via sudo apt install nodejs), you'll need its development headers to compile against that specific Node.js version.
sudo apt install nodejs-dev -y
If you installed Node.js using a Node Version Manager (like
nvmorfnm), the development headers are typically handled automatically by the version manager when you install a Node.js version. In such cases,sudo apt install nodejs-devmight not be strictly necessary for modules compiled within annvmcontext, but ensuring yournvmenvironment is healthy (e.g.,nvm install --reinstall-packages-from=node) is key. For system-wide Node.js (apt-managed),nodejs-devis crucial.
5. Clean NPM Cache and Rebuild
Sometimes, a corrupted npm cache or residual node_modules can prevent a clean build. It's often beneficial to perform a fresh start.
npm cache clean --force
rm -rf node_modules
# If you use package-lock.json or yarn.lock, remove them to force a fresh dependency resolution
rm package-lock.json
# rm yarn.lock # Uncomment if using Yarn
npm install
Running
rm -rf node_modulesandrm package-lock.jsonwill delete your current project dependencies and forcenpm installto download and rebuild everything from scratch. This can take some time depending on your project size and internet connection. Ensure you're in the correct project directory before executing these commands.
6. Verify Node.js and NPM Setup
Confirm that your Node.js and npm versions are as expected after the previous steps.
node -v
npm -v
If you're using nvm, ensure the correct Node.js version is active:
nvm current
7. Test the Fix
Navigate back to your Node.js project directory and attempt to install or rebuild your dependencies again:
cd /path/to/your/nodejs/project
npm install
If the issue was with a specific native module, npm install should now successfully compile and install it. If you were trying to install a global package that uses native modules, try that npm install -g <package-name> command again.
By systematically addressing the missing build tools, Python environment, and Node.js headers, you should successfully resolve the NPM node-gyp rebuild failed error on Ubuntu 22.04 LTS.
Our Production Verification Guarantee
Encountering a bug not covered here or running a non-standard kernel configuration? Our solutions are continually refined against real production incidents. Submit an environment trace for our editorial team to replicate.