/nodejs, Raspberry Pi, npm, Linux

Install any version of NodeJS and npm on Raspberry Pi

Or simply put, easily switch between versions of NodeJS on any Linux based system. There are two most popular Node version managers. One, my personal favourite is n package, the other one is nvm.

n

For me the easiest way is to use n package. Just install whatever Node version your official distribution ships with.

sudo yum install nodejs

or, depending on what Linux you're using.

sudo apt-get install nodejs

Then install n as a global package.

sudo npm install -g n

/usr/local/bin/n -> /usr/local/lib/node_modules/n/bin/n
+ [email protected]
added 1 package from 4 contributors in 0.201s

Done, it was that easy. Now install the version of node you want to use:

sudo n lts <--- latest LTS official release
sudo n latest <--- latest official release
sudo n stable <--- stable official release
sudo n 10.16.0 <--- specific version

Execute n on its own to view your downloaded versions, and install the selected one.

n

    node/8.11.2
    node/10.14.2
    node/10.15.0
    node/10.16.0
  ο node/12.18.1

Use up/down arrow keys to select a version, return key to install, d to delete, q to quit

nvm

Run the official install script by executing one of below commands:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

Rerun Profile script to start NVM

source ~/.bashrc

Now nvm should be running and you can confirm it by running below

nvm

Node Version Manager (v0.35.3)

If everything went fine, make nvm use your currently installed Node version

nvm use system

Now using system version of node: v8.11.2 (npm v6.9.0)

List available versions using

nvm ls-remote

Install the latest release of node

nvm install node <--- latest version
nvm install 10.16.0 <--- specific version

If after exiting console and logging in again nvm is not a recognised command, you'll need to add source ~/.bashrc to your .profile or .bash_profile file to make sure it's automatically run on every login. A good explanation of what's going on, can be found here.