Node.js v4 & v5 on Raspberry Pi 2

Node.jsI had been experimenting with the world of Javascript on the server-side using Node.js.  I “fell off the horse” when Node.js had an identity and direction crisis and io.js was forked.  Node.js and io.js have reunited and the latest supported version is v4.2 with v5.0 just released.  The latest “apt-get” version of Node.js as of 15 September 2015 is (something like) 0.6 on Raspbian Wheezy.  Not current enough IMHO. I also wanted to take advantage of the ARMv7 processor in the Raspberry Pi 2.  The best way forward was to figure out how to compile the latest version from source.  NOTE that instructions have been updated for Raspbian Jessie and later versions of node.js.

I looked around and found a couple of resources that helped me compile and install Node.js 4.1 on a Raspberry Pi 2.  One challenge is that compiling source requires gcc/g++ version 4.8 or better.  R-Pi currently has 4.6.  I found a great tutorial on how to upgrade gcc to 4.8.4 from the “raspbian jesse” repository.  You can find it at the SOME WIDE OPEN SPACE blog.  I also have the instructions below.

After installing gcc 4.8, it is a far easier task to download the latest Node.js source, configure, make, and install.  Again I found online help– at eLinux – Node.js on R-Pi.  You can find these after the gcc install instructions.


Using Raspbian Jessie?  Go directly to the “Build Latest” section below!

GCC 4.8 on Raspberry Pi Wheezy

Ever needed gcc/g++ 4.8 on Raspbian Wheezy? Then do:


sudo nano /etc/apt/sources.list

Change content to

deb http://mirrordirector.raspbian.org/raspbian/ wheezy main contrib non-free rpi
deb http://archive.raspbian.org/raspbian wheezy main contrib non-free rpi
# Source repository to add
deb-src http://archive.raspbian.org/raspbian wheezy main contrib non-free rpi
deb http://mirrordirector.raspbian.org/raspbian/ jessie main contrib non-free rpi
deb http://archive.raspbian.org/raspbian jessie main contrib non-free rpi
# Source repository to add
deb-src http://archive.raspbian.org/raspbian jessie main contrib non-free rpi

Add preferences file

sudo nano /etc/apt/preferences

and insert
Package: *
Pin: release n=wheezy
Pin-Priority: 900
Package: *
Pin: release n=jessie
Pin-Priority: 300
Package: *
Pin: release o=Raspbian
Pin-Priority: -10

update package list
sudo apt-get update

Install gcc/g++ 4.8 from jessie repositories
sudo apt-get install -t jessie gcc-4.8 g++-4.8

To remove gcc/g++ alternative configuration (if there is any, there is none by default)
sudo update-alternatives --remove-all gcc
sudo update-alternatives --remove-all g++

Install alternatives
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 4.8 is your default compiler. You can verify it by
sudo gcc --version

If you want to change it, you can
sudo update-alternatives --config gcc
sudo update-alternatives --config g++


Build latest version of Node.js

Go to Nodejs.org and copy the source code download url. In my example: node-v4.1.0.tar.gz

More current version (as of 8 November 2015) URLs:

https://nodejs.org/dist/v4.2.2/node-v4.2.2.tar.gz

https://nodejs.org/dist/v5.0.0/node-v5.0.0.tar.gz

 

Run the following commands in a terminal.  Replace the URL and “node.xxx” depending on the version you are compiling.  As mentioned in a comment, you can use “make -j 4” if you have the R-Pi 2.

wget https://nodejs.org/dist/v4.1.0/node-v4.1.0.tar.gz
tar -xzf node-v4.1.0.tar.gz
cd node-v4.1.0
./configure
make
sudo make install

Note that in your case, you may need to change the bold parts according to the version you want

After that, you can check that Node and NPM are properly installed using the following command lines:

node -v

should reply v4.1.0 (or whatever version you compiled)

npm -v

should reply 2.14.3 (or later)

The compilation will run for 1 or 2 hours on RPi.

Those instructions have been tested with Node.js 4.1.0 and Node.js 4.2.2

 

3 thoughts on “Node.js v4 & v5 on Raspberry Pi 2”

  1. Pingback: 在Raspberry Pi編譯與安裝最新版Node.js - 網昱多媒體

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top