在Ubuntu22.04上安装Node.js 18

如果正常安装 sudo apt install nodejs,最终将得到一个非常旧的版本 v12。

$ sudo apt install nodejs
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following package was automatically installed and is no longer required:
  libnuma1
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
  javascript-common libc-ares2 libjs-highlight.js libnode72 nodejs-doc
Suggested packages:
  npm
The following NEW packages will be installed:
  javascript-common libc-ares2 libjs-highlight.js libnode72 nodejs nodejs-doc
0 upgraded, 6 newly installed, 0 to remove and 38 not upgraded.
Need to get 13.7 MB of archives.
After this operation, 53.9 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu jammy/main amd64 javascript-common all 11+nmu1 [5936 B]
Get:2 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu jammy/universe amd64 libjs-highlight.js all 9.18.5+dfsg1-1 [367 kB]
Get:3 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu jammy/main amd64 libc-ares2 amd64 1.18.1-1build1 [44.9 kB]
Get:4 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu jammy/universe amd64 libnode72 amd64 12.22.9~dfsg-1ubuntu3 [10.8 MB]
Get:5 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu jammy/universe amd64 nodejs-doc all 12.22.9~dfsg-1ubuntu3 [2409 kB]
Get:6 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu jammy/universe amd64 nodejs amd64 12.22.9~dfsg-1ubuntu3 [122 kB]
Fetched 13.7 MB in 4s (3641 kB/s)
Selecting previously unselected package javascript-common.
(Reading database ... 65209 files and directories currently installed.)
Preparing to unpack .../0-javascript-common_11+nmu1_all.deb ...
Unpacking javascript-common (11+nmu1) ...
Selecting previously unselected package libjs-highlight.js.
Preparing to unpack .../1-libjs-highlight.js_9.18.5+dfsg1-1_all.deb ...
Unpacking libjs-highlight.js (9.18.5+dfsg1-1) ...
Selecting previously unselected package libc-ares2:amd64.
Preparing to unpack .../2-libc-ares2_1.18.1-1build1_amd64.deb ...
Unpacking libc-ares2:amd64 (1.18.1-1build1) ...
Selecting previously unselected package libnode72:amd64.
Preparing to unpack .../3-libnode72_12.22.9~dfsg-1ubuntu3_amd64.deb ...
Unpacking libnode72:amd64 (12.22.9~dfsg-1ubuntu3) ...
Selecting previously unselected package nodejs-doc.
Preparing to unpack .../4-nodejs-doc_12.22.9~dfsg-1ubuntu3_all.deb ...
Unpacking nodejs-doc (12.22.9~dfsg-1ubuntu3) ...
Selecting previously unselected package nodejs.
Preparing to unpack .../5-nodejs_12.22.9~dfsg-1ubuntu3_amd64.deb ...
Unpacking nodejs (12.22.9~dfsg-1ubuntu3) ...
Setting up javascript-common (11+nmu1) ...
apache2_invoke: Enable configuration javascript-common
Setting up libc-ares2:amd64 (1.18.1-1build1) ...
Setting up libnode72:amd64 (12.22.9~dfsg-1ubuntu3) ...
Setting up libjs-highlight.js (9.18.5+dfsg1-1) ...
Setting up nodejs (12.22.9~dfsg-1ubuntu3) ...
update-alternatives: using /usr/bin/nodejs to provide /usr/bin/js (js) in auto mode
Setting up nodejs-doc (12.22.9~dfsg-1ubuntu3) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...
Scanning processes...                                                                                                                                                                                       
Scanning linux images...                                                                                                                                                                                    

Running kernel seems to be up-to-date.

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.

$ node -v
v12.22.9

尝试指定并安装v18,然后进行擦除操作。

$ sudo apt --purge remove nodejs
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  javascript-common libc-ares2 libjs-highlight.js libnode72 libnuma1
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  nodejs*
0 upgraded, 0 newly installed, 1 to remove and 5 not upgraded.
After this operation, 932 kB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 94926 files and directories currently installed.)
Removing nodejs (12.22.9~dfsg-1ubuntu3) ...
Processing triggers for man-db (2.10.2-1) ...
Scanning processes...                                                                                                                                                                                       
Scanning candidates...                                                                                                                                                                                      
Scanning linux images...                                                                                                                                                                                    

Restarting services...
Service restarts being deferred:
 systemctl restart unattended-upgrades.service

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.
$ curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

## Installing the NodeSource Node.js 18.x repo...


## Populating apt-get cache...

+ apt-get update
Hit:1 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu jammy InRelease
Hit:2 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:3 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu jammy-backports InRelease
Get:4 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Fetched 110 kB in 1s (88.0 kB/s)   
Reading package lists... Done

## Confirming "jammy" is supported...

+ curl -sLf -o /dev/null 'https://deb.nodesource.com/node_18.x/dists/jammy/Release'

## Adding the NodeSource signing key to your keyring...

+ curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | gpg --dearmor | tee /usr/share/keyrings/nodesource.gpg >/dev/null

## Creating apt sources list file for the NodeSource Node.js 18.x repo...

+ echo 'deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_18.x jammy main' > /etc/apt/sources.list.d/nodesource.list
+ echo 'deb-src [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_18.x jammy main' >> /etc/apt/sources.list.d/nodesource.list

## Running `apt-get update` for you...

+ apt-get update
Hit:1 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu jammy InRelease
Hit:2 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:3 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu jammy-backports InRelease
Get:4 https://deb.nodesource.com/node_18.x jammy InRelease [4563 B]                                     
Get:5 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]                               
Get:6 https://deb.nodesource.com/node_18.x jammy/main amd64 Packages [774 B]
Fetched 116 kB in 1s (90.3 kB/s)
Reading package lists... Done

## Run `sudo apt-get install -y nodejs` to install Node.js 18.x and npm
## You may also need development tools to build native addons:
     sudo apt-get install gcc g++ make
## To install the Yarn package manager, run:
     curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null
     echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
     sudo apt-get update && sudo apt-get install yarn
$ sudo apt-get install -y nodejs
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  javascript-common libc-ares2 libjs-highlight.js libnode72 libnuma1
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
  nodejs
0 upgraded, 1 newly installed, 0 to remove and 5 not upgraded.
Need to get 0 B/28.5 MB of archives.
After this operation, 186 MB of additional disk space will be used.
Selecting previously unselected package nodejs.
(Reading database ... 94866 files and directories currently installed.)
Preparing to unpack .../nodejs_18.14.0-deb-1nodesource1_amd64.deb ...
Unpacking nodejs (18.14.0-deb-1nodesource1) ...
dpkg: error processing archive /var/cache/apt/archives/nodejs_18.14.0-deb-1nodesource1_amd64.deb (--unpack):
 trying to overwrite '/usr/share/systemtap/tapset/node.stp', which is also in package libnode72:amd64 12.22.9~dfsg-1ubuntu3
Errors were encountered while processing:
 /var/cache/apt/archives/nodejs_18.14.0-deb-1nodesource1_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

由于错误无法输入

由于似乎在最初(v12)安装时附带的依赖库(根据消息来看可能是libnode72)引起了问题,所以需要将它们删除掉。

$ sudo apt autoremove
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
  javascript-common libc-ares2 libjs-highlight.js libnode72 libnuma1
0 upgraded, 0 newly installed, 5 to remove and 5 not upgraded.
After this operation, 43.6 MB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 99901 files and directories currently installed.)
Removing javascript-common (11+nmu1) ...
Conf javascript-common disabled.
apache2_invoke prerm: Disable configuration javascript-common
Removing libnode72:amd64 (12.22.9~dfsg-1ubuntu3) ...
Removing libc-ares2:amd64 (1.18.1-1build1) ...
Removing libjs-highlight.js (9.18.5+dfsg1-1) ...
Removing libnuma1:amd64 (2.0.14-3ubuntu2) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...

再次尝试重新安装

$ sudo apt install -y nodejs
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  nodejs
0 upgraded, 1 newly installed, 0 to remove and 5 not upgraded.
Need to get 0 B/28.5 MB of archives.
After this operation, 186 MB of additional disk space will be used.
(Reading database ... 99773 files and directories currently installed.)
Preparing to unpack .../nodejs_18.14.0-deb-1nodesource1_amd64.deb ...
Unpacking nodejs (18.14.0-deb-1nodesource1) ...
Setting up nodejs (18.14.0-deb-1nodesource1) ...
Processing triggers for man-db (2.10.2-1) ...
Scanning processes...                                                                                                                                                                                       
Scanning linux images...                                                                                                                                                                                    

Running kernel seems to be up-to-date.

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.

$ node -v
v18.14.0

喜闻乐见