Raspberry Pi and Node.JS: Basic Setup

Finally I had some time to play around with my Raspberry Pi. The primary goal was to have a basic setup for Node.JS 0.10.x with Raspbian “wheezy” (Hard-float) on a Raspberry Pi.

I run in some troubles to install Node.JS on Raspbian soft-float image and also when you wanted to compile it for yourself you need some time. Luckily there is an ARM binary package for Raspberry Pi (See ‘other release files’ on Node.JS download section).

For this tutorial you need:

  • Raspberry Pi
  • SD card at least 4GB
  • HDMI to DVI Converter – to plug the Raspberry Pi to your monitor
  • micro usb charger – for the power supply
  • Network cable and internet connection
  • Node.JS 0.10.x ARM binary package
  • Raspbian ‘wheezy’ hard-float image (2013-02-09)

Note: We will use Node.JS 0.10.2 in this tutorial because the ARM binary package for 0.10.3 was not yet available. But I think when the new ARM package is available this tutorial should also work with the new minor release.

20130328_175044

Install Raspbian on your SD Card

Download Raspian “wheezy” (hard-float, 2013-02-09-wheezy-raspbian) and install it on your SD card. There are plenty of tutorials which shows you how to install Raspbian on your SD card. Have a look at the following tutorials how you can install Raspbian on your SD card.

Note: You have to use the Raspbian hard-float image. With the soft-float image the Node.JS ARM binary will not work.

Configure your Raspberry Pi

Plug in your usb micro charger and Rasberry Pi will boot up and display the
raspi-config dialog.

raspi-config

These are the configuration steps you should do with ‘raspi-config’ after the first time you start your Raspberry Pi:

  • expand_rootfs – select expand_rootfs to expand the root partition to fill out the whole SD card
  • memory_split – we reduce the GPU to the minimum (16mb), because we will not start intensive graphical tasks.
  • change_pass – change password for ‘pi’ user. The user ‘pi’ is the preconfigured user in Raspbian
  • ssh – enable the ssh server, so that we can use ssh to log in from remote.
  • You should also adapt the keyboard, timezone and locale to your needs. When you are finished select finish to end the configuration.

After the configuration you need to restart your system, so that the resizing of the root partition and memory split will work.

sudo reboot

You can always change your settings again with ‘raspi-config’.

sudo raspi-config

After the restart, login in with the user ‘pi’ and the new password you have choosen. Now we need to update our system with ‘apt-get’ our package manager.

sudo apt-get update
sudo apt-get upgrade

With no further network changes your Raspberry Pi will use DHCP to get a dynamic IP adress. So we are going to change this to a fixed IP adress. Because we will use our system as Node.JS server and this is quite simpler with a fixed IP adress. You can also work with a dynamic IP adress and just skip the following step.

To change the system to a fixed IP adress just open the interface configurartion file. For that we use the preinstalled editor ‘nano’.

sudo nano /etc/networks/interfaces

Replace the following lines for your network device eth0 and just adapt the settings to your network (e.g. IP, gateway, dns, etc.) configuration.

iface eth0 inet static
address 10.0.0.40
netmask 255.255.255.0
gateway 10.0.0.1
nameserver 10.0.0.1

After the changes in the network configuration file just stop and start your network device with ‘ifdown’ and ‘ifup’.

sudo ifdown eth0
sudo ifup eth0

Yo can try now from a remote system to connect with the user ‘pi’ over SSH to our Raspberry Pi.

ssh pi@10.0.0.40

Installing Node.JS

We will download the ARM binary package from Node.JS and create the directory /opt/node where we want Node.JS installed. We will not place Node.JS in ‘/usr/local’, instead we use a separate directory for Node.JS, this way it is much easier to update a manually installed package.

sudo mkdir /opt/node

The next step is to dowload the Node.JS ARM binary package, unpack it and copy the content to our node directory /opt/node.

wget http://nodejs.org/dist/v0.10.2/node-v0.10.2-linux-arm-pi.tar.gz
tar xvzf node-v0.10.2-linux-arm-pi.tar.gz
sudo cp -r node-v0.10.2-linux-arm-pi/* /opt/node

Finally we have to add Node.JS to our path variable. For that you have to edit the ‘/etc/profile’ configuration file.

nano /etc/profile

Add the following lines to the configuration file before the ‘export’ command.

...
NODE_JS_HOME="/opt/node"
PATH="$PATH:$NODE_JS_HOME/bin"
export PATH
...

Yo need now to logout and log in again, so that the changed path variable will work.

logout

Configure Node.JS

We need now a start script for Node.JS. Our script will start the Node.JS with the user ‘pi’ and look for a server.js file in the /home/app directory. The output stream from Node.JS will be stored in the file nodejs.log. Just create the file nodejs.sh in your current directory with the following content:

#!/bin/bash

NODE=/opt/node/bin/node
SERVER_JS_FILE=/home/pi/app/server.js
USER=pi
OUT=/home/pi/nodejs.log

case "$1" in

start)
	echo "starting node: $NODE $SERVER_JS_FILE"
	sudo -u $USER $NODE $SERVER_JS_FILE > $OUT 2>$OUT &
	;;

stop)
	killall $NODE
	;;

*)
	echo "usage: $0 (start|stop)"
esac

exit 0

Note: When yo want to use a privileged port (TCP/IP port numbers below 1024) you have to start the script as root.

Make the script executable with ‘chmod’ and copy it to ‘/etc.init.d’. The last step is to register the script as service with ‘update-rc.d’. Then our Node.JS server will automatically start up when the Raspberry Pi is powered on.

chmod 755 nodejs.sh
sudo cp nodejs.sh /etc/init.d
sudo update-rc.d nodejs.sh defaults

The last step is to create a Node.JS server.js file in our /home/pi/app directory. The directory ‘/home/pi/app’ will be our Node.JS project directory.

mkdir /home/pi/app

The ‘server.js’ file is just a simple Node.JS server which will listen on port 8080 and display ‘Hello World’ and write some output to the console. Just place the following content as ‘server.js’ file in directory ‘/home/pi/app’.

var http = require('http');

http.createServer(function(req,resp) {
	resp.writeHead(200, {"Content-Type": "text/plain"});
	resp.write("Hello World");
	resp.end();
	
	console.log("sample output to console");

}).listen(8080);

Start Node.JS

It’s time to start Node.JS with our start script and see if it all fits together.

sudo /etc/init.d/nodejs.sh start

Open a browser with the url http://10.0.0.40:8080. You should now see a page which displays ‘Hello World’ and the output from the console.log() statement in the /home/pi/node.log file.

To stop Node.JS just enter

sudo /etc/init.d/nodejs.sh stop

When you want to use ‘npm’ or ‘node’ as root with sudo you have to add the ‘-i’ option, so that the root user’s environment is acquired. For example when you want to install the package ‘forever’ globally with ‘npm’.

sudo -i npm install forever -g

Update Node.JS to a new version

To update Node.JS to a new version just follow the next steps. Also check first if there is an ARM package available.

delete the old version

# sudo rm /opt/node -r

create new directory

# sudo mkdir /opt/node

unpack the new version and copy the content to our node.js directory. Replace ‘x’ with the new minor version.

# tar xvzf node-v0.10.x-linux-arm-pi.tar.gz
# sudo cp -r node-v0.10.x-linux-arm-pi/* /opt/node

Check current version

# node -v
v0.10.2

I hope this tutorial was useful for you and will give you some quick start to play around with Node.JS and Raspberry Pi.