Javascript on a Raspberry Pi – How to install Node.JS

 

Why install Node.JS on a Raspberry Pi ? 

Nodejs_logo_lightI’ve been using Node.JS as the backend framework for building single-page web apps recently. On top of providing the advantage of an asynchronous, event-based programming model on the backend, it means I can code in Javascript on both the frontend and the backend again – just like in the good old days coding client-server applications in C.

Raspi-PGB001-300x267

And so when I come up with an application where I want to use my Raspberry Pi as a micro web server, but one that needs more than the ability to serve static webpages, I  right away think of Node.

How to install Node.JS on your Raspbery Pi 

Installing node on the RPi is not difficult, and no longer requires patches to the standard source or even a local compile, as it did the first time i went through the exercise.
I was first using the Debian Wheezy image on a v2 RPi Model B, and the installation was straightforward. The Raspberry Pi’s CPU is based on ARMv6, and there is a compiled release on nodejs.org especially for it – I first used v0.8.21.

You now have 2 options for installing NodeJS, both described below. They are

  • 1. install the latest pre-built NodeJS binary for the Pi, or
  • 2. download the latest NodeJS source code and compile it yourself

Preferred method – install the latest pre-built binary

1. Start by getting the URL to the pre-built binaries you want to install. The latest version can be found at http://nodejs.org/download/
Click “Other release files” and get the URL of the packages named

  • node-v0.8.XXlinux-arm-pi.tar.gz,

where XX is the latest stable version number.

(The reason the Pi binary you need is identified as “linux-arm-pi” is because Linux is the Operating System on your Pi, and the Pi has a CPU made by ARM.)

The latest Release for the Pi that I can currently find is V0.10.26 and is here http://nodejs.org/dist/v0.10.26/node-v0.10.26-linux-arm-pi.tar.gz

2. You will need to install Node using the command line interface on your Raspberry Pi, so either ssh in to the Raspberry Pi from your Mac or PC as described here, or just open a terminal window if you’re using the Debian GUI desktop.

3. Download the Node tar file you identified above using wget, then go to the /user/local directory and unpack the tar file there, as shown in the following steps:


$wget http://nodejs.org/dist/v0.8.21/node-v0.8.21-linux-arm-pi.tar.gz
$cd /usr/local
$sudo tar xzvf ~/node-v0.8.21-linux-arm-pi.tar.gz --strip=1

That’s as easy as it is; you should now have a working Node.JS install !
Verify this by typing:


$ node –v

which should display the Node version number, and


$ npm -v

should show you the version of the Node Package Manager.

An alternative approach: Downloading the latest NodeJS source and compiling it locally

The latest version of NodeJS is v0.12 but they seem to have stopped making compiled versions of NodeJS available on nodejs.org since v0.10. So if you want that latest version on your Pi, you’ll have to comply it yourself.

With the Raspberry Pi 2’s 4-core CPU, its also now practical to download the latest Node source and compile it on the Pi itself like this:

First, check out the latest source code version: http://nodejs.org/dist/

As of now, this would be v0.12.2. So open a SSH session to your Raspberry Pi and use the following commands:


$ wget http://nodejs.org/dist/v0.12.2/node-v0.12.2.tar.gz
$ tar xvzf node-v0.12.2.tar.gz
$ cd node-v0.12.2

This will download and extract the latest sources, and you will end up in the source folder.
Next compile it. It will take time. (The parameter “-j 4″ is useful because the Raspberry Pi 2 ARM CPU comes with 4 cores, and here’s a good time to put them to work for you! It shouldn’t take longer than 30-40min with all cores.)


$ ./configure
$ make -j 4

After the compiling process, use the following command to install the binaries:


$ sudo make install

The install location will be /usr/local/bin, so you can use node/npm from any directory after that.

Verify your NodeJS installation

Check the version of node and npm you have installed:


$ node -v
v0.12.2
$ npm -v
2.7.4

Now start up node and check it is working, using the REPL (Node’s Javascript Read-Eval-Print Loop environment):


$ node
> 1 + 1
2
> "hello " + "world"
hello world
>

 

 

Now you’re ready to start coding in Javascript on your Raspberry Pi, and to use npm to download and install any other modules you need to build your web application. I will be using express, connect, socket.io and mongoose among other packages.

A working Node.JS example on the RPi

A “Hello World” Web Server

First, note down the IP address of your Raspberry Pi within your local network by executing :


$ ifconfig

In my own network, I’ve configured my Router to reserve the fixed IP address 192.168.0.22 for my Pi (identified by its MAC hardware address), rather than have a different address dynamically assigned by the Router’s DHCP service each time the Pi starts up. Check the instructions for your Router if you’re not sure how to do this.

Now create a file called helloworld.js and copy the code below for a simple web server in server-side Javascript, which in tribute to Dennis Ritchie simply responds to every incoming HTTP request on port 8000 with the text “Hello World!“.
Be sure to use the IP address of your Pi which you noted down earlier.


var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World!\n');
}).listen(8000,'192.168.0.22');

console.log('Server running at http://192.168.0.22:8000/');

Start up your  RPi Web Server

To start up your new web server, execute your application using node:


$ node helloworld.js
Server running at http://192.168.0.22:8000/
$

The web server is now running continuously in the background.

Finally, using another machine on the same local network, open a web browser and navigate to the URL of your webserver:

http://192.168.0.22:8000

If you’re in luck, the text “Hello World!” should appear in your browser window !

What’s Next ?

In our next exercise, we will turn this simple webserver into a web application server providing a RESTful web API. That will open up a world of possibilities for interactions between our RPi’s environment and the rest of the world, via the internet. Next – How to build a RESTful API on a Raspberry Pi

I hope this recipe has been useful. If you have any feedback or spot any errors or omissions, feel free to leave a note in the comments section below.

13 comments

  1. Florian W.

    Thank you very much for this REALLY simple way to install nodejs on my Raspberry Pi…
    I tried to do this for about the last 3 hours and the most tutorials had about 10-20 lines of shell “code” and at the end did not work at all or just partly.. but your solution seems to be just perfect 😉

  2. mike

    I get a runtime complaint about the line in helloworld.js that contains ‘text/plain’. “text” is underscored as ‘syntax error: unexpected identifier.

  3. mike

    Actually, I had a typo in that line. But when I run the script, I dont get a prompt back. The script is running because I can pull up the page on another machine and it works as expected. But my SSH command line is stuck. If I ctl-c out to a prompt the script stops.

  4. Doug Thomason

    When I run which on node, it shows it at: /home/pi/bin/node
    I am almost sure I changed directory to /usr/local/ before running sudo tar.
    I have also installed express and npm for the RESTful tutorial. They are also located at /home/pi/bin/~. Anything wrong here?

  5. Doug Thomason

    Duh, pat me on the head. I didn’t unpack the tar where you told us to.

    Some other tutorials suggest to use /opt/node for install location. Would there be a difference with regard to access privileges between these?

  6. http://Www.slideshare.Net/theonejosephpantelv

    Hi terrific blog! Does running a blog like this require a lot of work?
    I’ve very little expertise in programming however I was
    hoping to start my own blog soon. Anyhow, if you
    have any suggestions or tips for new blog owners please
    share. I know this is off topic however I simply wanted to ask.
    Thanks a lot!

  7. James

    Hello! Thanks for the info! Just a quick update: it looks like after V0.8.22 the linux-arm-pi version is no longer continued. Is this a problem? I see they are on version 10.xx already, so I am wondering if they just quit compiling a version for the Pi because they had everything worked out or if a newer/better thing came along?

  8. Austin

    Is there something needed to enable incoming connections? I can only reach my node server from the rpi itself. On the same network from another machine, the request never makes it to my node server.
    I can ping my rpi from the network and I can access other node servers from my network as well as the internet on the rpi no problem though.

  9. Andres BURBANO

    Hi I install the v 0.10 and when I create the helloworld I got this erro: pi@raspberrypi /usr/local $ node helloworld.js
    Server running at http://192.168.4.31:8000/

    events.js:72
    throw er; // Unhandled ‘error’ event
    ^
    Error: listen EADDRNOTAVAIL
    at errnoException (net.js:904:11)
    at Server._listen2 (net.js:1023:19)
    at listen (net.js:1064:10)
    at net.js:1146:9
    at dns.js:72:18
    at process._tickCallback (node.js:419:13)
    at Function.Module.runMain (module.js:499:11)
    at startup (node.js:119:16)
    at node.js:906:3

  10. burningplain

    With the Pi 2 you can also download the latest Node source and compile it on the Pi like this:
    check out the latest version: http://nodejs.org/dist/
    As of now, this would be v0.12.2, so open a SSH session to your Raspberry Pi and use the following commands:
    wget http://nodejs.org/dist/v0.12.2/node-v0.12.2.tar.gz
    tar xvzf node-v0.12.2.tar.gz
    cd node-v0.12.2

    This will download and extract the latest sources, and you will end up in the source folder.
    Next compile it. It will take time:
    ./configure
    make -j 4

    (The parameter “-j 4″ is useful because the Raspberry Pi 2 ARM CPU comes with 4 cores, and that lets make use them all! It should not take longer than 30-40min with all cores.

    After the compiling process, use the following command to install the binaries:
    sudo make install

    The install location will be /usr/local/bin, so you can use node/npm from any directory after that.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s