Raspberry Pi に Node.js をインストール

Raspberry Pi に Node.js をインストール

apt により、Node.js もインストールできるのですが、少しバージョンが古いものがインストールされてしまいます。

最新のバージョンをインストールするには、下記を参考に Node.js のパッケージをインストールします。

インストールする環境の確認

Raspberry Pi 3B / Raspbian buster にインストールします。

$ cat /proc/device-tree/model 
Raspberry Pi 3 Model B Rev 1.2

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Raspbian
Description:	Raspbian GNU/Linux 10 (buster)
Release:	10
Codename:	buster

レポジトリの取得

$ curl -sL https://deb.nodesource.com/setup_13.x | sudo bash -

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


## Populating apt-get cache...

+ apt-get update
ヒット:1 http://raspbian.raspberrypi.org/raspbian buster InRelease
ヒット:2 http://archive.raspberrypi.org/debian buster InRelease
パッケージリストを読み込んでいます... 完了

## Confirming "buster" is supported...

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

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

+ curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add -
OK

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

+ echo 'deb https://deb.nodesource.com/node_13.x buster main' > /etc/apt/sources.list.d/nodesource.list
+ echo 'deb-src https://deb.nodesource.com/node_13.x buster main' >> /etc/apt/sources.list.d/nodesource.list

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

+ apt-get update
取得:1 https://deb.nodesource.com/node_13.x buster InRelease [4,584 B]
ヒット:2 http://raspbian.raspberrypi.org/raspbian buster InRelease             
ヒット:3 http://archive.raspberrypi.org/debian buster InRelease                
取得:4 https://deb.nodesource.com/node_13.x buster/main armhf Packages [764 B]
5,348 B を 1秒 で取得しました (3,677 B/s)
パッケージリストを読み込んでいます... 完了

## Run `sudo apt-get install -y nodejs` to install Node.js 13.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 | sudo apt-key add -
     echo "deb 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

インストール

Node.js のレポジトリが追加されたので、apt を使ってインストールします。

$ sudo apt-get install -y nodejs
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています                
状態情報を読み取っています... 完了
以下のパッケージが新たにインストールされます:
  nodejs
アップグレード: 0 個、新規インストール: 1 個、削除: 0 個、保留: 0 個。
21.9 MB のアーカイブを取得する必要があります。
この操作後に追加で 108 MB のディスク容量が消費されます。
取得:1 https://deb.nodesource.com/node_13.x buster/main armhf nodejs armhf 13.5.0-1nodesource1 [21.9 MB]
21.9 MB を 4秒 で取得しました (5,631 kB/s)
以前に未選択のパッケージ nodejs を選択しています。
(データベースを読み込んでいます ... 現在 41543 個のファイルとディレクトリがインストールされています。)
.../nodejs_13.5.0-1nodesource1_armhf.deb を展開する準備をしています ...
nodejs (13.5.0-1nodesource1) を展開しています...
nodejs (13.5.0-1nodesource1) を設定しています ...
man-db (2.8.5-2) のトリガを処理しています ...

バージョンの確認

インストールされたバージョンを確認します。

$ nodejs -v
v13.5.0
$ npm -v
6.13.4

Raspberry Pi Zero へのインストール

Raspberry Pi Zero にも Node.js をインストールします。

$ cat /proc/device-tree/model 
Raspberry Pi Zero Rev 1.3

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Raspbian
Description:	Raspbian GNU/Linux 10 (buster)
Release:	10
Codename:	buster

Raspberry Pi Zero で同様にインストールしようとすると、

$ curl -sL https://deb.nodesource.com/setup_13.x | sudo bash -

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


## You appear to be running on ARMv6 hardware. Unfortunately this is not currently supported by the NodeSource Linux distributions. Please use the 'linux-armv6l' binary tarballs available directly from nodejs.org for Node.js 4 and later.

となり、直接 linux-armv6l 版をインストールするように促されます。

Node.js の公式ページから辿り、Node.js Unofficial Builds Project から、linux-armv6l 版をダウンロードします。

$ wget https://unofficial-builds.nodejs.org/download/release/v13.5.0/node-v13.5.0-linux-armv6l.tar.gz

ダウンロードしたアーカイブを展開し、/usr/local 以下にコピーしインストールします。

$ tar xvfz node-v13.5.0-linux-armv6l.tar.gz
$ cd node-v13.5.0-linux-armv6l
$ rm CHANGELOG.md LICENSE README.md
$ sudo cp -R * /usr/local/

バージョンを確認します。ちゃんと動きました。

$ node -v
v13.5.0
$ npm -v
6.13.4