I’m working on a CentOS 5.9 server that comes with Python 2.4.3 by default. Since I needed a newer version, I compiled and installed Python 2.7.3 using the altinstall method to avoid replacing the system Python.
curl -O https://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
tar -zxf Python-2.7.3.tgz
cd Python-2.7.3
./configure --prefix=/usr/local
make && make altinstall
After that, I built Node.js from source using the newer Python version:
python2.7 configure
make && make install
Everything works fine until I try to install Node packages that need native compilation. The npm installer fails because it’s still trying to use the old Python 2.4.3 instead of my newer installation:
node-gyp ERR! build error
node-gyp ERR! stack Error: Python executable "python" is v2.4.3, which is not supported.
node-gyp ERR! stack You can pass the --python switch to point to Python >= v2.5.0 & < 3.0.0.
How can I configure npm to use my Python 2.7.3 installation instead of the system default when building native modules?
try this: npm config set python /usr/local/bin/python2.7. that should force npm to use ur 2.7 version for all builds, saving u from passin --python each time.
You can also create a symlink to temporarily override the default python path. I hit this same issue on an older RHEL system. What worked was making a local bin directory in my project folder, then symlinking python2.7 to just ‘python’ there. Run mkdir ~/myproject/bin && ln -s /usr/local/bin/python2.7 ~/myproject/bin/python then add that bin directory to the front of your PATH before running npm. Node-gyp will find your newer python first without messing with system-wide configs. Just update your PATH in the terminal session where you’re doing npm installs. Works great when you need different python versions for different projects.
You can also set the PYTHON environment variable before running npm commands. Just export PYTHON=/usr/local/bin/python2.7 in your shell profile or run it inline like PYTHON=/usr/local/bin/python2.7 npm install package-name. This worked great for me on CentOS with multiple Python versions - the environment variable overrides whatever python executable node-gyp would normally find. Or create a .npmrc file in your project directory with python=/usr/local/bin/python2.7 to keep the config local instead of global.