Install NodeJs on Raspberry Pi

What about ARM6?

There is one way to install nodeJs on a Raspberry Pi, but this method has pitfalls. If you are like me and have more than one Raspberry Pi, then you have an issue. Installing NodeJs on a raspberry pi 1 or zero can't be done by adding the repository to your Raspbian system. I made a simple script that will check the processor of your pi for you, and install the latest version of NodeJs without worrying. If you have a pi, no matter which type of pi, then this will work universally.

Here is the code, and then I will break down the shell script so that it makes a little more sense:

cd;  
git clone https://github.com/audstanley/NodeJs-Raspberry-Pi;  
cd NodeJs-Raspberry-Pi;  
chmod +x Install-Node.sh;  
sudo ./Install-Node.sh;  
cd .. && rm -R -f NodeJs-Raspberry-Pi/;  
node -v;  

Here is the link to GitHub.

GitClonesGraph The script is getting a fair amount of attention. So I wrote another application to send me push notifications when NodeJs updates to the next integer version. This way, I can keep the script up to date on Node future releases.


The tl;dr part:

Breaking down the code block:
  1. cd;
    • change directory to my "home directory" aka: /home/pi/
  2. git clone https://github.com/audstanley/NodeJs-Raspberry-Pi;
    • This will clone the script that you will see below
  3. cd NodeJs-Raspberry-Pi;
    • change directory into the project folder that was just copied from the internet.
  4. chmod +x Install-Node.sh;
    • make the script executable since it needs to be able to execute.
  5. sudo ./Install-Node.sh;
    • "run the code as the pi's administrator" aka sudo
    • The reason linux type systems sometimes need sudo prefixing a command is because if you want to install something or change some system configuration, the console just needs to know that you are the admin.
  6. cd .. && rm -R -f NodeJs-Raspberry-Pi/;
    • change directory up one level, and then remove the files that we just downloaded and ran from GitHub.
    • this is done because you don't need the installation script after node, and npm are installed.
  7. node -v;
    • run node, and tell me what version I'm now running.

Even more tl;dr:

Breaking down the Install-Node.sh:
  • Comments are in the script.
#!/bin/bash
# written by Richard Stanley;
PICHIP=$(uname -m);  
if [ "$EUID" -ne 0 ]  
        then echo "You need to install as root by using sudo ./Install-Node.sh";
        exit
else LINKTONODE=$(curl -G https://nodejs.org/dist/latest-v7.x/ | awk '{print $2}' | grep -P 'href=\"node-v7\.\d{1,}\.\d{1,}-linux-'$PICHIP'\.tar\.gz' | sed 's/href="//' | sed 's/<\/a>//' | sed 's/">.*//');  
# curl -G https://nodejs.org/dist/latest-v7.x/ | awk '{print $2}' | grep -P 'href=\"node-v7\.\d{1,}\.\d{1,}-linux-armv7l\.tar\.gz' | sed 's/href="//' | sed 's/<\/a>//' | sed 's/">.*//'
NODEFOLDER=$(echo $LINKTONODE | sed 's/.tar.gz/\//');  
#Shell script created by @audstanley
#Next, Creates directory for downloads, and downloads node 7.x
cd ~/ && mkdir tempNode && cd tempNode && wget https://nodejs.org/dist/latest-v7.x/$LINKTONODE;  
tar -xzf $LINKTONODE;  
#Remove the tar after extracing it.
rm $LINKTONODE;  
#remove older version of node:
rm -R -f /opt/nodejs/;  
#remove symlinks
rm /usr/bin/node /usr/sbin/node /sbin/node /sbin/node /usr/local/bin/node /usr/bin/npm /usr/sbin/npm /sbin/npm /usr/local/bin/npm;  
#This next line will copy Node over to the appropriate folder.
mv /root/tempNode/$NODEFOLDER /opt/nodejs/;  
#This line will remove the nodeJs tar we downloaded.
rm -R -f /root/tempNode/$LINKTONODE/;  
#Create symlinks to node && npm
sudo ln -s /opt/nodejs/bin/node /usr/bin/node; sudo ln -s /opt/nodejs/bin/node /usr/sbin/node;  
sudo ln -s /opt/nodejs/bin/node /sbin/node; sudo ln -s /opt/nodejs/bin/node /usr/local/bin/node;  
sudo ln -s /opt/nodejs/bin/npm /usr/bin/npm;  
sudo ln -s /opt/nodejs/bin/npm /usr/sbin/npm; sudo ln -s /opt/nodejs/bin/npm /sbin/npm;  
sudo ln -s /opt/nodejs/bin/npm /usr/local/bin/npm;  
rm -R -f /root/tempNode/;  
fi