Installing Node and NVM on Raspberry Pi

Node.js is an open source cross-platform JavaScript run-time environment built on Chrome's JavaScript engine that allows server-side execution of JavaScript code. It is mainly used to build server-side applications, but it is also very popular as a full-stack and for providing build tooling for front-end solutions. NPM is the default package manager for Node.js and the world's largest software registry.

In this tutorial, I'm going to explain how to install Node.js and NPM on Raspberry Pi. I'm going to assume that you have Raspbian installed on your Raspberry Pi.

Install Node.js and NPM using NVM

NVM (Node Version Manager) is a bash script that allows you to install and manage multiple Node.js versions. Use this method if you need to install a specific Node.js version or if you need to have more than one Node.js versions installed on your Raspberry Pi.

To install NVM, run the following curl command which will download and run the nvm installation script:

{% highlight ruby linenos %} curl -o- | bash {% endhighlight %}

The installation script will clone the NVM repository from Github to your local ~/.nvm directory and add the NVM path to your Bash profile.

Close and reopen your terminal to start using NVM, or run the following to use it now:

{% highlight ruby linenos %} export NVMDIR="$HOME/.nvm" [ -s "$NVMDIR/" ] && . "$NVMDIR/" # This loads nvm [ -s "$NVMDIR/bashcompletion" ] && . "$NVMDIR/bashcompletion" # This loads nvm bashcompletion {% endhighlight %}

As the output says, you can either open a new shell session or run those commands that will add the nvm path to your current session.

Do whatever is easier for you.

To ensure that nvm is properly installed type:

{% highlight ruby linenos %} nvm --version {% endhighlight %}

Assuming you see a version number, you can now install the latest available version of Node.js by running:

{% highlight ruby linenos %} nvm install node {% endhighlight %}

Once NVM has downloaded Node and made it available to you, it can be verified by:

{% highlight ruby linenos %} node --version {% endhighlight %}

To better demonstrate how NVM works we'll install two more versions, the latest LTS version and version 10.

{% highlight ruby linenos %} nvm install --lts nvm install 8.9.4 {% endhighlight %}

Once both versions are installed, list the Node.js instances by typing: {% highlight ruby linenos %} nvm ls -> v8.9.4 v10.16.3 v12.10.0 default -> node (-> v12.10.0) node -> stable (-> v12.10.0) (default) stable -> 12.10 (-> v12.10.0) (default) iojs -> N/A (default) unstable -> N/A (default) lts/* -> lts/dubnium (-> v10.16.3) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.16.1 (-> N/A) lts/dubnium -> v10.16.3 {% endhighlight %}

In the output above, the entry with an arrow on the right (-> v8.9.4), is the version used in the current shell session and the default version is set to v12.3.1.

The default version is the version that will be used when you open new shell (aka. terminal) session windows.

To change the currently active version to v10.16.3 use the following command:

{% highlight ruby linenos %} nvm use 10.16.3 {% endhighlight %}

and verify it by typing:

{% highlight ruby linenos %} nvm current {% endhighlight %}

If you want to set version 10.16.3 as the new default version when you open a new shell session window, use the alias default command:

{% highlight ruby linenos %} nvm alias default 10.16.3 {% endhighlight %}


Hopefully this was a simple walkthrough to show you how to setup NVM and Node.js on the Raspberry Pi. NVM is a popular tool in most desktop environments, so it's great to also have that option on the Raspberry Pi!

About the author

Hey, I'm Erik, a software engineer, gamer, writer, and content creator. I publish articles and tutorials about modern software development, design, and programming.

Get the newsletterBuy me a coffee