šŸ§Installing the Latest Node.js, npm, & PM2 from NodeSource on Debianāš™ļø

Ronald BartelsRonald Bartels
2 min read

If you're running Debian and need the latest version of Node.js, npm, and PM2, the best way is to use NodeSourceā€™s setup script. NodeSource maintains up-to-date versions of Node.js, which are newer than what Debianā€™s package manager provides.


Step 1: Update and Install Required Packages

Before installing anything, update your system and install prerequisites:

sudo apt update && sudo apt upgrade -y
sudo apt install -y curl ca-certificates
  • curl: Used to fetch the NodeSource setup script.

  • ca-certificates: Ensures secure HTTPS connections.


Step 2: Install Node.js from NodeSource

Run the official setup script from NodeSource. You can choose the latest LTS (recommended) or current (cutting-edge) version.

bashCopyEditcurl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
  • The setup_lts.x script automatically detects your Debian version and configures the correct repository.

OR Install the Latest Current Version (More Experimental)

curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
  • This installs the latest cutting-edge release instead of the stable LTS.

Now, install Node.js:

sudo apt install -y nodejs

Step 3: Verify the Installation

After installation, check the installed versions:

node -v
npm -v

Expected output (example):

20.10.0
10.2.1

(Your version may vary depending on the latest release.)


Step 4: Install PM2 (Process Manager for Node.js)

PM2 is a production process manager that helps keep your Node.js apps running in the background.

Install it globally using npm:

sudo npm install -g pm2

Verify PM2 Installation

pm2 -v

Expected output:

5.3.0

Step 5: Set PM2 to Auto-Start on System Boot

To ensure that PM2 starts your apps automatically after a reboot, run:

pm2 startup systemd

This outputs a command similar to:

sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u youruser --hp /home/youruser

Run the command as shown to enable PM2 at boot.


Step 6: Test PM2 with a Sample Node.js App

To test, create a simple Node.js server:

echo 'console.log("Hello from PM2!"); setInterval(() => console.log("Running..."), 5000);' > test.js

Run the script with PM2:

pm2 start test.js --name my-test-app
pm2 status

This starts test.js and keeps it running.


Step 7: Save and Auto-Restore Running Applications

To ensure PM2 restarts all running apps after a reboot:

pm2 save

Wrap

šŸŽ‰ You now have the latest Node.js, npm, and PM2 installed on Debian!

  • Node.js & npm from NodeSource

  • PM2 to manage Node.js applications

  • Auto-start and persistence after reboots

Now you can deploy and manage your Node.js apps easily! šŸš€

1
Subscribe to my newsletter

Read articles from Ronald Bartels directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ronald Bartels
Ronald Bartels

Driving SD-WAN Adoption in South Africa