Questions

Forum Navigation
Please to create posts and topics.

Ways to set up Node.js on Raspberry Pi

Which ways are available to install properly Node.js on Raspberry Pi systems?

There are several ways that you can set up Node.js on a Raspberry Pi when running Raspbian (Debian-based OS).

Node.js from source

Do not do this if you can avoid it, it's super slow. If you insist on and have the time, you can start at https://nodejs.org. But really, don't do this. This method is pretty easy:

$ sudo apt-get update
$ sudo apt-get install nodejs

This works and is a reasonably quick install, but the version of Node.js is likely to be pretty old, since the official repo doesn't get updated often.

Install with apt, using NodeSource Binary Distribution

This is probably your best bet if you only work with one version of Node and still want the convenience of using apt. See the install instructions at Nodesource binary distribution, but here's the basics:

# Using Ubuntu
curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
sudo apt-get install -y nodejs

# Using Debian, as root
curl -fsSL https://deb.nodesource.com/setup_current.x | bash -
apt-get install -y nodejs

This method lets you pick a modern, and current, version of Node.js, and still use apt, so you can get updates pretty easily. Unfortunately, NodeSource only build for armv7/armv8 (so it will work with Raspberry Pi 4), so this won't work for Raspberry Pis that use armv6, like Pi 1 and the Pi Zero/Zero W. For those, see the next choice down.

Install a binary from Nodejs.org

You can download and install binaries of Node.js from https://nodejs.org. This method works for all Raspberry Pi models, as long as the official distribution keeps building for armv6.

Note: it looks like official builds for Node on Linux armv6 stopped with the v12.x releases. The last version to get official builds for this platform was v11.x, which is now retired. I recommend sticking with the v10.X/LTS Dubnium releases. At least that version will get support, including security updates, through April 2021.

You can still get up-to-date armv6 builds from the Unoffical Builds project, including the lastest v12.X LTS versions: https://unofficial-builds.nodejs.org/.

Here's how that works:

wget https://nodejs.org/dist/vX.X.X/node-vX.X.X-linux-armv6l.tar.xz
tar -xf node-vX.X.X-linux-armv6l.tar.xz
sudo mv node-vX.X.X-linux-armv6l /usr/local/node
cd /usr/bin
sudo ln -s /usr/local/node/bin/node node
sudo ln -s /usr/local/node/bin/npm npm
node -v # Verifying that the Node.js install worked
npm -v # Verifying that the npm install worked

The downside of the above is that it doesn't put the /usr/local/node/bin directory on your path, so any binary commands that get installed when you do something like npm i -g cli-providing-package will require using the full path.

Another way to install the binary

An alternative method is described here: https://github.com/nodejs/help/wiki/Installation.

You might prefer setting up Node this way, since it makes it slightly easier to install multiple versions (as it puts each version of Node in a subdirectory of /usr/local/lib/nodejs and adds variables to .profile that can be used to switch the versions).

Unzip the binary archive to any directory you wanna install Node, we can use /usr/local/lib/nodejs:

VERSION=v10.15.0
DISTRO=linux-x64
sudo mkdir -p /usr/local/lib/nodejs
sudo tar -xJvf node-$VERSION-$DISTRO.tar.xz -C /usr/local/lib/nodejs

Set the environment variable ~/.profile, add below to the end:

# Nodejs
VERSION=v10.15.0
DISTRO=linux-x64
export PATH=/usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin:$PATH

Refresh profile:

. ~/.profile

Test installation using:

$ node -v
$ npm -v
$ npx -v

NOTE: if you install with this method, you may find that the node, npm, npx or any other binary commands installed via a global node module do not work correctly with sudo. This is because the default sudoers config uses a safe reset for the path. You can override this by setting the path explicitly, like this:

sudo env "PATH=$PATH" npm -g i some_module

You can also choose to create symlinks so they'll appear in your path:

sudo ln -s `which node` /usr/bin/node
sudo ln -s `which npm` /usr/bin/npm
sudo ln -s `which npx` /usr/bin/npx

Using the above approach means that we have to update symlinks on installation of a new version of Node.

Install with nvm

NVM is a great tool for installing and managing multiple versions of Node.js. We are not going into detail here (using the installer script and the tools is already well documented), but this is only a choice, since it allows for simple upgrades and multiple versions.

A quick summary:

Install nvm with the install script:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash

Or:

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash

Install a version of Node:

nvm install lts/erbium