Skip to content

Instantly share code, notes, and snippets.

@stonehippo
Last active March 2, 2025 19:33
Show Gist options
  • Save stonehippo/f4ef8446226101e8bed3e07a58ea512a to your computer and use it in GitHub Desktop.
Save stonehippo/f4ef8446226101e8bed3e07a58ea512a to your computer and use it in GitHub Desktop.
Methods for installing Node.js on Raspberry Pi

Setting Up Node.js On Raspberry Pi

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.

Node.js from source

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.

Install with apt

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…

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:

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.

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. 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, as of the end of 2024: you can still get relatively up-to-date armv6 builds from the Unofficial Builds project, through the 20.x LTS and non-LTS 21.x versions: https://unofficial-builds.nodejs.org/. Sadly, it seems that due to some compiler issues, support for builidng 22+ on armv6 is disabled, so you won't be able to get the latest stuff from there for some RPis.

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.

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 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.

Install with nvm

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.

note: nvm will attempt to use pre-compiled binaries for your platform, if available, and will fall back to compiling from source if not. If you are using a Raspberry Pi with an armv6 processor, that is any RPi 1 or Zero (not Zero 2) and you use nvm with default settings to get a moderm Node, you'll be compiling from source. There is a workaround of sorts for this; see Setting You Binary source, below.

A quick summary:

  1. Install nvm with the install script
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash

or

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
  1. 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.

Setting Your Binary Source

If, for some reason, you don't want to use the official NodeJS mirrors with nvm (for example, you're running a Raspberry Pi that has an armv6 processor, like a RPi 1 or Zero), you can change the binary mirror before you install. For example, if you wanted to use the Unofficial Builds binaries:

export NVM_NODEJS_ORG_MIRROR=https://unofficial-builds.nodejs.org/download/release
nvm install node

or

NVM_NODEJS_ORG_MIRROR=https://unofficial-builds.nodejs.org/download/release nvm install 20.18

There are a couple of gotchas with this approach. First, don't export the NVM_NODEJS_ORG_MIRROR in multiple shells, since that can create some race considitions; always do it right before you run install. Secound, a mirror may not be up-to-date. And for Unofficial Builds, versions of NodeJS 22+ binaries aren't available for armv6 Raspberry Pis. :(

@nwawrzyniak
Copy link

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!

@stonehippo
Copy link
Author

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!

Thanks for the suggestion. Linked.

@WhiteyDude
Copy link

It's probably best to update the apt instructions to meet the latest node lts:

sudo curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - &&\
sudo apt-get install -y nodejs

I know the existing example is just that - an example - but people will copy/paste without thinking given this is a high search engine result, and node 12 is EOL over a year ago.

Thanks for the guide!

@stonehippo
Copy link
Author

stonehippo commented Jul 15, 2023

It's probably best to update the apt instructions to meet the latest node lts:

sudo curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - &&\
sudo apt-get install -y nodejs

I know the existing example is just that - an example - but people will copy/paste without thinking given this is a high search engine result, and node 12 is EOL over a year ago.

Thanks for the guide!

Thanks for the nudge to update the guide. Back when I first wrote this, I don't think the tools where as robust around LTS or current label support. Happily, that has changed. I've made a few updates, to reflect installing LTS, current, and specific versions using both NodeSource and NVM.

@peteruithoven
Copy link

peteruithoven commented Dec 30, 2024

I tried nvm install --lts but it ended up trying to compile it from source. I then found the following article, which recommends using unofficial-builds.nodejs.org as a mirror:
https://dev.to/patrickweaver/installing-node-12-and-higher-on-a-raspberry-pi-zero-with-nvm-4dnj
It didn't have 22, but installing 20 from it went super quick.

NVM_NODEJS_ORG_MIRROR=https://unofficial-builds.nodejs.org/download/release nvm install 20

Curious to hear what people think of this approach.

Update, I was indeed trying to install Node on a Raspberry Pi Zero W (not Zero 2).

@stonehippo
Copy link
Author

stonehippo commented Dec 30, 2024

@peteruithoven you can definitely take this approach, as outlined in the nvm readme: https://github.com/nvm-sh/nvm/blob/master/README.md#use-a-mirror-of-node-binaries.

You should probably only do this if you're running an armv6 RPi, like a RPi 1 or Zero (not Zero 2), since the newer RPis and Zero W will get binaries from the main repo.

There is a problem, however. Unofficial builds has stopped compiling armv6 support for NodeJS v22+, at least for now (see commit nodejs/unofficial-builds@f298882). This means that older RPis are stuck at version 20 or lower if you take this approach (it's also why nvm is trying to install the current LTS from source).

I have updated the notes above to reflect this unfortunate turn of events.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment