How to Install Node.js on Raspberry Pi Using NVM: Complete Guide

Node.js is a powerful JavaScript runtime that's perfect for running web applications, APIs, and automation scripts on your Raspberry Pi. Using NVM (Node Version Manager) is the best approach because it allows you to easily install, manage, and switch between different Node.js versions.

In this guide, I'll walk you through installing Node.js on your Raspberry Pi using NVM, including how to manage versions and troubleshoot common issues.

Prerequisites

Before we start, make sure you have:

  • A Raspberry Pi running Raspberry Pi OS (or any Debian-based distribution)
  • Access to the terminal (local or via SSH)
  • An active internet connection
  • Basic knowledge of using the terminal

Step 1: Check Your Raspberry Pi Architecture

First, let's check your Raspberry Pi's architecture to determine the best installation method:

uname -m

You'll see one of these results:

  • aarch64 - 64-bit ARM (Raspberry Pi 3/4/5 with 64-bit OS)
  • armv7l - 32-bit ARM (Raspberry Pi 3/4 with 32-bit OS)
  • armv6l - 32-bit ARM (Raspberry Pi Zero/1)

For most modern Raspberry Pi models, you'll see aarch64 or armv7l, which work perfectly with NVM.

Step 2: Update Your System

Before installing Node.js, it's a good idea to update your system:

sudo apt update && sudo apt upgrade -y

This ensures you have the latest packages and dependencies.

Step 3: Install Required Dependencies

Install the necessary build tools and dependencies:

sudo apt install -y curl wget build-essential

These packages are needed for NVM and Node.js compilation.

Step 4: Install NVM (Node Version Manager)

Now let's install NVM, which will manage our Node.js installations:

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

This command downloads and runs the NVM installation script.

Step 5: Load NVM in Your Current Session

After installation, you need to load NVM in your current terminal session:

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

Or simply restart your terminal session for the changes to take effect.

Step 6: Verify NVM Installation

Check if NVM is properly installed:

nvm --version

You should see the NVM version number (e.g., 0.39.7).

Step 7: Install Node.js

Now let's install Node.js. I recommend using the LTS (Long Term Support) version for stability:

Install Latest LTS Version

nvm install --lts

Or Install a Specific Version

If you need a specific version, you can install it directly:

nvm install 20
nvm install 18

Set Default Version

Set the LTS version as your default:

nvm use --lts
nvm alias default node

Step 8: Verify Node.js Installation

Check that Node.js and npm are properly installed:

node --version
npm --version

You should see output like:

v20.15.1
10.7.0

Step 9: Install Global Packages (Optional)

You can now install global npm packages. For example, to install a development server:

npm install -g nodemon
npm install -g pm2

Alternative: Direct Binary Installation for ARMv6

If you have a Raspberry Pi Zero or Pi 1 (ARMv6 architecture), NVM might not work properly. In this case, use direct binary installation:

Download Node.js Binary

wget https://unofficial-builds.nodejs.org/download/release/v18.19.0/node-v18.19.0-linux-armv6l.tar.xz

Extract and Install

tar -xJf node-v18.19.0-linux-armv6l.tar.xz
sudo mv node-v18.19.0-linux-armv6l /usr/local/node18

Add to PATH

echo 'export PATH=/usr/local/node18/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

Verify Installation

node --version
npm --version

Managing Node.js Versions with NVM

NVM makes it easy to manage multiple Node.js versions:

List Installed Versions

nvm list

Switch Between Versions

nvm use 18
nvm use 20
nvm use --lts

Install Additional Versions

nvm install 16
nvm install 22

Set Default Version

nvm alias default 20

Uninstall a Version

nvm uninstall 16

Making Node.js Available System-Wide

By default, NVM installs Node.js for your user only. If you need Node.js available system-wide (for root or other users):

Create Symlinks

sudo ln -s $(which node) /usr/bin/node
sudo ln -s $(which npm) /usr/bin/npm

Or Install for Root User

sudo su
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm install --lts
nvm use --lts
nvm alias default node

Testing Your Installation

Let's create a simple test to verify everything is working:

Create a Test File

mkdir ~/nodejs-test
cd ~/nodejs-test
nano test.js

Add this content to test.js:

console.log('Node.js is working on Raspberry Pi!');
console.log('Node.js version:', process.version);
console.log('Platform:', process.platform);
console.log('Architecture:', process.arch);

Run the Test

node test.js

You should see output confirming Node.js is working correctly.

Troubleshooting Common Issues

NVM Command Not Found

If you get "nvm: command not found":

  1. Reload your shell configuration:

    source ~/.bashrc
    
  2. Or restart your terminal session

  3. Check if NVM is properly installed:

    ls -la ~/.nvm
    

Permission Errors

If you encounter permission errors:

  1. Check file permissions:

    ls -la ~/.nvm
    
  2. Fix permissions if needed:

    chmod +x ~/.nvm/nvm.sh
    

Node.js Installation Fails

If Node.js installation fails:

  1. Clear NVM cache:

    nvm cache clear
    
  2. Try installing with specific architecture:

    nvm install --lts --latest-npm
    
  3. Check available versions:

    nvm list-remote
    

Memory Issues During Installation

If you encounter memory issues during compilation:

  1. Increase swap space:

    sudo dphys-swapfile swapoff
    sudo nano /etc/dphys-swapfile
    

    Change CONF_SWAPSIZE=100 to CONF_SWAPSIZE=2048

    sudo dphys-swapfile setup
    sudo dphys-swapfile swapon
    
  2. Or use pre-compiled binaries:

    nvm install --lts --binary
    

Performance Considerations

Memory Usage

Node.js applications can be memory-intensive. Monitor your Pi's memory usage:

free -h

CPU Temperature

Check CPU temperature during intensive operations:

vcgencmd measure_temp

Optimizing for Raspberry Pi

For better performance on Raspberry Pi:

  1. Use PM2 for process management:

    npm install -g pm2
    
  2. Enable Node.js optimizations:

    export NODE_OPTIONS="--max-old-space-size=512"
    

Conclusion

You now have Node.js installed on your Raspberry Pi using NVM! This setup gives you the flexibility to:

  • Easily switch between Node.js versions
  • Install multiple versions simultaneously
  • Keep your system organized and up-to-date
  • Run Node.js applications efficiently

Whether you're building web applications, APIs, or automation scripts, Node.js on Raspberry Pi opens up countless possibilities for your projects.

Need help with a specific Node.js project on your Raspberry Pi? Drop me a message on X!

A Wifi Place Logo

Find Work-Friendly Cafes Near You

Check my free directory of laptop-friendly cafes with Wifi.

Browse Cafes