The programming language landscape is always changing, and in my "day job" as a software developer I use a wide range of languages. Over the last year or so I can remember working with Awk, C, C++, Java, JavaScript, Ruby, shell script and SQL. And that's not counting all the specialist configuration, scripting and data languages. Although there is still a lot of jobs looking after enterprise systems written in the likes of C, C++ and Java, I am also seeing more and more development being done in more dynamic languages. In particular, the rising star seems to be node.js.
If you are not familiar with node.js I recommend giving it a look. It's JavaScript, but running on a server rather than in a browser. It's free, runs on most systems, easy to work with, surprisingly fast, and has a wide array of third-party software available though its npm package management system.
Running such software on a generic server is all well and good but, naturally, I want to be able to build systems using node.js and Raspberry Pi. I looked around and found an article from Josh On Design which got me part of the way, but it did seem a bit vague about a couple of the steps, so I thought I'd describe how I got things set up for a project I'm currently working on.
As always, the first step is to get a Raspbian image on an SD card. I don't think node.js is particularly picky, and should run on any reasonably recent version. For this project I started with a clean SD card with the latest Raspbian version (2014-01-07 at the time of writing). To install this software you will need a command line. I prefer to connect to the Raspberry Pi over ssh (using my favourite tool MobaXterm), but if you want to use a keyboard and screen attached to the Raspberry Pi you will need to open a terminal window.
If you are concerned that your Raspbian might be getting a bit old, you may want to refresh it using:
sudo apt-get update
sudo apt-get upgrade
This is usually a good idea, but if you have specific versions of other software installed, or are just in a hurry, you can skip to the next step.
As Josh points out, compiling a modern programming language and toolset from scratch takes a long time on a little Raspberry Pi, so the node.js team are now providing downloadable versions pre-built for the Pi. Unfortunately, they don't seem to build these for every released version, so you can't always just grab the latest. All the downloads are available from http://nodejs.org/dist/, and I found I needed to start with latest
and work backwards through the versions until I found one with linux-arm-pi
in its name. At the time of writing, this is version 0.10.25. Once you have found a recent Raspberry Pi build, you need to download it and install it on the Pi. I decided to install it to /opt/node
as follows. Of course, if you have chosen a different version, you will need to change the names.
sudo su -
cd /opt
wget http://nodejs.org/dist/v0.10.25/node-v0.10.25-linux-arm-pi.tar.gz
tar xvzf node-v0.10.25-linux-arm-pi.tar.gz
ln -s node-v0.10.25-linux-arm-pi node
chmod a+rw /opt/node/lib/node_modules
chmod a+rw /opt/node/bin
echo 'PATH=$PATH:/opt/node/bin' > /etc/profile.d/node.sh
^D
The ^D at the end indicates pressing Control-D to exit the root shell and return to your regular user._
Once you have done this installation, either logout and log back in, or open a new terminal session and enter node --version
which should show the version you have installed. You can also try npm --version
, which will tell you the version of npm. Now, whenever you start the system, you will have access to node and npm.
One final step is to install node-gyp. This is not strictly necessary in every case, but you will find it useful if you ever need to build and install any "native" npm modules.
npm install -g node-gyp
That should be enough for now. I will cover some useful things to do with node.js in a future article.