概要
Raspberry PiでNode-REDを利用していると、インストールしているカスタムノードの都合でNode.jsやNode-REDのバージョンを固定してインストールしたいってことがあります。
そんなときのやり方を紹介します。
そもそもインストールがどうなっているのか
公式サイトを見てみます。Node-RED日本ユーザー会⇒ドキュメント⇒Getting Started⇒Raspberry Pi のページにはこう書いてあります。
https://nodered.jp/docs/getting-started/raspberrypi#node-redをインストールする
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)
これは、GitHub上のnode-redのlinux-installersリポジトリにある、devディレクトリのupdate-nodejs-and-noderedというファイルを取得して、実行するというコマンドです。
このコマンドの中にNode.jsとNode-REDのバージョン指定がされています。ファイルの全容はここで確認できます。執筆時点では、177行目と212行目と224行目にNode.jsのインストールコマンドが、259~263行目にNode-REDのインストールコマンドがあります。
Node.jsのインストールコマンドはaptコマンドで
sudo apt install -y nodejs
Node-REDのインストールコマンドはnpm iコマンドで
npm i -g --unsafe-perm --no-progress node-red@latest
となっていますので、ここにそれぞれバージョンを指定するコマンドに変更します。なお、上記はコマンド前後のsudoやifを除いて抜粋してありますので、それぞれのコマンド行を見て、必要な箇所だけ変更しましょう。
aptでのバージョン指定
aptコマンドのバージョンの指定の仕方はapt install 【パッケージ名】=【バージョン名】のように、イコールでバージョンを付けます。
どんなバージョンがあるのかの調べ方はapt list -a 【パッケージ名】です。
pi@raspberrypi:~ $ apt list -a nodejs
一覧表示... 完了
nodejs/不明 10.18.1-1nodesource1 armhf [10.17.0-1nodesource1 からアップグレード可]
nodejs/now 10.17.0-1nodesource1 armhf [インストール済み、10.18.1-1nodesource1 にアップグレード可]
nodejs/stable 10.15.2~dfsg-2+rpi1 armh
バージョンなので 12.14.1 のような数字なのかと思いきや10.18.1-1nodesource1のような長い名称がバージョン名となっています。
sudo apt install -y nodejs=10.18.1-1nodesource1
のように変更すれば指定のバージョンでインストールできます。
なお、apt-cache showpkg 【パッケージ名】でもインストールできるバージョンの一覧を表示できます。
pi@raspberrypi:~ $ apt-cache showpkg nodejs
Package: nodejs
Versions:
10.18.1-1nodesource1 (/var/lib/apt/lists/deb.nodesource.com_node%5f10.x_dists_buster_main_binary-armhf_Packages)
Description Language:
File: /var/lib/apt/lists/deb.nodesource.com_node%5f10.x_dists_buster_main_binary-armhf_Packages
MD5: 964493985d4a02c9abd7e062f9234325
Description Language:
File: /var/lib/dpkg/status
MD5: 964493985d4a02c9abd7e062f9234325
10.17.0-1nodesource1 (/var/lib/dpkg/status)
Description Language:
File: /var/lib/apt/lists/deb.nodesource.com_node%5f10.x_dists_buster_main_binary-armhf_Packages
MD5: 964493985d4a02c9abd7e062f9234325
Description Language:
File: /var/lib/dpkg/status
MD5: 964493985d4a02c9abd7e062f9234325
10.15.2~dfsg-2+rpi1 (/var/lib/apt/lists/raspbian.raspberrypi.org_raspbian_dists_buster_main_binary-armhf_Packages)
Description Language:
File: /var/lib/apt/lists/raspbian.raspberrypi.org_raspbian_dists_buster_main_binary-armhf_Packages
MD5: 0d0bbaed314d7d26588d112ee4ede074
(以下長いので略)
ちなみに、パッケージの詳細を表示するコマンドはapt show -a 【パッケージ名】です。
pi@raspberrypi:~ $ apt show -a nodejs
Package: nodejs
Version: 10.17.0-1nodesource1
Priority: optional
Section: web
Maintainer: Chris Lea <chl@nodesource.com>
Installed-Size: 72.5 MB
Provides: nodejs-dev, nodejs-legacy, npm
Depends: libc6 (>= 2.17), libgcc1 (>= 1:3.5), libstdc++6 (>= 4.9), python-minima l, ca-certificates
Conflicts: nodejs-dev, nodejs-legacy, npm
Replaces: nodejs-dev (<= 0.8.22), nodejs-legacy, npm (<= 1.2.14)
Homepage: https://nodejs.org
Download-Size: 14.2 MB
APT-Manual-Installed: yes
APT-Sources: https://deb.nodesource.com/node_10.x buster/main armhf 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.
Package: nodejs
Version: 10.15.2~dfsg-2+rpi1
Priority: optional
Section: javascript
Maintainer: Debian Javascript Maintainers <pkg-javascript-devel@lists.alioth.deb ian.org>
Installed-Size: 153 kB
Depends: libatomic1 (>= 4.8), libc-ares2 (>= 1.7.0), libc6 (>= 2.4), libgcc1 (>= 1:3.0), libicu63 (>= 63.1-1~), libnghttp2-14 (>= 1.3.0), libnode64 (= 10.15.2~d fsg-2+rpi1), libssl1.1 (>= 1.1.0), libstdc++6 (>= 4.1.1), libuv1 (>= 1.20.0~), z lib1g (>= 1:1.1.4)
Recommends: ca-certificates, nodejs-doc
Suggests: npm
Conflicts: nodejs-legacy
Replaces: nodejs-legacy
Homepage: http://nodejs.org/
Download-Size: 86.5 kB
APT-Sources: http://raspbian.raspberrypi.org/raspbian buster/main armhf 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.
npm i コマンドのバージョン指定
npmでのバージョン指定はnpm i 【パッケージ名】@【バージョン名】です。
バージョンの検索方法は、npmリポジトリのNode-REDのページを参照します。
www.npmjs.com
ページを開くと、上部にタブが Readme, Explorer, Dependencies, Dependents, Releases と並んでいますので、この中の Releases タブをクリックすると、バージョンの一覧が表示されます。
npm i -g --unsafe-perm --no-progress node-red@0.20.7
のようにインストール対象のバージョンを指定します。
インストールコマンドは、
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)
のようにスクリプトの取得と実行を同時に行うコマンドになっていますので、スクリプトファイルをダウンロード⇒ファイル変更⇒ファイル実行の順で行います。
pi@raspberrypi:~ $ wget https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered
で、まずはファイルを取得します。
pi@raspberrypi:~ $ nano update-nodejs-and-nodered
で、ファイルの編集をします。エディタはviでもnanoでもいいですが、nanoが便利なの。
pi@raspberrypi:~ $ sh update-nodejs-and-nodered
で、スクリプトの実行をします。
参考
nodered.jp
yomon.hatenablog.com