Tuesday, 17 July 2012

Compiling node.js on the Raspberry Pi

I see a few people have already complied node.js on the Raspberry Pi already and so these instructions are somewhat of a repeat but here goes anyway. I've included various links below, but as you'll see we've all the same experiences more or less but it is good to see commonality.

Disclaimer: you're on your own from here...I take no responsibility for data loss, your Pi exploding or anything else.

Here's my configuration:

$uname -a
Linux raspberrypi 3.1.9+ #90 Wed Apr 18 18:23:05 BST 2012 armv6l GNU/Linux
$ cat /etc/debian_version
$ gcc -v
Using built-in specs.
Target: arm-linux-gnueabi
Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.5-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --disable-sjlj-exceptions --enable-checking=release --build=arm-linux-gnueabi --host=arm-linux-gnueabi --target=arm-linux-gnueabi
Thread model: posix
gcc version 4.4.5 (Debian 4.4.5-8)

There are reports of the Pi running out of memory, so you can change the distribution between the CPU and GPU memory allocation by using a different kernel.

NB: the $ symbol is the command prompt I'm using under bash!

$sudo cp /boot/arm224_start.elf /boot/start.elf

This is explained more here: Changing RaspberryPi RAM CPU:GPU Ratio

Then I downloaded libssl-dev, after running apt-get update a couple(!!) of times.

$sudo apt-get update
$sudo apt-get update
$sudo apt-get install libssl-dev

Download and extract node.js:

$wget http://nodejs.org/dist/v0.8.2/node-v0.8.2.tar.gz
$tar xvf node-v0.8.2.tar.gz
$cd node-v0.8.2

Set the compilation flags:

$export CCFLAGS='-march=armv6'
$export CXXFLAGS='-march=armv6'
$export GYP_DEFINES='armv7=0'

Edit one of the node.js configuration files with vi

$vi deps/v8/SConstruct

Aside: use vi as emacs is now too big for any known Earth based computer - rumour has it that the cloud was only invented to gather together enough resources just to even start emacs, let alone do anything useful...(note to emacs users, this is humour!! ;-)

The lines you're looking for are found around lines 82-90 and look like this:

'all': {
    'CXXFLAGS': ['-fno-rtti', '-fno-exceptions'],

change these by adding the -march=armv6 options so they look like this:

'all': {
   'CCFLAGS': ['$DIALECTFLAGS', '$WARNINGFLAGS', '-march=armv6'],
   'CXXFLAGS': ['-fno-rtti', '-fno-exceptions', '-march=armv6'],

Then somewhere around lines 170-175 you'll find:

'armeabi:softfp' : {
   'vfp3:on': {
   'simulator:none': {
      'CCFLAGS': ['-mfloat-abi=softfp'],

You'll need to comment out the 'vfp3' and 'simulator' sections AND remove the trailing comma at the end of the 'CPPDEFINES' line so that it now looks like this:

'armeabi:softfp' : {
   # 'vfp3:on': {
   # },
   # 'simulator:none': {
   #  'CCFLAGS': ['-mfloat-abi=softfp'],
   # }

NB: remember that trailing comma!

And start the compilation:


Note the use of the -O2 option, aparently this is to get around some compiler bug which seems to result in the following errors:

make[1]: Entering directory `/home/pi/node-v0.8.2/out'
  ACTION _home_pi_node_v0_8_2_deps_v8_tools_gyp_v8_gyp_v8_snapshot_target_run_mksnapshot /home/pi/node-v0.8.2/out/Release/obj.target/v8_snapshot/geni/snapshot.cc
pure virtual method called
terminate called without an active exception
make[1]: *** [/home/pi/node-v0.8.2/out/Release/obj.target/v8_snapshot/geni/snapshot.cc] Error 134
make[1]: Leaving directory `/home/pi/node-v0.8.2/out'
make: *** [node] Error 2

First compilation took about 2 hours 20 minutes which then ended with the above problem. I've a 4Gb SanDisk Extreme SDHC card which might be a bit of an I/O bottleneck - some people have reported much faster compilation times. Anyway, successful compilation and linking took about 2 hours 40:

$ ls -l out/Release/node
-rwxr-xr-x 1 pi pi 9475651 Jul 17 20:10 out/Release/node

and running a simple server (found via Felix's node.js beginner's guide) :


  1. Tom Gallacher's page on Node.js
  2. doowttam's posting on the R-Pi forums about nodejs 0.8.1
  3. Elsmorian's Nodejs on Raspberry Pi
  4. Cannot compile node.js 0.8.2 under CentOS 6.2 - compiler optimisation errors: use -O2 option for CFLAGS and CXXFLAGS
  5. Node.js compile fails in Amazon AMI x64 - snapshot.cc - pure functional method call - more on the compiler optimisation flags.
  6. Issue 2093: fix "pure virtual method called" error with gcc 4.4.6 + -fno-strict-aliasing (original link)

No comments: