> ## Documentation Index
> Fetch the complete documentation index at: https://help.freakhosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Install Node.js on Ubuntu and Run Apps with PM2

> Install Node.js LTS on Ubuntu with nvm, start your app with pm2 start app.js, then run pm2 save and pm2 startup so it restarts after a crash or a reboot.

To install Node.js on Ubuntu, install nvm, run `nvm install --lts`, then install PM2 with `npm install -g pm2`. Start your app with `pm2 start app.js --name myapp`, run `pm2 save`, then run `pm2 startup` and paste the `sudo` command it prints. PM2 now restarts your app if it crashes and starts it again after a reboot. Put Nginx in front for your domain and HTTPS.

| Difficulty | Time | Works on | Needs |
| - | - | - | - |
| Beginner | 15 minutes | Ubuntu 22.04 / 24.04, Debian 12 / 13, AlmaLinux / Rocky 9 / 10 | A non-root user with sudo |

## Before you start

* Work as a normal user, not `root`, so your app does not run with full system rights. See [create a sudo user](https://help.freakhosting.com/portals/other/how-to-create-a-sudo-user-on-linux.md). This guide uses the user `alex`.
* Pick an LTS (long-term support) release of Node.js for servers. Today the LTS lines are 26, 24 and 22. Node.js 26 is the newest LTS.

Node.js runs JavaScript on the server. PM2 is a process manager: it keeps your app running in the background, restarts it on crashes and collects its logs.

## How to install Node.js

> [tab] nvm (recommended, any distro)
>
> nvm installs Node.js in your home folder, so you need no `sudo` and can switch versions easily.
>
> ```bash theme={null}
> curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.8/install.sh | bash
> source ~/.bashrc
> nvm install --lts
> nvm alias default --lts node
> ```
>
> `nvm install --lts` installs the newest LTS release. To pin a line, use for example `nvm install 24`.

> [tab] NodeSource (system-wide)
>
> NodeSource packages install Node.js for all users through `apt` or `dnf`. On Ubuntu / Debian:
>
> ```bash theme={null}
> curl -fsSL https://deb.nodesource.com/setup_lts.x -o nodesource_setup.sh
> sudo -E bash nodesource_setup.sh
> sudo apt install -y nodejs
> ```
>
> On AlmaLinux / Rocky:
>
> ```bash theme={null}
> curl -fsSL https://rpm.nodesource.com/setup_lts.x -o nodesource_setup.sh
> sudo bash nodesource_setup.sh
> sudo dnf install -y nodejs
> ```
>
> Check NodeSource's list of supported releases first.

Check the result:

```bash theme={null}
node -v
npm -v
```

The version distribution repositories ship (`apt install nodejs`) is often older than the current LTS. Check it with `apt-cache policy nodejs` before you rely on it.

## How to install PM2 and start your app

**1. Install PM2**

```bash theme={null}
npm install -g pm2
```

With NodeSource Node.js, use `sudo npm install -g pm2`.

**2. Start your app**

From your app's folder:

```bash theme={null}
cd ~/myapp
pm2 start app.js --name myapp
```

For an app started with `npm start`, use `pm2 start npm --name myapp -- start`.

**3. Save the process list**

```bash theme={null}
pm2 save
```

PM2 stores the list in `~/.pm2/dump.pm2` and restores it at boot.

**4. Start PM2 at boot**

```bash theme={null}
pm2 startup
```

PM2 prints a command that starts with `sudo env PATH=$PATH:…`. Copy and run that exact command. It creates a systemd service named `pm2-alex`.

## How to check it worked

```bash theme={null}
pm2 list
```

Your app shows `online` (output shortened):

```text theme={null}
│ id │ name     │ mode │ ↺ │ status │ cpu │ memory │
│ 0  │ myapp    │ fork │ 0 │ online │ 0%  │ 42.1mb │
```

Test the app locally, for example `curl http://127.0.0.1:3000`. Reboot the server once to confirm the app comes back by itself.

## Everyday PM2 commands

| Task | Command |
| - | - |
| Show apps | `pm2 list` |
| Live logs | `pm2 logs myapp` |
| Restart | `pm2 restart myapp` |
| Zero-downtime reload (cluster mode) | `pm2 reload myapp` |
| Stop / remove | `pm2 stop myapp` / `pm2 delete myapp` |
| CPU and memory view | `pm2 monit` |
| Save after changes | `pm2 save` |

For several apps or environment variables, create a config file with `pm2 init simple`. It writes `ecosystem.config.js`, which you start with `pm2 start ecosystem.config.js`.

## How to put your app online with Nginx

Keep the app listening on `127.0.0.1`, not on a public port. Then add an Nginx reverse proxy for your domain and HTTPS. See [set up Nginx as a reverse proxy](https://help.freakhosting.com/portals/other/how-to-set-up-nginx-as-a-reverse-proxy.md).

> [!WARNING]
>
> Do not open your app's port (for example 3000) in the firewall. Only ports 80 and 443 for Nginx, and your SSH port, need to be open.

## Frequently asked questions

### My app stops when I close the SSH window. Why?

You started it with `node app.js` in the terminal. Start it with PM2 instead, or use [tmux or screen](https://help.freakhosting.com/portals/other/how-to-keep-ssh-sessions-running-with-tmux-and-screen.md) for quick tests only.

### After a reboot my app is gone. What did I miss?

Either `pm2 save` was not run after starting the app, or the `sudo env PATH=…` command from `pm2 startup` was not run. Do both again.

### How do I upgrade Node.js?

Install the new version (`nvm install --lts`), then refresh PM2's boot script: run `pm2 unstartup` and the command it prints, then `pm2 startup` and the command it prints, and finally `pm2 save`.

### Can I use systemd instead of PM2?

Yes. A plain systemd service also restarts your app and starts it at boot. See [create a systemd service](https://help.freakhosting.com/portals/other/how-to-create-a-systemd-service.md).

## Related guides

> [card] [VPS and dedicated server guides](https://help.freakhosting.com/portals/other/vps-and-dedicated-server-guides.md)
>
> Every server guide in one place.

> [card] [Nginx reverse proxy](https://help.freakhosting.com/portals/other/how-to-set-up-nginx-as-a-reverse-proxy.md)
>
> Domain and HTTPS for your app.

> [card] [Host a Discord bot](https://help.freakhosting.com/portals/other/how-to-host-a-discord-bot-on-a-vps.md)
>
> A common Node.js use.

> [card] [Create a systemd service](https://help.freakhosting.com/portals/other/how-to-create-a-systemd-service.md)
>
> The alternative to PM2.

> [card] [Fix 502 Bad Gateway](https://help.freakhosting.com/portals/other/how-to-fix-502-bad-gateway-nginx.md)
>
> When Nginx can't reach the app.

***

> [!NOTE]
>
> **Checked against the official [Node.js releases page](https://nodejs.org/en/about/previous-releases), the [nvm README](https://github.com/nvm-sh/nvm) and the [PM2 startup documentation](https://pm2.keymetrics.io/docs/usage/startup/)** on 4 November 2026; PM2 7.1.2 commands (`start`, `save`, `startup`, `init simple`) tested on Ubuntu 24.04 LTS with Node.js 22. nvm and NodeSource installs were not tested. Commands can differ on other distributions or releases.

> [!NOTE]
>
> ### Need Extra Help?
>
> If you encounter any issues, our support team is ready to assist:
>
> * **Live Chat:** Quick assistance via our website.
> * **Support Ticket:** [Open a Ticket](https://freakhosting.com/clientarea/submitticket.php)
> * **Discord:** [Join our Community](https://discord.gg/freakhosting)
> * **Email:** [support@freakhosting.com](mailto:support@freakhosting.com)

> [!TIP]
>
> ### Save on Your Hosting
>
> Ready to upgrade your hosting? Use code **KB20** at checkout for **20% off** your first order!

***

**Last Updated:** November 2026


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.