Computing stuff tied to the physical world

Node.js on Raspberry Pi

In Software on Jan 6, 2013 at 00:01

After all this fiddling with Node.js on my Mac, it’s time to see how it works out on the Raspberry Pi. This is a running summary of how to get a fresh setup with Node.js going.


I’m using the Raspbian build from Dec 16, called “2012-12-16-wheezy-raspbian”. See the download page and directory for the ZIP file I used.

The next step is to get this image onto an SD memory card. I used a 4 GB card, of which over half will be available once everything has been installed. Plenty!

Good instructions for this can be found at – in my case the section titled Copying an image to the SD card in Mac OS X (command line). With a minor tweak on Mac OSX 10.8.2, as I had to use the following command in step 10:

sudo dd bs=1m if=~/Downloads/2012-12-16-wheezy-raspbian.img of=/dev/rdisk1

First boot

Next: insert the SD card and power up the RPi! Luckily, the setup comes with SSH enabled out of the box, so only an Ethernet cable and 5V power with USB-micro plug are needed.

When launched and logged in (user “pi”, password “raspberry” – change it!), it will say:

Please run 'sudo raspi-config'

So I went through all the settings one by one, overclocking set to “medium”, only 16 MB assigned to video, and booting without graphical display. Don’t forget to exit the utility via the “Finish” button, and then restart using:

$ sudo shutdown -r now

Now is a good time to perform all pending updates and clean up:

$ sudo -i
# apt-get update
# apt-get upgrade
# apt-get clean
# reboot

The reboot at the end helps make sure that everything really works as expected.

Up and running

That’s it, the system is now ready for use. Some info about my 256 MB RPi:

$ uname -a
Linux raspberrypi 3.2.27+ #250 PREEMPT \
                    Thu Oct 18 19:03:02 BST 2012 armv6l GNU/Linux
$ free
             total       used       free     shared    buffers     cached
Mem:        237868      51784     186084          0       8904      25972
-/+ buffers/cache:      16908     220960
Swap:       102396          0     102396
$ df -H
Filesystem      Size  Used Avail Use% Mounted on
rootfs          3.9G  1.6G  2.2G  42% /
/dev/root       3.9G  1.6G  2.2G  42% /
devtmpfs        122M     0  122M   0% /dev
tmpfs            25M  213k   25M   1% /run
tmpfs           5.3M     0  5.3M   0% /run/lock
tmpfs            49M     0   49M   0% /run/shm
/dev/mmcblk0p1   59M   18M   42M  30% /boot
$ cat /proc/cpuinfo 
Processor       : ARMv6-compatible processor rev 7 (v6l)
BogoMIPS        : 697.95
Features        : swp half thumb fastmult vfp edsp java tls 
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xb76
CPU revision    : 7

Hardware        : BCM2708
Revision        : 0004
Serial          : 00000000596372ab

So far, it’s just a standard boilerplate setup. Yawn…


On to Node.js! Unfortunately, the build included in Debian/Raspbian is 0.6.19, which is a bit old. I’d rather get started with the 0.8.x series, so here’s how to build it from source.

But first, let’s use this simple trick to get write permission in /usr/local as non-root:

$ sudo usermod -aG staff pi

Note: you have to logout and back in, or reboot, to get the new permissions.

With that out of the way, code can be built and installed as user “pi” – no need for sudo:

$ curl | tar xz 
$ cd node-v0.8.16
$ ./configure
$ make
(... two hours of build output gibberish ...)
$ make install

That’s it. A quick check that everything is in working order:

$ node -v
$ npm -v
$ which node
$ ldd `which node`
  /usr/lib/arm-linux-gnueabihf/ (0x40236000) => /lib/arm-linux-gnueabihf/ (0x40074000) => /lib/arm-linux-gnueabihf/ (0x40036000) => /usr/lib/arm-linux-gnueabihf/ (0x40107000) => /lib/arm-linux-gnueabihf/ (0x4023f000) => /lib/arm-linux-gnueabihf/ (0x4007f000) => /lib/arm-linux-gnueabihf/ (0x400a7000) => /lib/arm-linux-gnueabihf/ (0x402b0000)
  /lib/ (0x400d2000)

Oh, one more thing – the RPi doesn’t come with “git” installed, so let’s fix that right now:

$ sudo apt-get install git

There. Now you’re ready to start cookin’ with node and npm on the RPi. Onwards!

PS. If you don’t want to wait two hours, you can download my build, unpack with “tar xfz node-v0.8.16-rpi.tgz”, and do a “cd node-v0.8.16 && make install” to copy the build results into /usr/local/ (probably only works if you have exactly the same Raspbian image).

  1. Looks like it’s time to power up my RPi again…

    One small note, don’t forget to update the firmware to the newest release. It solved some reliability issues I had with my SD card and also improved performance. As you may know the firmware is just a file on the SD card, so not a big risk of doing an upgrade.

    There is a nice little tool:

  2. Most interesting to read about the installation – looking forward to hearing how Node.js will perform on the little PC. Builds are slow, as you comment, and seem to be mostly CPU-limited.

    BTW: my own notes on making a stratum-1 NTP server for the Raspberry Pi are here:

    • RPi is about 1/20th the speed of my laptop. Fascinating notes about your NTP server, thanks.

  3. Giving yourself write access to /usr/local is not entirely sensible – it does undermine the security of the system a bit meaning that if anything does go wrong it might be harder to clean up. I’d much rather just use “sudo make install” instead – the earlier stages can be run without privileges.

    While talking of such things, if you’re going to list the other routine setup operations it’s probably also worth mentioning changing the default password first time through.

    • I don’t see how allowing the “pi” user write access to /usr/local can undermine security, over regularly doing sudo and getting far more dangerous full root access on a regular basis. All you end up with is “untrusted” code in /usr/local, which can however never affect anything requiring root privileges. I actually think this is the intended way for /usr/local to be used (it has all the group modes and “sticky” permission bits set up for it).

      Note that I’m not changing any security aspects of the stock RPi install, i.e. the /usr/local permissions remain unchanged.

      Good point about changing the password. I’ve added a note.

  4. Note: you can also safely overclock your Raspberry Pi somewhat. Makes it a little faster. I compiled and installed node.js a week or so ago and it’s working fine.

    To make your Raspberry go faster, safely, edit /boot/config.txt and set arm_freq=900.

    For more details check:

  5. I have a few Pi’s both 256/512, you can use /boot/config.txt and the gpu_mem_256= and gpu_mem_512= params to set different mem splits for each so the card can be used in both models, then comment out your gpu_mem param.

    Also from the trenches: node_gyp is somewhat dumb and can spit out splurious error messages when compiling modules, I had a weird error with last week only the realise I was low on disk space on a stock SD (I had not expanded the f/s) – wasted 2hrs scratching my head at a stupid permissions error – doh!

  6. @Jwc I did not find “moduser” linux command. Do you mean “usermod” instead of “moduser” about “$ sudo moduser -aG staff pi”? Thx

  7. The “shutdown -r now” feels a bit cryptic. Does rpi have the “reboot” command ? AFAIK this will do the same, and is readable. I.e. % sudo reboot

  8. we can cross-compile node-v0.8.x for Pi.

  9. If I download your .tgz file (twice now the build has failed on my raspi) and I’m using the same raspi wheezy image, does make install unzip the package I downloaded, or does that requre unzipping first? and where do I locate the unzipped files before running make install?

    • Ah, good point. You unpack manually wherever you want (tar xfz node-v0.8.16-rpi.tgz), change into the resulting directory (cd node-v0.8.16), and then do “make install”. It’s a shortcut for doing only the last step of the whole process. Once installed, you can then delete the tgz file and that directory again.

  10. Thanks! It worked great. Saved me oodles of time and frustration.

  11. I haven’t yet updated for 0.8.16 but binaries for 0.8.15 are at plus instructions to cross compile yourself.

  12. @jcw I had to reinstall after corrupting an SD card. (finding this easy to do on my pi) So starting all over again with the 12/12 rasbian version… I copied over the node directory. Unzipped and unarchived. just the straight files. In the shell I cd into that directory. I run ‘sudo make install’ and it all seems to complete. Files are all placed in /usr/local/bin/ and /usr/local/lib/. But I cannot issue any node or npm command. If use sudo just get ‘command not found’ No matter what directory I’m in. sudo su does not help. I thought the ‘make install’ set up all the dirs and permissions if I had copied over the complete node directory and files. What did I miss.

  13. I began to think it was permissions related. So I did chmod 777 -R to the Node directory I had copied over and I ran make install again. Resolved the problem but feels like I used a hammer.

Comments are closed.