Python

How Can I Fix a Different Version of Python During NPM Install?

How Can I Fix a Different Version of Python During NPM Install?

I recently ran into a classic compatibility headache on a legacy CentOS 5.9 VPS. The server shipped with Python 2.4.3 as the default, but I needed to install a Node.js package that required a newer Python (between 2.5 and 2.7). I had already compiled and installed Python 2.7.3 via make altinstall, so I thought I was set.

But when I ran npm install, I hit this error:

gyp ERR! configure error
gyp ERR! stack Error: Python executable "python" is v2.4.3, which is not supported by gyp.
gyp ERR! stack You can pass the --python switch to point to Python >= v2.5.0 & < 3.0.0.

That was frustrating, but after some digging, I learned why it happens and how to fix it properly.

What Happening

The culprit is node-gyp, a build tool that many npm packages rely on. It doesn’t support Python 2.4 at all—it requires 2.5, 2.6, or 2.7.

When you run npm install, node-gyp searches for python in your PATH. On CentOS 5.9, that’s /usr/bin/python, which points to version 2.4.3. Even though I had /usr/local/bin/python2.7 installed, node-gyp didn’t know about it.

That mismatch is why the error showed up.

Quick Fix

I tried a few approaches and found that any of these will solve the problem:

Tell npm to use Python 2.7

which python2.7   # usually /usr/local/bin/python2.7
npm config set python /usr/local/bin/python2.7
npm config get python
# /usr/local/bin/python2.7

From now on, npm install will always use Python 2.7.3.

Use a project local .npmrc

Inside your project directory:

echo "python=/usr/local/bin/python2.7" >> .npmrc

This keeps the setting project-specific, which is neat if you collaborate with others.

One off override

npm install --python=/usr/local/bin/python2.7

Great for testing or when you don’t want a global config change.

Use an environment variable

export PYTHON=/usr/local/bin/python2.7
npm install

If you add that export to ~/.bashrc, it’ll stick for future sessions.

Reproducing My Setup

To clarify, here’s how I installed Python 2.7.3:

wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
tar -xf Python-2.7.3.tgz
cd Python-2.7.3
./configure
make
sudo make altinstall

That installed /usr/local/bin/python2.7 without touching /usr/bin/python.

Then I built Node.js with:

python2.7 ./configure
make
sudo make install

That worked fine but npm install later defaulted to python (2.4.3), which is where the error came from.

Sanity Check & Troubleshooting

Here are the checks I used to verify everything:

/usr/bin/python -V         # 2.4.3 (system default)
/usr/local/bin/python2.7 -V  # 2.7.3 (custom install)

npm config get python
# Should show /usr/local/bin/python2.7

If problems persisted, I ran:

npm rebuild --python=/usr/local/bin/python2.7

Important: never replace /usr/bin/python on CentOS. System tools (like yum) depend on it. Breaking that can wreck the server.

Extra Practice Making it Foolproof

Since I didn’t want to repeat these steps every time, I added a few enhancements.

Script to auto-select the right Python

scripts/choose-python.sh:

#!/usr/bin/env bash
set -euo pipefail

CANDIDATES=(
  "/usr/local/bin/python2.7"
  "/usr/bin/python2.7"
  "/usr/bin/python2.6"
)

pick=""
for p in "${CANDIDATES[@]}"; do
  if [ -x "$p" ]; then
    ver=$("$p" -c 'import sys; print(".".join(map(str, sys.version_info[:3])))' 2>/dev/null || true)
    case "$ver" in
      2.[5-9].* ) pick="$p"; break;;
    esac
  fi
done

if [ -z "$pick" ]; then
  echo "No suitable Python (>=2.5,<3.0) found. Install python2.7 and retry." >&2
  exit 1
fi

echo "Using Python at: $pick"
npm config set python "$pick"

Make it executable:

chmod +x scripts/choose-python.sh

Run it once, and npm gets configured automatically.

Add a preinstall hook in package.json

{
  "scripts": {
    "preinstall": "bash ./scripts/choose-python.sh"
  }
}

This way, every npm install automatically picks the right Python.

Bash alias for convenience

echo 'alias npm27="PYTHON=/usr/local/bin/python2.7 npm"' >> ~/.bashrc
source ~/.bashrc

npm27 install

Per-user .npmrc

echo "python=/usr/local/bin/python2.7" >> ~/.npmrc

This keeps the configuration safe at the user level.

Common Pitfalls I Discovered

  • Don’t replace /usr/bin/python → it breaks CentOS tools.
  • Old SSL libraries → sometimes npm registry fetch fails on CentOS 5.x. Updating ca-certificates or running newer Node.js in a container is easier.
  • nvm is your friend → on legacy systems, using Node.js through nvm often saves a lot of pain.

Final Thought

The solution turned out to be much simpler than I first thought. All I needed to do was explicitly point npm (and node-gyp) to the Python 2.7 binary instead of relying on the system default. Running npm config set python /usr/local/bin/python2.7 immediately solved the issue no more complaints about Python 2.4.3, and my builds ran smoothly. If you’re working on an older CentOS or a similar setup, the safest approach is not to tamper with the system Python but to install the version you need separately and then make sure npm knows exactly where to find it.

author-avatar

About Daniyal Ahmed

Python Developer since 2020 | Release Manager for Python 3.8 | Creator of Black code formatter | Contributing to Python's growth and adoption.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments