Node.js, being fast and light-weight, is a perfect fit for low power compute modules like the Raspberry Pi. This article covers an easy way to install the most up-to-date version of Node.js and a way to easily keep the version up-to-date over time. At the time of this writing, the most recent version of Node.js is v5.7.0.
We're going to use nvm to manage our Node.js installations. nvm is a well supported tool that allows us to install multiple versions of node and easily switch between them.
This tutorial assumes you're using the Raspian OS, but they should port fairly well to most Linux distributions.
Open the terminal and as the pi user run the following:
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash
Close the terminal and re-open it. Now install the required version of node:
$ nvm install v5.7.0
$ nvm alias default v5.7.0
You can now test and ensure node is properly installed:
$ node --version
v5.7.0
This only installs node for the pi user. Many operations on the Raspberry Pi require root priviledges, like talking to the GPIO. To install node where root can access it, simply symlink the desired version into /usr/bin:
$ sudo ln -s /home/pi/.nvm/versions/node/v5.7.0/bin/node /usr/bin/node
$ sudo ln -s /home/pi/.nvm/versions/node/v5.7.0/bin/npm /usr/bin/npm
Now you can use node and npm as root, which isn't recommended, but sometimes hard to avoid. There are instructions out there, that we've tried with varying degrees of success, to give non-root users GPIO access.
$ sudo node --version
v5.7.0
You can update the node version at any time by running the nvm install and symlink commands again for the new version:
$ nvm install vX.X.X
$ nvm alias default vX.X.X
$ sudo rm /usr/bin/node
$ sudo rm /usr/bin/npm
$ sudo ln -s /home/pi/.nvm/versions/node/vX.X.X/bin/node /usr/bin/node
$ sudo ln -s /home/pi/.nvm/versions/node/vX.X.X/bin/npm /usr/bin/npm