Page cover image

Installing Node.js on Raspberry Pi

Introduction

Node.js is an open-source, cross-platform JavaScript runtime environment that allows you to run JavaScript code outside of a web browser.

It is built on the Chrome V8 JavaScript engine and provides an event-driven, non-blocking I/O model, making it efficient and suitable for building scalable network applications.

Node.js has gained popularity among developers for its ability to handle high-throughput and real-time applications. It is commonly used for building server-side applications, command-line tools, and even desktop applications using frameworks like Electron.

In this guide, we will walk through the steps to install Node.js on a Raspberry Pi.

Prerequisites

Before proceeding with the installation, ensure that you have the following:

  • Raspberry Pi (any model)

  • Raspberry Pi OS (formerly known as Raspbian) installed on your Raspberry Pi

  • Internet connection on your Raspberry Pi

Step 1: Update and Upgrade Packages

First, let's make sure your Raspberry Pi is up to date.

Open a terminal and run the following commands to update the package list and upgrade installed packages:

sudo apt update
sudo apt upgrade

Step 2: Install Required Dependencies

To set up the Node.js repository and install Node.js, we need a few dependencies. Run the following command to install them:

sudo apt install -y curl gnupg

Step 3: Add Node.js Repository

Node.js provides an official repository for easy installation on Debian-based systems like Raspberry Pi OS.

Follow these steps to add the repository:

Import the Node.js GPG key

curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -

This command downloads and runs a setup script that adds the Node.js GPG key and repository to your system.

Note that setup_18.x refers to the latest LTS (Long-Term Support) version of Node.js. You can replace 18.x with the desired version number.

Update the package list again to include the newly added repository

sudo apt update

Step 4: Install Node.js

Now that the Node.js repository is set up, you can install Node.js using the following command:

sudo apt install -y nodejs

This command installs Node.js and npm (Node Package Manager), which is used for managing Node.js packages and dependencies.

Step 5: Verify Installation

To verify that Node.js and npm are installed correctly, run the following commands:

node -v
npm -v

These commands will display the installed versions of Node.js and npm, respectively.

Step 6: (Optional) Install Build Tools

Some Node.js packages may require compiling native code.

To ensure that you have the necessary build tools, you can install the build-essential package:

sudo apt install -y build-essential

This package includes the required compilers and libraries for building Node.js packages with native dependencies.

Conclusion

Congratulations! You have successfully installed Node.js on your Raspberry Pi.

You can now start developing and running Node.js applications on your Raspberry Pi.

To get started, you can create a new file with a .js extension and write your Node.js code.

For example, create a file named app.js with the following content:

console.log("Hello, Node.js on Raspberry Pi!");

Then, open a terminal, navigate to the directory where you saved the file, and run the following command:

node app.js

You should see the message "Hello, Node.js on Raspberry Pi!" printed in the terminal.

Feel free to explore the vast ecosystem of Node.js packages and frameworks to build powerful applications on your Raspberry Pi. Happy coding!

Last updated

Was this helpful?