Cross-compiling Node.js for ARM6 (Raspberry Pi)

13,714

Solution 1

I've been working on this a bit since the question was originally asked, even added some patches to help auto-detect cross-compiler settings. Node.js in the repositories is (at the moment) a rather old version, and may or may not support the full hard-float (VFP) architecture.

For a full detailed HOWTO, see Nathan Rajlich's write up at http://n8.io/cross-compiling-nodejs-v0.8/

I've posted binaries for others who don't want to go through all this hassle for the same hardware at https://gist.github.com/3245130

Solution 2

NodeJS is available pre-compiled for a few Linux distributions: https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager

Since NodeJS is still in testing phase for Debian, in order to install NodeJS on my Raspberry Pi under Debian wheezy, I do:

sudo su
echo deb ftp://ftp.dk.debian.org/debian/ sid main > /etc/apt/sources.list.d/sid.list
apt-get update
apt-get install nodejs
rm /etc/apt/sources.list.d/sid.list
apt-get update
exit

NodeJS works very fine and stable on my Raspberry Pi.

Solution 3

Just a note - this worked great for me but didn't install NPM. So if you want NPM (which you most assuredly do I would think) make sure to run

apt-get install npm

after installing node before you remove the update locations from the update list.

Solution 4

Here's the script I wrote to automatically cross compile NodeJS for ARMv6 (Raspbery Pi) using Jenkins.

https://gist.github.com/hertzg/12c2d7fc40f68ff6deeb

I've used @Adam M-W's 'packing' snippet

The script will automatically download everything required and start building.

Just note that current (v0.10.30) version will not compile due to this issue: https://github.com/joyent/node/issues/8062#issuecomment-52541037

Run with _PARAMS_NODEJS_SOURCE_ARCHIVE_URL=http://nodejs.org/dist/v0.10.29/node-v0.10.29.tar.gz to get the latest cross compile-able version.

#!/bin/bash

set -e
set -x

### IF we dont have archive url prefix
if [ -z "$_PARAMS_NODEJS_SOURCE_ARCHIVE_URL" ]; then
    _PARAMS_NODEJS_SOURCE_ARCHIVE_URL=$(wget -qO- http://nodejs.org/dist/latest/ | egrep -o 'node-v[0-9\.]+.tar.gz' | tail -1);
    _PARAMS_NODEJS_SOURCE_ARCHIVE_URL="http://nodejs.org/dist/latest/"$_PARAMS_NODEJS_SOURCE_ARCHIVE_URL
fi

if [ -z "$_PRAMS_RPI_TOOLS_SOURCE_ARCHIVE_URL" ]; then
    _PRAMS_RPI_TOOLS_SOURCE_ARCHIVE_URL="https://github.com/raspberrypi/tools/archive/master.tar.gz"
fi;

NODEJS_SOURCE_ARCHIVE_FILENAME=$(basename $_PARAMS_NODEJS_SOURCE_ARCHIVE_URL)
NODEJS_SOURCE_DIRECTORY=${NODEJS_SOURCE_ARCHIVE_FILENAME%.tar.gz}
#Download NodeJS
echo "-> Searching for NodeJS "$NODEJS_SOURCE_ARCHIVE_FILENAME;
if [ ! -e "$PWD/$NODEJS_SOURCE_ARCHIVE_FILENAME" ]; then
    echo "--> Downloading from "$_PARAMS_NODEJS_SOURCE_ARCHIVE_URL;
    wget --no-check-certificate -O $NODEJS_SOURCE_ARCHIVE_FILENAME $_PARAMS_NODEJS_SOURCE_ARCHIVE_URL
    echo "--> Download finished!"
fi;

echo "--> Extracting"
rm -rf $NODEJS_SOURCE_DIRECTORY
tar --overwrite -xf $NODEJS_SOURCE_ARCHIVE_FILENAME

echo "--> Linking"
ln -snf "$PWD/$NODEJS_SOURCE_DIRECTORY" "$PWD/node"
echo "-> Done!"


echo "-> Searching Raspberry Pi Toolset";
if [ ! -d "$PWD/rpi" ]; then

    if [ ! -e "$PWD/rpi-tools.tar.gz" ] || [ -s "$PWD/rpi-tools.tar.gz" ]; then
        echo "--> Downloading from "$_PRAMS_RPI_TOOLS_SOURCE_ARCHIVE_URL
        wget --no-check-certificate -O "rpi-tools.tar.gz" $_PRAMS_RPI_TOOLS_SOURCE_ARCHIVE_URL
        echo "--> Download finished"
    else
        echo "--> Found rpi-tools.tar.gz."
    fi

    echo "--> Extracting"
    tar xf "rpi-tools.tar.gz"
    echo "--> Linking tools-master to rpi"
    ln -snf "$PWD/tools-master" "$PWD/rpi"
else
    echo "-> found"
fi;
echo "-> Done!"

echo "-> Cross-Compile..."

echo "--> Setup ENV"
export PATH="$PWD/rpi/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin:"$PATH
export HOST="arm-bcm2708hardfp-linux-gnueabi"
export CC="${HOST}-gcc"
export CXX="${HOST}-g++"
export AR="${HOST}-ar"
export RANLIB="${HOST}-ranlib"
export LD="${HOST}-ld"
export CPP="${HOST}-gcc -E"
export STRIP="${HOST}-strip"
export OBJCOPY="${HOST}-objcopy"
export OBJDUMP="${HOST}-objdump"
export NM="${HOST}-nm"
export AS="${HOST}-as"

"${HOST}-gcc" --version
echo "--> Waiting 5s to contiue"
pushd "$PWD/node"
sleep 5

echo "--> Clean"
make clean

echo "--> Configure"
./configure --prefix=/ --without-snapshot --dest-cpu=arm --dest-os=linux

echo "--> Build"
VERSION=${NODEJS_SOURCE_DIRECTORY##node-}
export BINARYNAME=node-${VERSION}-linux-arm-armv6j-vfp-hard
mkdir ${BINARYNAME}
make install DESTDIR=${BINARYNAME} V=1 PORTABLE=1

echo "--> Pack"
cp README.md ${BINARYNAME}
cp LICENSE ${BINARYNAME}
cp ChangeLog ${BINARYNAME}
tar -czf ${BINARYNAME}.tar.gz ${BINARYNAME}

echo "--> Cleanup"
popd
mv $PWD"/node/${BINARYNAME}.tar.gz" "./"
echo "-> Done!"
Share:
13,714

Related videos on Youtube

Adam M-W
Author by

Adam M-W

Swinburne University of Technology Student (2011 - present):Studying Bachelor of Science (Computer Science) and Engineering (Telecommunications and Networks)

Updated on June 04, 2022

Comments

  • Adam M-W
    Adam M-W almost 2 years

    I'm trying to get node.js v0.7.9 to compile for the raspberry pi, but as node and v8 are quite large, I'm hoping to be able to cross-compile on another more powerful PC. I'm using the linux-x86 arm-bcm2708-linux-gnueabi toolchain from https://github.com/raspberrypi/tools and have used them to successfully build other executables for the system. I ended up setting the CC,CXX,CPP,STRIP,OBJCOPY,etc. variables to the toolchain equivalents in the environmental variables and ran configure with: ./configure --dest-cpu=arm --without-snapshot to get the final executable. Copying it over to the system and running it however produces the following error:

    Extension or internal compilation error at line 0.
    Segmentation fault
    

    However, the segmentation fault doesn't happen for any of the non-javascript tasks like node --version and node --help. Are there any CFLAGS/CXXFLAGS I might be missing causing this problem? Bit confused....

  • Erdogan Kurtur
    Erdogan Kurtur over 11 years
    if you get a NO_PUBKEY error as I did try following lines to get and load required key. gpg --recv-keys AED4B06F473041FA --keyserver keys.gnupg.net gpg -a --export AED4B06F473041FA | apt-key add - good luck
  • Pluto
    Pluto almost 10 years
    I tried this at first, but the version of node.js available was 0.6, which is like 2 years old! At least nvm had a newer version available... But still 0.7.
  • Saran
    Saran over 9 years
    I'm getting node -v --> "Segmentation fault". How can I fix this (apart from building from source)?
  • Banjocat
    Banjocat over 9 years
    This helped me setup on an embedded project that does not run a package manager. Thanks a lot!
  • Brannon
    Brannon over 9 years
    Does this install the latest version of nodejs? When I run this node 0.6 is downloaded.
  • Nathan Friedly
    Nathan Friedly about 9 years
    This didn't work for me, libc6 segfaulted on install.. and uninstall. I ended up just wiping my sd card and reinstalling raspbian because I couldn't figure out how to clean it up.