Skip to content

linux 安装 node 错误

error
node: /lib64/libm.so.6: version `GLIBC_2.27' not found (required by node)
node: /lib64/libc.so.6: version `GLIBC_2.25' not found (required by node)
node: /lib64/libc.so.6: version `GLIBC_2.28' not found (required by node)
node: /lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by node)
node: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by node)
node: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by node)

原因:新版的 node v18 开始,都需要 GLIBC_2.27 支持

查看系统中 glibc:strings /lib64/libc.so.6 |grep GLIBC\_

解决办法,更新 glibc

sh
wget http://ftp.gnu.org/gnu/glibc/glibc-2.28.tar.gz
tar xf glibc-2.28.tar.gz
cd glibc-2.28/ && mkdir build && cd build
../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin

可能出现的错误——最后几行,make 问题

make 版本低错误:升级 make(默认为 3 升级为 4)

sh
wget http://ftp.gnu.org/gnu/make/make-4.3.tar.gz
tar -xzvf make-4.3.tar.gz && cd make-4.3/
./configure --prefix=/usr/local/make
make && make install
cd /usr/bin/ && mv make make.bak
ln -sv /usr/local/make/bin/make /usr/bin/make

gcc 版本低错误:升级 GCC(默认为 4 升级为 8)

sh
yum install -y centos-release-scl
yum install -y devtoolset-8-gcc\* # 这里可能找不到包,需要手动升级 gcc
mv /usr/bin/gcc /usr/bin/gcc-4.8.5
ln -s /opt/rh/devtoolset-8/root/bin/gcc /usr/bin/gcc
mv /usr/bin/g++ /usr/bin/g++-4.8.5
ln -s /opt/rh/devtoolset-8/root/bin/g++ /usr/bin/g++

手动升级 gcc

  1. 下载
  1. 解压 tar -zxvf gcc-8.5.0.tar.gz

  2. 运行下载脚本

sh
cd gcc-8.5.0
./contrib/download_prerequisites
  1. 生成 Makefile 文件 创建 gcc-build-8.5.0 存放编译文件

mkdir gcc-build-8.5.0

进入 gcc-build-8.5.0,执行 configure 脚本,生成 Makefile 文件

sh
cd gcc-build-8.5.0
../configure --enable-checking=release --enable-languages=c,c++ --disable-multilib

../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
  1. 执行编译 make -j 2

  2. 执行安装 make install

  3. 查看版本 gcc -v

错误:在 make 编译时,出现 C++: fatal error:已杀死 signal terminated program cc1plus

可以考虑是虚拟机内存不足

  • 可以采用 swap 分区的方式解决
  • make时降低任务数量make -j2

这时所有的问题都已经解决完毕,再重新执行上一步,更新 glibc 即可。

sh
cd /root/glibc-2.28/build
../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin

# 需 make && make install

make
make install

上述步骤完成后,执行 node -v 时还有报错

sh
node -v
node: /lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by node)
node: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by node)
node: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by node)

通过 strings 命令查看判断是缺少 GLIBCXX_3.4.20,解决方法是升级 libstdc++

sh
strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX
strings /lib64/libstdc++.so.6 | grep GLIBCXX

make all
yum whatprovides libstdc++.so.6
yum update -y libstdc++.x86_64

# 这个版本的libstdc++.so.6实际上版本比较旧,并不能解决问题。
# 下载稍微新一点的版本
sudo wget http://www.vuln.cn/wp-content/uploads/2019/08/libstdc.so_.6.0.26.zip
unzip libstdc.so_.6.0.26.zip
cp libstdc++.so.6.0.26 /lib64/
cd /lib64

# 把原来的命令做备份
cp libstdc++.so.6 libstdc++.so.6.bak
rm -f libstdc++.so.6

# 重新链接
ln -s libstdc++.so.6.0.26 libstdc++.so.6

此时再次通过 strings 查看有没有 GLIBCXX_3.4.20

strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX

至此 node -v 输出 v20.17.0

参考资料

Released under the MIT License.