Skip to content

Node.js v4 and native addons

Brian Cooke edited this page Apr 14, 2017 · 7 revisions

Node.js addons like epoll (which onoff depends on), i2c-bus, and pigpio are compiled when they are installed. To compile addons for io.js v3 or Node.js v4 or higher, gcc/g++ 4.8 or higher are required. Linux images like Raspbian Wheezy 2015-05-05 for the Raspberry Pi or Debian Wheezy 2015-03-01 for the BeageBone Black ship with gcc/g++ 4.6. Before installing addons for io.js v3 or Node.js v4 or higher it's necessary to ensure that gcc/g++ 4.8 or higher are installed.

On a Raspberry Pi, one easy way to get gcc/g++ 4.9 is to use Raspbian Jessie rather that Raspbian Wheezy. Raspbian Jessie is available for download at raspberrypi.org. Alternatively, optional gcc/g++ compilers are available for Raspbian Wheezy and can be installed as described below.

Installing gcc/g++ 4.8 on Raspbian Wheezy for the Raspberry Pi

First, check to see which versions of gcc/g++ are installed:

pi@raspberrypi ~ $ gcc --version
gcc (Debian 4.6.3-14+rpi1) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

pi@raspberrypi ~ $ g++ --version
g++ (Debian 4.6.3-14+rpi1) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Here, it's 4.6.3 so 4.8 needs to be installed and this can be achieved with the following commands:

Run these commands first (the second one expects user input):

sudo apt-get update # update the system's package list
sudo apt-get install gcc-4.8 g++-4.8

Then run these commands:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 20
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.6 20
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 50

Now recheck to see which versions of gcc/g++ are installed:

pi@raspberrypi ~ $ gcc --version
gcc (Raspbian 4.8.2-21~rpi3rpi1) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

pi@raspberrypi ~ $ g++ --version
g++ (Raspbian 4.8.2-21~rpi3rpi1) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

It should now be possible to install onoff, epoll, i2c-bus, or pigpio with io.js v3 or Node.js v4 or higher.

If ever required, the following commands can be used to switch between the different version gcc/g++.

sudo update-alternatives --config gcc
sudo update-alternatives --config g++