How to Install Node.js and npm on Raspberry Pi

By 

Published on

4 min read

Install Node.js and npm 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 front-end solution. npm is the default package manager for Node.js and the world’s largest software registry.

In this tutorial, we will explain how to install Node.js and npm on Raspberry Pi. We’re assuming that you have Raspbian installed on your Raspberry Pi .

Install Node.js and npm from the NodeSource Repository

NodeSource is a company focused on providing enterprise-grade Node support. NodeSource maintains an APT repository containing the latest versions of Node.js.

Enable the NodeSource repository by running the following command in your terminal:

Terminal
curl -sL https://deb.nodesource.com/setup_10.x | sudo bash -
Info
The current LTS version of Node.js is version 10.x, Dubnium. If you want to install Node.js version 8.x just change setup_10.x with setup_8.x

Once the repository is enabled, install Node.js and npm by typing:

Terminal
sudo apt install nodejs

To verify the installation, run the following command which will print the Node.js version:

Terminal
node --version
output
v10.16.0

That’s it. You have successfully installed Node.js and npm on your Raspberry Pi board.

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:

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

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

output
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

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:

Terminal
nvm --version
output
0.34.00

You can now install the latest available version of Node.js by running:

Terminal
nvm install node

Verify the installation:

Terminal
node --version
output
v12.3.1

To better explain how nvm works we’ll install two more versions, the latest LTS version and version 8.9.4.

Terminal
nvm install --lts
nvm install 8.9.4

Once both versions are installed, list the Node.js instances by typing:

Terminal
nvm ls
output
->       v8.9.4
       v10.16.0
        v12.3.1
default -> node (-> v12.3.1)
node -> stable (-> v12.3.1) (default)
stable -> 12.3 (-> v12.3.1) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/dubnium (-> v10.16.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.16.0 (-> N/A)
lts/dubnium -> v10.16.0

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 sessions.

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

Terminal
nvm use 10.16.0

and verify it by typing:

Terminal
nvm current
output
v10.16.0

If you want to set version 10.16.0 as the default Node.js version type:

Terminal
nvm alias default 10.16.0

Install development tools

To be able to compile and install native add-ons from the npm registry you need to install the development tools:

Terminal
sudo apt install build-essential

Uninstall Node.js

If for some reasons you want to uninstall Node.js package, you can use the following command:

Terminal
sudo apt remove nodejs

Conclusion

We have shown you two different ways to install Node.js and npm on your Raspberry Pi board. The method you choose depends on your requirements and preferences. Even though installing the packaged version from the NodeSource repository is easier, the nvm method gives you more flexibility for adding and removing different Node.js versions on a per-user basis.

Now that you’ve installed Node.js on your Raspberry Pi system, you can start developing your application.

If you have any questions or feedback, feel free to comment below.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page