【Ubuntu】LTS版のNode.jsをインストールしてみる
Ubuntu で node コマンドが使えなくなりました。。。
そもそも前から使えていたか微妙ですが、node コマンドが使えるようにしていきます!
エラー
node コマンドを実行すると、このようなエラーが出ました。
~$ node -v
-bash: /usr/bin/node: cannot execute binary file: Exec format error
原因調査
エラー文で調べてみると、
node の実行ファイルが OS(Ubuntu)に対応していないこと
が原因のようです。
バージョンを確認してみる
// sudo apt install nodejs でインストールされるNodejsのバージョンを確認
~$ apt show nodejs
Package: nodejs
Version: 12.22.9~dfsg-1ubuntu3
Priority: extra
Section: universe/web
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian Javascript Maintainers <pkg-javascript-devel@alioth-lists.debian.net>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 932 kB
Provides: node-types-node (= 12.20.42~12.22.9~dfsg-1ubuntu3)
Depends: libc6 (>= 2.34), libnode72 (= 12.22.9~dfsg-1ubuntu3)
Recommends: ca-certificates, nodejs-doc
Suggests: npm
Breaks: node-babel-runtime (<< 7), node-typescript-types (<< 20210110~)
Homepage: https://nodejs.org/
Download-Size: 122 kB
APT-Manual-Installed: yes
APT-Sources: http://archive.ubuntu.com/ubuntu jammy/universe amd64 Packages
Description: evented I/O for V8 javascript - runtime executable
Node.js is a platform built on Chrome's JavaScript runtime for easily
building fast, scalable network applications. Node.js uses an
event-driven, non-blocking I/O model that makes it lightweight and
efficient, perfect for data-intensive real-time applications that run
across distributed devices.
.
Node.js is bundled with several useful libraries to handle server
tasks:
.
System, Events, Standard I/O, Modules, Timers, Child Processes, POSIX,
HTTP, Multipart Parsing, TCP, DNS, Assert, Path, URL, Query Strings.
ということで sudo apt install nodejs で 12.22.9 がインストールされることがわかりました。
現在(2023/06)の LTS が 18.16.1です。
12.× 系が 2022/04/30 に EOL を迎えているので、18.× 系をインストールできるようにします。
ついでにUbuntsu のバージョンも確認しておきます
~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.2 LTS
Release: 22.04
Codename: jammy
Ubuntu のバージョンは22.04みたいです。
現在の Node.js をアンインストール
apt remove --purge コマンドでインストールされている Node.js を完全削除します
完全削除とは、パッケージ+環境設定を削除するということです
// インストールされているが実行できない
~$ node -v
-bash: /usr/bin/node: cannot execute binary file: Exec format error
// Node.jsを完全削除
~$ sudo apt remove --purge nodejs -y
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 libflashrom1 libftdi1-2 libjs-highlight.js libnode72 nodejs-doc
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
nodejs*
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 932 kB disk space will be freed.
(Reading database ... 48137 files and directories currently installed.)
Removing nodejs (12.22.9~dfsg-1ubuntu3) ...
Processing triggers for man-db (2.10.2-1) ...
// 実行ファイルが見つからない→削除完了
~$ node -v
-bash: /usr/bin/node: No such file or directory
最新のパッケージを取得
こちらを参考にLTS 版(18.×)のパッケージを取得していきます
Node.js 公認の企業が用意している、 インストールを効率的に実行できるスクリプトを使用していきます。
~$ curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
## Installing the NodeSource Node.js 18.x repo...
## Populating apt-get cache...
+ apt-get update
Get:1 https://apt.releases.hashicorp.com jammy InRelease [12.9 kB]
Get:2 https://apt.releases.hashicorp.com jammy/main amd64 Packages [89.0 kB]
Get:3 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Hit:4 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:5 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
Get:6 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [501 kB]
Get:7 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [108 kB]
Get:8 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [724 kB]
Get:9 http://security.ubuntu.com/ubuntu jammy-security/main Translation-en [129 kB]
Get:10 http://security.ubuntu.com/ubuntu jammy-security/main amd64 c-n-f Metadata [10.3 kB]
Get:11 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [740 kB]
Get:12 http://security.ubuntu.com/ubuntu jammy-security/universe Translation-en [131 kB]
Get:13 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 c-n-f Metadata [15.6 kB]
Get:14 http://archive.ubuntu.com/ubuntu jammy-updates/main Translation-en [190 kB]
Get:15 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 c-n-f Metadata [15.3 kB]
Get:16 http://archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 Packages [418 kB]
Get:17 http://archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 c-n-f Metadata [604 B]
Get:18 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [932 kB]
Get:19 http://archive.ubuntu.com/ubuntu jammy-updates/universe Translation-en [199 kB]
Get:20 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 c-n-f Metadata [20.5 kB]
Get:21 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [22.2 kB]
Get:22 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 c-n-f Metadata [552 B]
Fetched 4488 kB in 5s (888 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 https://apt.releases.hashicorp.com jammy InRelease
Get:2 https://deb.nodesource.com/node_18.x jammy InRelease [4563 B]
Get:3 https://deb.nodesource.com/node_18.x jammy/main amd64 Packages [776 B]
Hit:4 http://archive.ubuntu.com/ubuntu jammy InRelease
Hit:5 http://security.ubuntu.com/ubuntu jammy-security InRelease
Hit:6 http://archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:7 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
Fetched 5339 B in 2s (2594 B/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
LTS 版ではなく、最新版をインストールしたい方は下記コマンドを実行してください
curl -fsSL https://deb.nodesource.com/setup_latest.x | sudo -E bash -
apt show コマンドで Node.js のバージョンを見ていきます
~$ apt show nodejs
Package: nodejs
Version: 18.16.0-deb-1nodesource1
Priority: optional
Section: web
Maintainer: Operations Nodesource <operations@nodesource.com>
Installed-Size: 187 MB
Provides: nodejs-dev, nodejs-doc, nodejs-legacy, npm
Depends: libc6 (>= 2.28), libgcc1 (>= 1:3.4), libstdc++6 (>= 5.2), python3-minimal, ca-certificates
Conflicts: nodejs-dev, nodejs-doc, nodejs-legacy, npm
Replaces: nodejs-dev (<= 0.8.22), nodejs-legacy, npm (<= 1.2.14)
Homepage: https://nodejs.org
Download-Size: 28.7 MB
APT-Sources: https://deb.nodesource.com/node_18.x jammy/main amd64 Packages
Description: Node.js event-based server-side javascript engine
Node.js is similar in design to and influenced by systems like
Ruby's Event Machine or Python's Twisted.
.
It takes the event model a bit further - it presents the event
loop as a language construct instead of as a library.
.
Node.js is bundled with several useful libraries to handle server tasks :
System, Events, Standard I/O, Modules, Timers, Child Processes, POSIX,
HTTP, Multipart Parsing, TCP, DNS, Assert, Path, URL, Query Strings.
N: There is 1 additional record. Please use the '-a' switch to see it
18.16.0が取得できています!!!
再度 Node.js をインストール
これで 18.16.0 がインストールできるようになったので、Node.js を入れていきます
~$ sudo apt install nodejs -y
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 libflashrom1 libftdi1-2 libjs-highlight.js libnode72
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
nodejs-doc
The following NEW packages will be installed:
nodejs
Unpacking nodejs (18.16.0-deb-1nodesource1) ...
dpkg: error processing archive /var/cache/apt/archives/nodejs_18.16.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.16.0-deb-1nodesource1_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)
んー、エラーになってますね。。
見てみると、
The following packages were automatically installed and are no longer required:
javascript-common libc-ares2 libflashrom1 libftdi1-2 libjs-highlight.js libnode72
Use 'sudo apt autoremove' to remove them.
とでています。
使っていないパッケージがあるので、削除してくださいみたいなメッセージです
実行コマンドも表示してくれているので実行してみます
~$ sudo apt autoremove
javascript-common libc-ares2 libflashrom1 libftdi1-2 libjs-highlight.js libnode72
0 upgraded, 0 newly installed, 6 to remove and 4 not upgraded.
After this operation, 44.5 MB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 47897 files and directories currently installed.)
Removing javascript-common (11+nmu1) ...
Removing libnode72:amd64 (12.22.9~dfsg-1ubuntu3) ...
Removing libc-ares2:amd64 (1.18.1-1ubuntu0.22.04.2) ...
Removing libflashrom1:amd64 (1.2-5build1) ...
Removing libftdi1-2:amd64 (1.5-5build3) ...
Removing libjs-highlight.js (9.18.5+dfsg1-1) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...
もう一度インストールしていきます
// Node.jsをインストール
~$ sudo apt install nodejs -y
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 4 not upgraded.
Need to get 0 B/28.7 MB of archives.
After this operation, 187 MB of additional disk space will be used.
(Reading database ... 47763 files and directories currently installed.)
Preparing to unpack .../nodejs_18.16.0-deb-1nodesource1_amd64.deb ...
Unpacking nodejs (18.16.0-deb-1nodesource1) ...
Setting up nodejs (18.16.0-deb-1nodesource1) ...
Processing triggers for man-db (2.10.2-1) ...
Scanning processes...
Scanning candidates...
Scanning processor microcode...
Scanning linux images...
Failed to retrieve available kernel versions.
Failed to check for processor microcode upgrades.
No services need to be restarted.
No containers need to be restarted.
User sessions running outdated binaries:
××× @ /dev/tty1: bash[9]
××× @ /dev/tty2: bash[174]
root @ /dev/pts/0: apt[1068,1136], sh[1141]
root @ /dev/tty1: SessionLeader[8]
root @ /dev/tty2: SessionLeader[173]
No VM guests are running outdated hypervisor (qemu) binaries on this host.
// インストールしたバージョンを確認
~$ node -v
v18.16.0
インストールできましたーーー!!!
参考文献
Discussion