So you want to use your Raspberry Pi as a NodeJS server? You are at the right place! In this short tutorial we will go through how to install NodeJS and the Node Package Manager (NPM) on the Raspberry Pi.
Step 1: Update & Upgrade your Pi
To prevent any issues with compatibility, always update the dependencies list on the Raspberry Pi and upgrade the libraries on the Pi before installing new libraries. Run the following commands in the terminal.
sudo apt-get update
This command updates the list of packages that can be installed, and checks if currently installed packages have any new updates. After the update of the list is completed, run the following command to install the updates.
sudo apt-get upgrade
It takes quite some time for the Raspberry Pi to be updated depending on which version of Raspbian you are using. Once that's done we can move on to the next step!
Step 2: Determine which version of Node you need
The Raspberry Pi runs off the ARM architecture and as of the writing of this article, NodeJS releases compiled Linux binaries for ARMv6, ARMv7 and ARMv8 architecture boards.
To find out which architecture your Raspberry Pi is running on, run the following command in the terminal
For example, if we run the above command on the Raspberry Pi Zero W, we get the following output.
For easy reference, we have compiled a list of common Raspberry Pi boards and their architectures. (Note that your board might have a different architecture even though the board name is the same as our table as within the same model number there are different revisions, safest bet is to run the above command)
|Raspberry Pi 4B
|Raspberry Pi 3 /3B+
|Raspberry Pi 2B v1.2
|Raspberry Pi 2B
|Raspberry Pi Zero /Zero W
Step 3: Download the NodeJS Binaries
Visit the NodeJS downloads page and copy the link to the version that you need.
If you cannot find your version's download link, it might be that it has not been released for the latest version of NodeJS. For example, the Raspberry Pi Zero W runs on ARMv6 architecture, but the link is not showing in our screenshot. This is because the ARMv6 binary for NodeJS v12 has not been released. To find the binary for the previous version of NodeJS, visit the previous releases page. Usually the version that is one major revision preceding the LTS version should have binaries for all the architectures. In our case, that is NodeJS v11. So on the page, we will look for "Node.js v11.X.X" where X is the latest revision of that major revision.
Once you have the link for the binaries, you can go ahead and download the archive onto your Raspberry Pi using wget.
wget [COPIED LINK HERE]
Replace [COPIED LINK HERE] with the link you copied from the downloads page. In our case, we needed the binaries for the ARMv6 architecture of the Raspberry Pi Zero W. At the time of writing, the latest NodeJS binary for ARMv6 was v11.15.0. Hence, our command to download was the following.
The download may take some time depending on your internet connection. If the download fails you can always download the file on your laptop and transfer the file to your Raspberry Pi with a removable drive (assuming you are not running it headless)
Step 4: Extract the file
Once the file in on your Raspberry Pi, you need to extract it. To do so, we can use the built in command "tar"
tar -xzf node-vXX.XX.X-linux-armvXl.tar.gz
Change X to your downloaded file name. In our example, we would use the following command
tar -xzf node-v11.15.0-linux-armv6l.tar.gz
The "-xzf" flag is actually 3 flags combined into one, it could also be written as "-x -z -f". The "-x" flag is to tell the program to "extract". The "-z" flag is to tell the program to use "gunzip" for the extraction as the file is an archive ending in ".gz". And finally the "-f" flag simply just means "perform operation on this file".
Step 5: Copy the files to a directory in PATH
To be able to run node from any directory, we need to copy the files extracted to a folder that is in PATH. One possibility is to copy it to the "/usr/local" folder.
As before, change the X to whatever version you need. In our case, we used the following
To copy the files, we use the cp command. The "-R" flag is to signify to the program to copy all the files recursively, i.e include all files within folders. "*" just means to copy everything in the folder.
sudo cp -R * /usr/local/
Step 6: Check if installation was successful
To check if the installation was successful, simply run the following commands to check the versions of NodeJS and NPM in any directory.
If all works well, the above commands should output the version numbers of NodeJS and NPM.
Well, that's all you need to do to install NodeJS and NPM on your Raspberry Pi. Hope that this tutorial has helped some of you out there. If you run into any issues, just leave us a comment down below and we will try to help you guys as soon as possible! :)