# 🐧Installing the Latest Node.js, npm, & PM2 from NodeSource on Debian⚙️

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:

```plaintext
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.

### **Install the Latest LTS Version (Recommended)**

```plaintext
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)**

```plaintext
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:

```plaintext
sudo apt install -y nodejs
```

---

## **Step 3: Verify the Installation**

After installation, check the installed versions:

```plaintext
node -v
npm -v
```

Expected output (example):

```plaintext
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:

```plaintext
sudo npm install -g pm2
```

### **Verify PM2 Installation**

```plaintext
pm2 -v
```

Expected output:

```plaintext
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:

```plaintext
pm2 startup systemd
```

This outputs a command similar to:

```plaintext
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:

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

Run the script with PM2:

```plaintext
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:

```plaintext
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! 🚀
