Ubuntu安装Node.js

Ubuntu 18.04 安装 Node.js 18.x 后报错：

Copy
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

Copy
root@ubuntu:~# ldd --version
Copyright (C) 2018 Free Software Foundation, Inc.

这里卸载 Node.js 重新安装

Copy
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下所有node和node_modules文件

安装 node.js 16.x

Copy
root@ubuntu:~# curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
root@ubuntu:~# sudo apt-get install -y nodejs

验证

Copy
root@ubuntu:~# node -v
v16.18.1
root@ubuntu:~# npm -v
8.19.2
root@ubuntu:~# npx -v
8.19.2

安装 yarn

Copy
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

验证

Copy
root@ubuntu:~# yarn --version
1.22.19

查看当前 yarn 镜像源

Copy
root@ubuntu:~# yarn config get registry
https://registry.yarnpkg.com

切换淘宝镜像源

Copy
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.