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
Pin: release n=wheezy
Pin: release n=jessie
Pin: release o=Raspbian
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++
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:
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:
should reply v4.1.0 (or whatever version you compiled)
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
Thanks for your notes.
For speeding up the build proces, one could use “make -j 4” on the Pi 2 to utilise all 4 cores.
[…] Felong先生的這篇貼文：Node.js v4 & v5 on Raspberry Pi 2。在Raspbian […]
I translate this article into Traditional Chinese and post it here: http://swf.com.tw/?p=836
I also created Node.js v4.4.0 and v5.8.0 .deb package files (tested on R-Pi 2 Jessie) and uploaded them to Google Drive.