如何在 ubuntu20.04 通过 apt 安装 nodejs16 ?

通过 ppa

curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -

How to install Node.js 16 on Ubuntu 20.04 LTS

─➤  cat sources.list.d/nodesource.list                                                                                    100 ↵
deb https://deb.nodesource.com/node_16.x focal main
deb-src https://deb.nodesource.com/node_16.x focal main

安装

好像北墙了?

─➤  sudo apt-get install -y nodejs                                                                                          100 ↵
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
下列【新】软件包将被安装:
  nodejs
升级了 0 个软件包,新安装了 1 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。
需要下载 26.5 MB 的归档。
解压缩后会消耗 124 MB 的额外空间。
错误:1 https://deb.nodesource.com/node_16.x focal/main amd64 nodejs amd64 16.16.0-deb-1nodesource1
  Could not wait for server fd - select (11: 资源暂时不可用) [IP: 23.44.51.9 443]
E: 无法下载 https://deb.nodesource.com/node_16.x/pool/main/n/nodejs/nodejs_16.16.0-deb-1nodesource1_amd64.deb  Could not wait for server fd - select (11: 资源暂时不可用) [IP: 23.44.51.9 443]
E: 有几个软件包无法下载,要不运行 apt-get update 或者加上 --fix-missing 的选项再试试?

找了一个教程:Nodesource 镜像使用帮助

换清华源

deb https://mirrors.tuna.tsinghua.edu.cn/nodesource/deb_16.x/ focal main
deb-src https://mirrors.tuna.tsinghua.edu.cn/nodesource/deb_16.x/ focal main

但是清华的镜像源好像已经过期了

╰─➤  sudo apt update                                             
命中:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal InRelease
命中:2 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates InRelease
命中:3 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-backports InRelease
命中:4 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-security InRelease
忽略:5 https://mirrors.tuna.tsinghua.edu.cn/nodesource/deb_16.x focal InRelease
错误:6 https://mirrors.tuna.tsinghua.edu.cn/nodesource/deb_16.x focal Release
  404  Not Found [IP: 101.6.15.130 443]
正在读取软件包列表... 完成
E: 仓库 “https://mirrors.tuna.tsinghua.edu.cn/nodesource/deb_16.x focal Release” 没有 Release 文件。
N: 无法安全地用该源进行更新,所以默认禁用该源。
N: 参见 apt-secure(8) 手册以了解仓库创建和用户配置方面的细节。

咋办?

阅读 4.5k
3 个回答

nodesource源更新了协议,似乎不允许镜像了,所以国内所有的镜像站基本都撤掉了nodesource镜像,你只能通过官方仓库的安装

nodesource的仓库没有被墙,在阿里云的服务器实测了下还有2MB/s的下载速度,你重试几次就行了

官网(https://nodejs.org/zh-cn/download/)提供了多种安装方式。

  1. 通过包管理器安装;
  2. 通过源码安装;
  3. 通过二进制文件安装。

对于 ubuntu20.04 最简单的一种应该是直接下载对应系统的二进制包。(我的是Ubuntu 22.04 LTS)

# 存放文件的目录
$ cd /path

$ wget https://nodejs.org/dist/v16.16.0/node-v16.16.0-linux-x64.tar.xz

$ tar xvJf node-v16.16.0-linux-x64.tar.xz

$ ls
node-v16.16.0-linux-x64  node-v16.16.0-linux-x64.tar.xz

$ cd node-v16.16.0-linux-x64/bin/

$ ls -ln
total 79280
lrwxrwxrwx 1 1000 1000       45 Jul  7 20:05 corepack -> ../lib/node_modules/corepack/dist/corepack.js
-rwxr-xr-x 1 1000 1000 81180048 Jul  7 20:05 node
lrwxrwxrwx 1 1000 1000       38 Jul  7 20:05 npm -> ../lib/node_modules/npm/bin/npm-cli.js
lrwxrwxrwx 1 1000 1000       38 Jul  7 20:05 npx -> ../lib/node_modules/npm/bin/npx-cli.js

$ ./node --version
v16.16.0

# 创建一个软链接
$ sudo ln -sf  /path/node-v16.16.0-linux-x64/bin/node /usr/local/bin/node

$ node --version
v16.16.0

可以使用 nvm 安装,github地址如下:
https://github.com/nvm-sh/nvm

如果安装脚本下载不下来的话,直接找到脚本页面源码,复制保存为一个本地文件,用 bash 执行下就可以安装了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题