Installing NodeJS on Raspberry Pi

Posted by in Experiments, Technology, Tutorials, Uncategorized

If you ask me Why, I’d ask why not?! RPi is a tiny linux computer with very limited hardware resources. If you’re planning to build a webserver on it, you’ll get to know the odds when you start using it.  Its totally functional as a web server & you can see a bunch of tutorials out in the wild on how to do it. Almost all of it is about turning your RPi into a LAMP setup. RPi can handle it since it has got more juice than your wireless router running the same LAMP setup. The catch is that, when the number of simultaneous users increase, your sever will start to slow down. This is because of the limited resources on the Pi & the Apache Server creates new threads for each of the users/requests – running out the memory in no time! But, nobody is gonna setup an RPi as a server for real(although there are many proof of concepts out there) & a LAMP setup will more than enough for running a connected temperature sensor or a home automation system.

Now comes NodeJS! Nodejs is unlike any other web servers out here, in fact it isn’t a webserver! Its much more than that. NodeJS can be used to create pretty much anything, from web servers to automation systems to real time web apps. Its super lightweight & can be installed on the RPi easily. NodeJS can be combined with the Express module to create a static file server or you can install SocketIO to create realtime apps. RPi won’t make a very high performance NodeJS server, but you’ll be more than happy with the performance. The best part is that, you can install Node GPIO module & can control the GPIO pins directly from your web server.

How to?

SSH into your RPi, I believe this part doesn’t need any explanation!

If you’re on Mac:

ssh rpi.static.address -l pi

You will be asked for your password & if you haven’t changed it, the password will be ‘rapsberry’. Type it in & hit enter.

If you’re on Windows, open up Putty & enter the static IP address of your pi & click open

Putty Configuration


Enter Pi & raspberry as user name & password when asked & you’re in!

SSH Login Window


Once you’re in, type in the following code or copy-paste it & hit enter. If you’re on Putty, simply copy it from here & right click inside the Terminal window & the code will be pasted there.


What this does is that, it will download the package node-v0.10.2-linux-arm-pi.tar.gz from the node website. Wget is used to download files from web servers. NodeJS website stores a precompiled NodeJS distribution for RPi, meaning that, we don’t have to perform the geeky wizardry of compiling it from scratch.

Once the file has been downloaded, the terminal will return to the prompt. Copy-paste or type in the following just like you did before:

tar -xvzf node-v0.10.2-linux-arm-pi.tar.gz

You should see a long list of files. This simply unpacks the downloaded file, which is similar to a ZIP file extraction, with the exception that the file type ie .gz.  The extracted files will be put inside the folder “node-v0.10.2-linux-arm-pi”. Now, technically node has been installed on your system. If you run the following code, you’ll see that the terminal will spit out the NodeJS version we just installed. ie, v0.10.2

node-v0.10.2-linux-arm-pi/bin/node --version

If you don’t see the version, probably you messed up something & I recommend you do everything again.

NodeJS & NPM has been installed on your RPi now. But it isn’t useless unless it is available to you from every prompt. To do that, you’ll have to add the installation path of NodeJS to your environment variables. To add it to your ENV, simply run the code below like you did before:

echo "export PATH=$PATH:/home/pi/node-v0.10.2-linux-arm-pi/bin" >> ~/.bashrc

This will add the node installation path to your ENV so that you’ll be able to access it from your terminal. Now, reboot your Pi, simply run the below code to reboot:

sudo shutdown -r now

This will disconnect your SSH connection. You will have to reconnect it after the RPi restarts. Now, test your node installation. If you type in “node” at your terminal, you’ll see the node’s prompt “>”. Test it out by running a simple HTTP server.  Type in the followin in terminal

nano test.js

Nano will be opened in terminal. Copy-paste the following code into Nano & press Ctrl+O then Ctrl+X to exit Nano:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World from NodeJS on RPI!\n');
}).listen(1337, '');
console.log('Server running at');

Now run  this:

node test.js

You’ll see “server running at” rightaway. Now, open up your browser & type in the address of your RPi on the addressbar with the port number 1337 like this:

You’ll see “Hello world from NodeJS running on RPi”. Thats pretty much it! NodeJS is up & running on your RPi. Now, if you’re planning to build native modules on the RPi, you’ll have to install the Node-GYP, which is the build tool of Node. In that case, run the following on your terminal:

npm install -g node-gyp

You’ll need GCC installed,it comes preinstalled on Raspbian.

Thats it! Completely working NodeJS installation on your RPi!