无法通过NPM安装brain.js?Ubuntu环境报错求助
Ubuntu 22.04安装brain.js时node-gyp报错排查
问题现象
全新安装Ubuntu 22.04后,执行npm install brain.js持续报错,核心错误为python: not found,完整报错日志如下:
npm ERR! code 1 npm ERR! path /var/www/ai/node_modules/gl npm ERR! command failed npm ERR! command sh -c prebuild-install || node-gyp rebuild npm ERR! gyp info it worked if it ends with ok npm ERR! gyp info using node-gyp@7.1.2 npm ERR! gyp info using node@12.22.9 | linux | x64 npm ERR! gyp info find Python using Python version 3.10.4 found at "/usr/bin/python3" npm ERR! gyp info spawn /usr/bin/python3 npm ERR! gyp info spawn args [ npm ERR! gyp info spawn args '/var/www/ai/node_modules/node-gyp/gyp/gyp_main.py', npm ERR! gyp info spawn args 'binding.gyp', npm ERR! gyp info spawn args '-f', npm ERR! gyp info spawn args 'make', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args '/var/www/ai/node_modules/gl/build/config.gypi', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args '/var/www/ai/node_modules/node-gyp/addon.gypi', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args '/home/user/.cache/node-gyp/12.22.9/include/node/common.gypi', npm ERR! gyp info spawn args '-Dlibrary=shared_library', npm ERR! gyp info spawn args '-Dvisibility=default', npm ERR! gyp info spawn args '-Dnode_root_dir=/home/user/.cache/node-gyp/12.22.9', npm ERR! gyp info spawn args '-Dnode_gyp_dir=/var/www/ai/node_modules/node-gyp', npm ERR! gyp info spawn args '-Dnode_lib_file=/home/user/.cache/node-gyp/12.22.9/<(target_arch)/node.lib', npm ERR! gyp info spawn args '-Dmodule_root_dir=/var/www/ai/node_modules/gl', npm ERR! gyp info spawn args '-Dnode_engine=v8', npm ERR! gyp info spawn args '--depth=.', npm ERR! gyp info spawn args '--no-parallel', npm ERR! gyp info spawn args '--generator-output', npm ERR! gyp info spawn args 'build', npm ERR! gyp info spawn args '-Goutput_dir=.' npm ERR! gyp info spawn args ] npm ERR! /bin/sh: 1: python: not found npm ERR! gyp: Call to 'python commit_id.py check ..' returned exit status 127 while in angle/src/angle.gyp. while loading dependencies of binding.gyp while trying to load binding.gyp npm ERR! gyp ERR! configure error npm ERR! gyp ERR! stack Error: `gyp` failed with exit code: 1 npm ERR! gyp ERR! stack at ChildProcess.onCpExit (/var/www/ai/node_modules/node-gyp/lib/configure.js:351:16) npm ERR! gyp ERR! stack at ChildProcess.emit (events.js:314:20) npm ERR! gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12) npm ERR! gyp ERR! System Linux 5.15.0-52-generic npm ERR! gyp ERR! command "/usr/bin/node" "/var/www/ai/node_modules/.bin/node-gyp" "rebuild" npm ERR! gyp ERR! cwd /var/www/ai/node_modules/gl npm ERR! gyp ERR! node -v v12.22.9 npm ERR! gyp ERR! node-gyp -v v7.1.2 npm ERR! gyp ERR! not ok
已尝试操作
- 安装Python 3和Python 2.7
- 将
python命令设为python2的shell别名
解决方法
1. 建立系统级Python软链接
shell别名仅在当前会话生效,node-gyp的子进程无法识别,需创建全局生效的软链接:
# 添加Python 2和Python 3到系统备选方案 sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2 # 选择默认Python版本(输入对应Python 2的编号) sudo update-alternatives --config python
2. 安装编译依赖工具
Ubuntu 22.04缺少node-gyp编译所需的基础工具,执行:
sudo apt-get install -y build-essential libssl-dev libffi-dev python2-dev
3. 直接指定node-gyp使用的Python版本
如果软链接仍不生效,强制让npm使用Python 2:
npm config set python /usr/bin/python2 # 重新安装brain.js npm install brain.js
或安装时临时指定:
npm install brain.js --python=/usr/bin/python2
4. 升级Node.js版本(推荐)
当前使用的Node.js 12.x版本较旧,与Ubuntu 22.04环境兼容性有限,建议升级到LTS版本:
# 以Node.js 18.x为例 curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - sudo apt-get install -y nodejs # 验证版本 node -v
内容的提问来源于stack exchange,提问作者Blue Herring
相关产品推荐
相关产品推荐

