Jeremy Morgan

My Blog about Programming, Tech, SEO, Marketing and whatever else I come up with.

How to Install Node.js on Your Raspberry Pi


Author:


Do you want to install Node.js on your Raspberry Pi? It’s actually quite a bit easier than it used to be. I just recently installed Node.js 0.8.16 on my Raspberry Pi and decided to document it. For this tutorial I’m using a setup with the latest Raspian image.


Do you like my Rasbperry Pi Tutorials?

I’m constantly hacking on stuff with the Raspberry Pi. If you want to get notified every time I post a new article subscribe to my Raspberry Pi feed here, or you can Sign up for the Raspberry Pi Newsletter spam free!

Installation of Node.Js 0.8.16

To do this, you’ll need to build Node.js from source. It’s not as bad as it sounds, and actually pretty easy. We’ll be getting the source from the downloads page at Nodejs.org and compiling it from there.

For this tutorial I’m using the Raspian “Wheezy” Image but since we’re compiling from source it shouldn’t matter too much what version you’re using.

You’ll want to go into your /usr/src directory (or some temporary directory) and use wget to download the file.

1
2
cd /usr/src
sudo wget http://nodejs.org/dist/v0.8.16/node-v0.8.16.tar.gz

This will pull down the source tarball to your machine. You must have your Raspberry Pi connected to the internet, if you don’t then download the file here and put it on your Pi.

1
2
sudo tar xvzf node-v0.8.16.tar.gz
cd node-v0.8.16

Next steps will build the program:

1
2
3
sudo ./configure
sudo make
sudo make install

Note: The make process can take quite a while, well over an hour in most cases. You may want to start the build and do something else, or if you’re in a hurry you can overclock your Pi to make it build a little faster.

Testing your Node.js Install

Once it’s built, if you don’t see any errors, it should be ready to go. You can run some tests with the following command:

1
sudo make test

Not all of the tests will pass, but most of them should. For a quick web test, put the following code in a file called test.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Load the http module to create an http server.
var http = require('http');

// Configure our HTTP server to respond with Hello World to all requests.
  var server = http.createServer(function (request, response) {
  response.writeHead(200, { "Content-Type": "text/plain" });
  response.end("Hello World\n");
});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");

And save it. Then run the following command:

1
sudo node test.js

And then point your web browser to http://127.0.0.1:8000 and you will see a hello world! message.

Summary

I hope this helps folks wanting to try out node on their Raspberry Pi. In the past it was a bit tricky and for some people compiling from source can scary. But as you can see this is a pretty straightforward process.

While it may not be the best node server to use it’s great for a development environment, as most hosting companies do not offer Node.js yet. The Raspberry Pi is designed for learning and experimenting and provides a great easy platform to do that.

So get out there and start building some node apps with non-blocking goodness!


Do you like my Rasbperry Pi Tutorials?

I’m constantly hacking on stuff with the Raspberry Pi. If you want to get notified every time I post a new article subscribe to my Raspberry Pi feed here, or you can Sign up for the Raspberry Pi Newsletter spam free!

Comments