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)
|Board Name||Architecture Version|
|Raspberry Pi 4B||ARMv8|
|Raspberry Pi 3 /3B+||ARMv7|
|Raspberry Pi 2B v1.2||ARMv7|
|Raspberry Pi 2B||ARMv6|
|Raspberry Pi Zero /Zero W||ARMv6|
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! :)
it got me most of the way there. I did have a hard time understanding at the beginning what version corresponds to what hardware I have. Seeing this page helped me figure some more things out: https://nodejs.org/download/release/v11.15.0/
in my case, i run too
sudo ln -s /usr/local/bin/node /usr/bin/node
Thanks. Might want to add reboot after moving files with
sudo cp -R * /usr/local/
Good tutorial! Not only did you show what the commands should be in “format” form but you then showed an example of an actual command. Additionally, you explained what the flag do.
However, the newest (7/21) tar file has a xz extension which won’t extract with the -xzf flags. Removing the “g” will work with both gz and xz. (https://askubuntu.com/questions/92328/how-do-i-uncompress-a-tarball-that-uses-xz)
e.g. sudo tar -xf node-v14.17.3-linux-armv7l.tar.xz
The only other thing I would add to your tutorial is to remove the tar file as it is not needed anymore and only taking up space.
sudo rm node-v14.17.3-linux-armv7l.tar.xz
Thanks very much! Nice, concise article.
It’s really good, but the uname -y don’t need to be used, just downloa dthe newes to ur operating system.
Thank you for this excellent guide!
You have no idea how long I’ve been searching for why node -v and npm -v wouldn’t return any commands, and it’s because I was once told “just apt-get install node” and I had no idea I needed to actually get and install separate binaries. ( ;)_( ;)
Thank you for this excellent guide!
Franco Rosa Alagon
Hey, this toturial is awesome, read it more than twise, currently the download links use this extension:
Could you update your “tar -xzf” to “tar -xvf” in order to be compatible with tar.gz too.
Awesome! This worked perfect. I had a hard time using other methods
works well! thank you