Centos 7.6 安装 Node.js 20 的环境配置记录

Centos 7.6 安装 Node.js 20 的环境配置记录

Centos 7在 2024 年的 6 月 30 号已经停止维护了,但是由于时代原因,很多服务还是跑在这个系统上。本篇博文记录如何在 Centos 7.6 上安装 Node20。

初步安装 node

  1. 下载 node.js 的 Linux 版本

    cd ~
    curl -O https://nodejs.org/dist/v20.11.1/node-v20.11.1-linux-x64.tar.xz
  2. 解压缩

    tar -xf node-v20.11.1-linux-x64.tar.xz
    mv node-v20.11.1-linux-x64 ~/nodejs
  3. 在命令行中生效

    只在当前终端生效

    export PATH=~/nodejs/bin:$PATH

    永久生效

    echo 'export PATH=~/nodejs/bin:$PATH' >> ~/.bashrc
    source ~/.bashrc
  4. 检查是否生效

    node -v
    npm -v

    不出意外的话,这一步会报一个类似的错:

image-20250505194845875

​ 原因是因为 node18 开始,都需要 2.27以上的版本支持,但是 centos7.6 老系统默认没有那么高的版本,因此接下来我们去安装

glibc_2.28

安装 glib.c 2.28

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

这一步可能会报一个错:

configure: error: 
*** These critical programs are missing or too old: make bison compiler
*** Check the INSTALL file for required versions.

就是提示我们的工具集太老了,经过测试,我们需要把服务器上的 gcc 升级到 8,然后 make 要升级到 4,具体安装方法在本文后面的引用里。

之后就可以运行下面的命令:

make
sudo make install

我在 make install 这里遇到了一个这样的错误:

msgfmt -o /home/curiosity/download/glibc-2.28/build/po/da.mo da.po
make[2]: *** [Makefile:87: /home/curiosity/download/glibc-2.28/build/po/da.mo] Segmentation fault
rm /home/curiosity/download/glibc-2.28/build/po/fr.mo /home/curiosity/download/glibc-2.28/build/po/vi.mo /home/curiosity/download/glibc-2.28/build/po/ja.mo /home/curiosity/download/glibc-2.28/build/po/tr.mo /home/curiosity/download/glibc-2.28/build/po/pl.mo /home/curiosity/download/glibc-2.28/build/po/be.mo /home/curiosity/download/glibc-2.28/build/po/rw.mo /home/curiosity/download/glibc-2.28/build/po/ia.mo
make[2]: Leaving directory '/home/curiosity/download/glibc-2.28/po'
make[1]: *** [Makefile:215: po/subdir_install] Error 2
make[1]: Leaving directory '/home/curiosity/download/glibc-2.28'
make: *** [Makefile:12: install] Error 2

根据第一行,看起来是需要用到一个 da.po 文件,我看了一下目录build/po/里,并没有这个文件,问了一下 ChatGPT,答案是可以使用make install -k忽略这个文件,因为这个文件是翻译相关的,不影响核心功能。

之后继续安装最后的提示是这样:

The script has found some problems with your installation!
Please read the FAQ and the README file and check the following:
- Did you change the gcc specs file (necessary after upgrading from
  Linux libc5)?
- Are there any symbolic links of the form libXXX.so to old libraries?
  Links like libm.so -> libm.so.5 (where libm.so.5 is an old library) are wrong,
  libm.so should point to the newly installed glibc file - and there should be
  only one such link (check e.g. /lib and /usr/lib)
You should restart this script from your build directory after you've
fixed all problems!
Btw. the script doesn't work if you're installing GNU libc not as your
primary library!
make[1]: *** [Makefile:111: install] Error 1
make[1]: Leaving directory '/home/curiosity/download/glibc-2.28'
make: *** [Makefile:12: install] Error 2

这里结合这篇博客的内容,其实就是已经安装成功了,可以忽略这个错误。使用 ldd --vwesion查看版本已经是 2.28

image-20250505194154393

这个时候在运行 node -v,就会发现 glibc 的依赖问题已经解决了。之后我们需要解决剩下的依赖问题:

image-20250505195623343

具体内容在这篇博客:https://www.cnblogs.com/yuwen01/p/18067005

服务器编译Node 项目

image-20250505195715779

这个时候就可以愉快地在服务器上编译我们的 node 项目,不过可能会遇到内存限制的问题,一般要大于 4g 比较好,如果是轻量云服务器,一般内存比较小,可以通过这段命令扩展一下交换内存:

# 创建 2G 的 swap 文件
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

# 验证
swapon --show
free -h

Refer

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王行知

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值