A few weeks ago I thought I’d try to install Node.js on my Raspberry Pi Zero W to use it as a simple API server that I could take with me on the road. I have installed Node.js so many times before, including on a standard Raspberry Pi, that I figured it would be just as easy to do on the Pi Zero.
I was mistaken in regards to the difficulty.
On the Raspberry Pi Zero W, Node.js was nowhere to be found in
apt-get and the scripts that used NodeSource in the official install documentation didn’t work because ARMv6l is no longer supported by NodeSource.
We’re going to see how to install Node.js on a Raspberry Pi Zero W when everything else fails.
When you can’t use
apt-get and there are no binaries available in NodeSource, you might think that your only option is to install Node.js from source. You don’t want to do that, trust me. If you try to build Node.js from source on a Raspberry Pi Zero W, you’ll likely be dead by the time it finishes. Sure you could try to cross-compile, but that is a pain as well.
Instead, we’re going to download a binary from the official Node.js website and manually install it on our Raspberry Pi Zero W.
You can find a list of distributions here:
At the time of writing this tutorial, the latest version of Node.js is 9.7.1, but in the future this guide should still work, as long as the appropriate builds are available.
When you find the version you’d like to install, you’re looking for the armv6l architecture. For example:
Now you might be wondering, well how is this different from what I’d get on NodeSource? Remember, the recommended way to install Node.js is by executing a script which places something in your
apt-get repository list. This method of downloading the node-v9.7.1-linux-armv6l.tar.gz file doesn’t help us in the slightest in terms of
SSH into the Raspberry Pi Zero W and execute the following command:
curl -o node-v9.7.1-linux-armv6l.tar.gz https://nodejs.org/dist/v9.7.1/node-v9.7.1-linux-armv6l.tar.gz
The above command will download the file to your device. Yes, there are probably 100 other ways to accomplish the task, but this is my preference.
With the archive downloaded, execute the following from the device:
tar -xzf node-v9.7.1-linux-armv6l.tar.gz
The above command will extract the archive, creating a new directory at the current path. The directory includes everything we need to use Node.js and the Node Package Manager (NPM). We just need to transfer the content to the correct locations.
Execute the following on the Raspberry Pi Zero W:
sudo cp -r node-v9.7.1-linux-armv6l/* /usr/local/
The content copied should already be in your
$PATH, so at this point you’re ready to start using Node.js. You can validate that everything works by executing the following:
node -v npm -v
One other thing to note is that NPM relies heavily on Git being available. By default the Raspbian Linux distribution will not have Git already downloaded. It can easily be installed by executing the following:
sudo apt-get install git
You can start developing Node.js applications to be deployed on your Pi Zero W or any other device for that matter, as long as you find the correct build architecture.
You just saw how to install Node.js on a Raspberry Pi Zero W. If you’re like me, you found out the hard way that the Raspberry Pi Zero and standard Raspberry Pi use two very different CPU architectures. While the standard Raspberry Pi can install Node.js as per the official documentation, we aren’t so lucky with the Raspberry Pi Zero. Lucky for us, Node.js still maintains an ARMv6l build that we can use.
It is important to note that just because you can use Node.js on your Pi Zero W, doesn’t mean that it will be fast. These are low power Internet of Things (IoT) devices, so they are not designed to do a lot of crunching.
A video version of this article can be seen below.
Unsubscribe at any time without hassle.