Ubuntu 安装 Node.js
2024-11-21 09:25:53 # Frontend # NodeJS

Operating System:Ubuntu 18.04.6 LTS

Kernel:Linux 4.15.0-169-generic

Architecture:x86-64

  • Ubuntu 18.04 安装Node.js 18.x 后报错:
1
2
root@ubuntu:~# node -v
node: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.28' not found (required by node)

因为 Ubuntu18.04 默认的 glibc 版本为 2.27

1
2
root@ubuntu:~# ldd --version
Copyright (C) 2018 Free Software Foundation, Inc.
  • 这里卸载 Node.js 重新安装
1
2
3
root@ubuntu:~# sudo apt remove nodejs
root@ubuntu:~# sudo apt remove npm
root@ubuntu:~# sudo apt autoremove

删除 /usr/local/lib/usr/local/include/usr/local/bin 下所有 nodenode_modules 文件

  • 安装 Node.js 16.x
1
2
root@ubuntu:~# curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
root@ubuntu:~# sudo apt-get install -y nodejs
  • 验证
1
2
3
4
5
6
root@ubuntu:~# node -v
v16.18.1
root@ubuntu:~# npm -v
8.19.2
root@ubuntu:~# npx -v
8.19.2
  • 安装 yarn
1
2
3
4
5
6
root@ubuntu:~# curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
ok
root@ubuntu:~# echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
deb https://dl.yarnpkg.com/debian/ stable main
root@ubuntu:~# apt update
root@ubuntu:~# apt install yarn
  • 验证
1
2
root@ubuntu:~# yarn --version
1.22.19
  • 查看当前 yarn 镜像源
1
2
root@ubuntu:~# yarn config get registry
https://registry.yarnpkg.com
  • 切换淘宝镜像源
1
2
3
4
root@ubuntu:~# yarn config set registry 'https://registry.npm.taobao.org'
yarn config v1.22.19
success Set "registry" to "https://registry.npm.taobao.org".
Done in 0.02s.