Tutorial, JavaScript, Node.js, Raspberry Pi

Installing Node.js on a Raspberry Pi

TL;DR: If you don't want the backstory for this tutorial, you can skip directly to the New Approach section towards the end.

A little while back, I created a series of videos demonstrating how to install Node.js on a Raspberry Pi. However, I recently tried installing it in the same manner for a new home automation project, but the version I installed did not meet the requirements for the NPM packages used in the project.

I had long assumed that the steps I had been following would always get me the latest version, so I was a bit confused when my NPM install failed due to a versioning issue. After a bit of digging, I realized my assumptions were wrong and that I should have been paying way more attention to version numbers all along.

"Dammit, Jim."

VERSION CONFUSION

When I first attempted to install Node.js on a Raspberry Pi many moons ago, I had read that the version of Node.js included with the default APT repository on Raspbian is usually severely outdated, so once I stumbled across Adafruit's installation instructions (which I taught in my videos referenced above), I assumed that the version I would be getting would always be the latest and greatest.

At first, that assumption was correct, because at the time when I found the instructions, the latest version of Node.js WAS being installed. The repo just stopped being updated at one point, however, and I didn't realize that until recently.

Say or think what you will, but I will freely admit that over the years, I have noticed that the Node.js versions I've installed on my Raspberry Pi's have been 0.12.x, and that I have seen newer versions listed on the Node.js website. I did wonder why at times, but - and this is were my naiveté with Node.js comes into play - I always thought there was a different series of version numbers for the ARM architecture (which is what the Raspberry Pi uses), so I just always chalked it up to that and moved on.

Until recently, I never had any versioning issues between Node.js and the NPM packages being installed, so I never bothered to investigate this discrepancy any further. My latest project put me in a position where I had to find out what was going on in order to get things working again, however, and I finally uncovered the truth.

MY LIGHT BULB MOMENT

Part of my version confusion was due to the fact that the Node.js Foundation offers Long-term Support (LTS) for various versions of Node.js, so if you look at the "Releases" page, you'll see that the latest version of 0.12.x (v0.12.18) was actually released earlier this year on 2/22/17.

I'm familiar with LTS, but since I thought 0.12.x. was a separate branch or version of Node.js specific to the ARM architecture (it's not, by the way), seeing fairly recent dates for 0.12.x further confirmed my assumptions.

Things still didn't make sense, though, and I thought to myself:

"Self - if v0.12.18 is the latest version for the ARM architecture, then why was NPM saying it required version 5.3.0 or later? How the fuck am I supposed to get version 5.3.0 on my Pi if it's not even compatible with the ARM architecture?"

At that point, I checked the version of Node.js I just installed to ensure I was running v0.12.18:

node --version

And that's when it hit me. I was only running v0.12.6, which meant I wasn't even getting the latest version of the v0.12.x series from Adafruit!

Huh.

"Maybe v0.12.x isn't specific to the ARM architecture after all, and maybe - just maybe - Adafruit stopped updating Node.js in its repository a long time ago?"

Bingo bango.

END-OF-LIFE

Well, it wasn't necessarily Adafruit who stopped updating the package, but a third-party site from which Adafruit pulls Node.js.

Based on this post, Adafruit pulls its Node.js package from the node-arm project, and the version of Node.js hosted on that site is v0.12.6, which was released over two years ago! The reason I was seeing recent dates on the "Releases" page for v0.12.x is because it had a LTS maintenance cycle that expired on 12/31/16.

It all made so much more sense!

At the time of this writing, the current version is 8.4.0, and v0.12.x has officially reached an "End-of-Life" LTS status, so at this point, I think it's safe to say that the node-arm project is no longer being maintained.

Even if Adafruit is still actively maintaining its package repository, as long as it continues pulling Node.js from the node-arm project, you won't get the latest version if you use those instructions I taught in my videos.

Depending on the packages you're trying to install, this may or may not cause problems for you. Since I was trying to install a package for my project that was dependent on a newer version of Node.js, I had to seek out a different approach.

NEW APPROACH

Luckily, the package I was trying to install referenced a different set of installation instructions for Node.js, so I figured those would be golden, especially since the instructions were taken directly from the Node.js website!

You can dig into the full instructions here if you'd like a deeper explanation, but if you're running a Raspberry Pi with an ARMv7 architecture and a newer distro, such as Raspbian (Debian) Jessie, you can just run the following two commands to install the latest version of Node.js:

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs

The first line just simply utilizes cURL to pull down the bash setup script from nodesource.com for v8.x of Node.js, and then pipes it into bash to be executed. The -E option on the sudo command indicates that the user's environment should be preserved (including environment variables), and the trailing hyphen ("-") after the bash command indicates that standard input should be used as the script source, instead of an actual file located on disk.

The gist of it is that a new package repository is being added, which houses the latest version of Node.js. This way, you can use the apt-get install command (the second line in the code block above) to install Node.js, just like you would with other Linux packages.

Of course, if you want to install a different version of Node.js, you can plug in a different setup script in the first line. Take a look at NodeSource's GitHub repo to see the available setup scripts you can use.

One final thing to note, too, is that if you're trying to install Node.js on an older Raspberry Pi and/or distribution, you probably won't be able to utilize these commands, so you may want to check out this guide to see if that will help you get squared away.

At this point, you should be able to get up and running with the latest version of Node.js on your Raspberry Pi.

You're welcome.

Author image

About Tony Thorsen

Father of two, husband of one, Maker of many things. Tinkerer, dreamer, pixel nudger.