centos7 服务器使用 nvm 安装高版本的 node 时,只要使用 npm 或者 node,均会出现以下问题

1
2
3
4
5
6
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)
原因

查看系统内安装的 glibc 版本
然后再根据分析可得知 新版的 node v18 开始 都需要 GLIBC_2.27 支持,可是目前系统内却没有那么高的版本

解决
1
2
3
4
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
升级 gcc 与 make
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 升级GCC(默认为4 升级为8)
yum install -y centos-release-scl
yum install -y devtoolset-8-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++

# 升级 make(默认为3 升级为4)
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

可能会出现这个错误

1
2
3
4
5
make[2]: *** [../o-iterator.mk:9: /root/glibc-2.25/build/time/strftime_l.o] Error 1
make[2]: Leaving directory '/root/glibc-2.25/time'
make[1]: *** [Makefile:215: time/subdir_lib] Error 2
make[1]: Leaving directory '/root/glibc-2.25'
make: *** [Makefile:9: all] Error 2
1
2
3
4
zzhao8053 16:11 (~) # 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)

解决:

Node nvm 安装

1
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

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

1
2
3
4
5
6
7
8
9
10
11
12
13
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 /usr/lib64/libstdc++.so.6 | grep GLIBCXX