无法通过 nodesource/distributions 在 ubuntu22 上安装 nodejs16/18/20?

参考:https://github.com/nodesource/distributions

docker pull ubuntu:22.04       
docker run --rm -it ubuntu:22.04 bash                      
apt-get update
apt-get install -y ca-certificates curl gnupg
mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
NODE_MAJOR=16; echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
apt-get update
apt-get install nodejs npm -y      

报错

137 added, 0 removed; done.
Setting up gpg-wks-server (2.2.27-3ubuntu2.1) ...
Setting up libcurl4:amd64 (7.81.0-1ubuntu1.13) ...
Setting up curl (7.81.0-1ubuntu1.13) ...
Setting up gpg-wks-client (2.2.27-3ubuntu2.1) ...
Setting up gnupg (2.2.27-3ubuntu2.1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...
Processing triggers for ca-certificates (20230311ubuntu0.22.04.1) ...
Updating certificates in /etc/ssl/certs...
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_16.x nodistro main
Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease                                          
Hit:2 http://archive.ubuntu.com/ubuntu jammy-updates InRelease                                  
Get:3 https://deb.nodesource.com/node_16.x nodistro InRelease [12.1 kB]
Hit:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease               
Get:5 https://deb.nodesource.com/node_16.x nodistro/main amd64 Packages [7253 B]
Hit:6 http://security.ubuntu.com/ubuntu jammy-security InRelease
Fetched 19.4 kB in 6s (3210 B/s)                                                                                                                                                                                                                                     
Reading package lists... Done
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 nodejs : Conflicts: npm
 npm : Depends: node-cacache but it is not going to be installed
       Depends: node-gyp but it is not going to be installed
       Depends: node-mkdirp but it is not going to be installed
       Depends: node-nopt but it is not going to be installed
       Depends: node-tar but it is not going to be installed
       Depends: node-which but it is not going to be installed
       Depends: nodejs:any (>= 10)
       Recommends: git but it is not going to be installed
       Recommends: node-tap but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
阅读 1.3k
avatarAI BotBETA

这个问题是关于在 Ubuntu 22.04 上安装 Node.js 的,但在尝试安装时遇到了依赖问题。从错误信息来看,npm 有一些未满足的依赖项,同时 nodejs 和 npm 之间也存在冲突。

以下是我建议的解决方案:

  1. 首先,我建议你尝试使用 apt 的在线搜索和安装功能来找到并安装这些缺失的依赖。例如,你可以运行以下命令来查找 node-gyp
apt-cache search node-gyp

然后,你可以根据返回的结果手动安装这些包。例如,如果你发现 node-gypbuild-essential 包中,你可以运行以下命令来安装它:

apt-get install -y build-essential

重复这个过程,直到你安装了所有 npm 的依赖项。

  1. 另一种可能的解决方案是尝试使用 nvm(Node Version Manager)来安装和管理 Node.js。nvm 可以帮助你在同一系统上安装和管理多个版本的 Node.js,这可能有助于解决你的问题。以下是安装和使用 nvm 的基本步骤:
bash < <(curl -sL install.nvm.sh)
nvm install node

这将安装 nvm 并使用它来安装 Node.js。

  1. 如果上述方法都不能解决你的问题,你可能需要考虑使用不同的 Linux 发行版,如 Ubuntu 20.04 或 21.04,这些版本可能更适合安装 Node.js。你也可以考虑使用 Docker 或其他容器技术来为你的应用程序创建一个独立的环境。

希望这些建议能帮助你解决问题!

1 个回答

nodejs这个包里面已经有npm了。

image.png


image.png

image.png

npm这个包,ubuntu默认的仓库里面默认就有,这个包和nodesource仓库里面冲突应该不算意外。

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