Install Nodejs and NPM on Raspberry Pi [Step-by-Step]


Written by - Deepak Prasad

What is NodeJS 

Nodejs or Node is an open-source and cross-platform runtime environment for executing Javascript code outside of a browser. We often use Node to build back-end services, also called APIs (Application Programming Interfaces). These services power our client application, like a web app running inside a web browser or a mobile app running on a mobile device. These client apps are simply what a user sees and interacts with. They are just the surface and need to talk to some services sitting on the server or in the cloud to store data, send emails or push notifications, kick-off workflows, and so on. Node is ideal for building highly scalable data-intensive and real-time back-end services that power our client applications.

 

What is NPM

On the other hand, NPM stands for Node Package Manager. You can think of NPM as two things:

  1. An online platform
  2. A command-line tool

NPM, as an online platform, is a place where developers can publish and share tools written in Javascript. Anyone can search and use tools available on this online platform. You can find tools for use in the browser, server, or even the command line.

NPM, as a command-line tool, helps developers interact with the NPM online platform. You can use NPM to install and uninstall packages available on the NPM online platform. NPM is also used to manage package versions and dependencies.

 

Install NodeJS and NPM on Raspberry Pi

There are two main ways you can use to install NodeJS and NPM on your Raspberry Pi.

  • Install NodeJS and NPM from the NodeSource repository
  • Install NodeJS and NPM from source code.

This post will look at all the methods above, and you can pick one that suits you.

 

Lab Requirements

  • A Raspberry Pi running the Official Raspberry Pi OS: For better performance, we highly recommend using Raspberry Pi board 3 or 4.
  • An active internet connection
  • A reliable power supply

 

Method 1: Install NodeJS and NPM From the NodeSource Repo

NodeSource is a platform focused on helping developers and organizations run production-ready NodeJS apps by providing enterprise-grade Node support. It maintains an APT repo where you can download the latest version of NodeJS on your system. Follow the steps below.

 

Step 1: Enable the NodeSource Repository

Launch the Terminal and execute the command below. In our case, we are connecting to the Raspberry Pi over SSH.

sudo su
curl -fsSL https://deb.nodesource.com/setup_17.x | bash -

Install Nodejs and NPM on Raspberry Pi [Step-by-Step]

NOTE:
As of writing this post, the latest version of NodeJS is version 17. To get the newest nodejs repository, please check the NodeSource GitHub page .

 

Step 2: Install NodeJS

When done, run the command below to install NodeJS on your system.

sudo apt install nodejs

Install Nodejs and NPM on Raspberry Pi [Step-by-Step]

NOTE:
NodeJS is installed together with NPM. Therefore, you don't need to install the two packages separately.

 

Step 3: Verify Installation

When done, verify NodeJS and NPM installation by running the version command below.

node --version
npm --version

Install Nodejs and NPM on Raspberry Pi [Step-by-Step]

 

Uninstall/ Remove NodeJS and NPM

If you wish to uninstall NodeJS and NPM installed using this method, run the command below.

sudo apt remove nodejs

Install Nodejs and NPM on Raspberry Pi [Step-by-Step]

 

Method 2: Install NodeJS and NPM From Source Code

As the title suggests, this method can get a little complicated since you need to compile and build the NodeJS source code yourself. This method is also limited to specific Raspberry Pi boards, as we will see below. Follow the steps below.

 

Step 1: Update and Upgrade Your System

Since you will compile the source code o your system, you need to ensure that you are running the latest packages to avoid errors. Execute the command below on your Raspberry Pi.

sudo apt update
sudo apt upgrade

The upgrade command can take quite some time to finish depending on your internet connection and when you last upgraded your Pi.

 

Step 2: Check the Node Version You Need to Download

As said earlier, this method can only be used on specific Pi boards architecture depending on the node source code available. To know the architecture on which your PI is running on, execute the command below.

uname -m

Install Nodejs and NPM on Raspberry Pi [Step-by-Step]

From the image above, you can see we are running on armv7l. This architecture version will guide us in downloading the required ode source code as described in the next step.

 

Step 3: Download NodeJS

Open your browser and navigate to the NodeJS Download Page. You will see the available binaries for different platforms and architecture, including Windows, Linux, macOS, x64, and ARM. We need to download the ARM binaries. Now you need to download the specific binary for your architecture. In our case, we are running armv7l. Therefore, we will download NodeJS binaries for ARMV7L.

Install Nodejs and NPM on Raspberry Pi [Step-by-Step]

 

Instead of clicking the download link and downloading the package using our browser or download manager, we will use the wget command. Right-click on the download links and select the option "copy link address" or "copy file location." Launch the Terminal on your Pi and use the syntax below to download the package.

sudo wget https://nodejs.org/dist/v16.13.0/node-v16.13.0-linux-armv7l.tar.xz

Install Nodejs and NPM on Raspberry Pi [Step-by-Step]

When done, run the ls command, and you should see a node package.

 

Step 4: Extract the File

The package downloads in a compressed format; to extract it, use the syntax below.

sudo tar -xvf [file-name]
e.g
sudo tar -xvf node-v16.13.0-linux-armv7l.tar.xz

Install Nodejs and NPM on Raspberry Pi [Step-by-Step]

When you rerun the ls command, you will now see a node directory.

 

Step 5: Copy the Extracted Files to Directory Path

To make Node available system-wide for the currently logged-in user, we need to copy the extracted file to the /usr/local directory. To get started, first navigate to the newly created directory using the cd command as shown below.

cd node-v16.13.0-linux-armv7l

We will use the command below to copy all the contents to the /usr/local directory.

sudo cp -R * /usr/local

Let's discuss the command above in detail:

  • -R: This parameter means that "copy all the files recursively." That enables us to copy a directory and all its contents.
  • *: This asterisk means that we are copying everything in our current directory.

Install Nodejs and NPM on Raspberry Pi [Step-by-Step]

That's it! We have successfully installed NodeJS and NPM on our system.

 

Step 6: Verify Installation

When done, verify NodeJS and NPM installation by running the version command below.

node --version
npm --version

Install Nodejs and NPM on Raspberry Pi [Step-by-Step]

 

Conclusion

This post has given you a step-by-step guide on installing NodeJS and NPM on your system. If you encounter any errors while executing any of the commands above, please tell us in the comments, and we will help where we can.

 

Views: 116

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can reach out to him on his LinkedIn profile or join on Facebook page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

1 thought on “Install Nodejs and NPM on Raspberry Pi [Step-by-Step]”

  1. # curl -fsSL https://deb.nodesource.com/setup_17.x | bash -
      Node.js 17.x is no longer actively supported!
    
      You will not receive security or critical stability updates for this version.
    
      You should migrate to a supported version of Node.js as soon as possible.
      Use the installation script that corresponds to the version of Node.js you
      wish to install. e.g.
    
       * https://deb.nodesource.com/setup_14.x — Node.js 14 LTS "Fermium" (recommended)
       * https://deb.nodesource.com/setup_16.x — Node.js 16 "Gallium"
       * https://deb.nodesource.com/setup_18.x — Node.js 18 "Eighteen" (current)
    
      Please see https://github.com/nodejs/Release for details about which
      version may be appropriate for you.
    
      The NodeSource Node.js distributions repository contains
      information both about supported versions of Node.js and supported Linux
      distributions. To learn more about usage, see the repository:
        https://github.com/nodesource/distributions

    So I continued with:

    > # curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
    Reply

Leave a Comment