Help center
Tutorials

Install and run Node.js on a VPS

Node.js, pm2, and a systemd unit that survives reboots.

Install Node.js (the right way)

Skip the Ubuntu repo; it ships ancient versions. Use NodeSource or nvm.

With NodeSource (system-wide)

curl -fsSL https://deb.nodesource.com/setup_22.x | bash -
apt install -y nodejs

With nvm (per-user, recommended for dev)

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
source ~/.bashrc
nvm install --lts

Build dependencies

Native modules (bcrypt, sharp, canvas) need:

apt install -y build-essential python3

Run in production

Don't run node app.js directly. Use a process manager.

Option A; pm2 (zero-config, restart-on-crash)

npm i -g pm2
pm2 start npm --name web -- start
pm2 save
pm2 startup systemd -u $USER --hp $HOME

The last command prints a sudo command; copy and run it. pm2 now restarts your app on every reboot.

Option B; plain systemd

/etc/systemd/system/myapp.service:

[Unit]
Description=My App
After=network.target

[Service]
Type=simple
User=deploy
WorkingDirectory=/srv/myapp
ExecStart=/usr/bin/node server.js
Restart=on-failure
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable --now myapp
journalctl -u myapp -f

Reverse proxy

Always front Node.js with nginx (see our nginx guide). Node.js doesn't handle TLS termination or static assets as efficiently as nginx does.

Didn't solve it?

Problems on our side are always handled. Hands-on help with your own setup is part of the support plans.

More in Tutorials