There are several ways that you can set up Node.js on a Raspberry Pi when running Raspbian/Rapberry Pi OS. Depending on your needs, the version of the RPi that you're using, and how you like to manage installs, you have a lot of options.
Do not do this if you can avoid it, it's super slow. If you insist on doing it and have the time, you can start at https://nodejs.org. But really, don't do this.
If you have tons of time on your hands, don't need Node anytime soon, and insist on building from source for some reason, here's a guide you can try out that covers building Node.js on an ARMv6 Raspberry Pi.
Side note: unless you have a need for the latest and greatest features, I recommend developing using the most recent Long Term Support (LTS) version of Node available, especially for anything you plan to put into production for any length of time.
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.
Fortunately, there's another way. In fact, there are several ways…
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:
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - &&\
sudo apt-get install -y nodejs
This will install the current LTS version of Node. You can also install the current version:
curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash - &&\
sudo apt-get install -y nodejs
Or the most recent available release of a specific major version:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - &&\
sudo apt-get install -y nodejs
This method lets you pick a modern version of Node.js, and still use apt
, so you can get updates pretty easily. Unfortunately, NodeSource only build for armv7/armv8, 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.
Side note: you can choose to use sudo or install as root; I've chosen to show the former version as it's a bit safer. For more details, see the install documentation in the NodeSource repo.
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. See below for unofficial builds, which will keep your Node.js updated, patched, and supported, but avoids you having to build it yourself.
The good news: you can still get up-to-date armv6 builds from the Unoffical Builds project, including the lastest v14.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
I got the lowdown on that from https://github.com/nebrius/raspi-io/wiki/Getting-a-Raspberry-Pi-ready-for-NodeBots. 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.
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 mutiple 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). This is the method I use on one of my older Pis, and I find that it makes upgrading fairly easy (though far more time-consuming that using nvm
or apt
).
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
I choose to do this, but it means I have to update my symlinks on installation of a new version of Node.
NVM is a great tool for installing and managing multiple versions of Node.js. I'm not going into detail here (using the installer script and the tools is already well documented), but this is my goto 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
To install the latest version of Node using NVM, whatever that happens to be, simply do this:
nvm install
If you want to install the latest Long Term Support version of Node with NVM, use:
nvm install --lts
You can also install specific versions, using partial or complete semantic versioning (will install the most recent version that matches the level of semver specified):
nvm install 18
nvm install 18.16
nvm install 18.16.1
You can also use labels:
nvm install lts/hydrogen
More detail on NVM here available here: https://github.com/nvm-sh/nvm.
Hi. Nice guide!
I wrote a guide specifically about building Node.js from source (which you recommend not to do).
Since your guide skips over that part, but is found on Google when searching "build node.js from source raspberry pi", it might help others if you link to my guide in that section: Guide to build Node.js from source on ARMv6.
Just an idea though.
Keep up the great work!