Save 10%Pay yearly on any plan. 6% half-yearly, 3% quarterly. Help centreWhatsAppTelegram TeamsOpen a ticket

Installing npm and Node.js on Ubuntu Server

Three ways to install Node.js and npm on Ubuntu Server: the Ubuntu package, the NodeSource repository, or NVM. Then run an app as a service.

3 min readBy Daniel

Reviewed by the AmazingRDP technical team

Installing Node.js on Ubuntu Server

A place to run your Node.js application. A Linux VPS gives you root access and a public IP address. See Linux VPS from $6.99

Node.js runs JavaScript on a server, and npm is the package manager that comes with it. Ubuntu offers three ways to install them. They differ in one thing that matters: how current the version is, and who keeps it updated.

Which method?

MethodVersion you getBest for
Ubuntu's own packageThe one that was current when your Ubuntu release was made; often oldQuick tests; software that only needs "some" Node.js
NodeSource repositoryA current release line of your choice, updated with aptA production server running one application
NVMAny versions, side by side, per userDevelopment, and servers where versions change

Method 1: Ubuntu's package

sudo apt update
sudo apt install nodejs npm
node -v && npm -v

Simple and maintained by Ubuntu. Check the version it prints against what your application needs before going further.

NodeSource publishes Ubuntu packages of current Node.js versions. Its setup script adds the repository:

sudo apt install curl
curl -fsSL https://deb.nodesource.com/setup_lts.x -o nodesource_setup.sh
sudo -E bash nodesource_setup.sh
sudo apt install nodejs
node -v && npm -v

setup_lts.x selects the current long-term version; replace it with, for example, setup_22.x for a specific line. The package includes npm. Read a downloaded script before running it as root, which is why the lines above save it to a file first. Updates then arrive with the normal sudo apt upgrade.

Method 3: NVM

NVM installs Node.js into your home folder and switches versions per project. It is covered in its own guide: setting up NVM on Ubuntu. Keep in mind that services do not load NVM by themselves.

Tools for native modules

Some npm packages compile code during installation and fail with long errors if the tools are missing:

sudo apt install build-essential

Global packages without sudo

With methods 1 and 2, npm install -g writes into system folders and asks for root. Running npm as root is a bad habit, since package scripts then run as root too. Give your user a folder of its own:

mkdir -p ~/.npm-global
npm config set prefix ~/.npm-global
echo "export PATH=$HOME/.npm-global/bin:$PATH" >> ~/.profile
source ~/.profile

A first application

mkdir ~/hello && cd ~/hello
npm init -y
nano server.js
const http = require("http");
http.createServer((req, res) => res.end("Hello from Node.js")).listen(3000, "127.0.0.1");
node server.js
curl http://127.0.0.1:3000

Run the last line in a second terminal. The application listens on the server itself only, which is the right default.

Run it as a service

An application started in a terminal stops when you log out. Create a systemd unit so that it starts with the server and restarts if it crashes:

sudo nano /etc/systemd/system/hello.service
[Unit]
Description=Hello Node.js app
After=network.target
[Service]
User=www-data
WorkingDirectory=/home/youruser/hello
ExecStart=/usr/bin/node server.js
Restart=on-failure
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now hello
systemctl status hello

Use a user without login rights, and make sure it can read the folder. Logs go to the journal: journalctl -u hello -f. The process manager PM2 is a popular alternative that does the same from npm.

Put a web server in front

Do not expose a Node.js port to the internet directly. Let Nginx or Apache take requests on ports 80 and 443, handle HTTPS, and pass them to the application on 127.0.0.1: see Nginx with Let's Encrypt for the certificate part. Open only those two ports in the firewall: the Ubuntu firewall guide.

Problems

MessageCause
node: command not foundThe package is called nodejs; install it. With NVM, open a new terminal
Unsupported engine, or syntax errors in a dependencyThe Node.js version is too old for the package. Use method 2 or 3
EACCES on npm install -gSee "global packages without sudo"
node-gyp errorsInstall build-essential and Python 3
EADDRINUSEAnother process holds the port: sudo ss -tlnp | grep 3000
The app works by hand and not as a serviceWrong path to node, or the service user cannot read the folder

On Windows, the corresponding guide is 'node' is not recognized. Several services together: Docker Compose. For the server, see our Linux VPS plans.

Questions

How do I install Node.js on Ubuntu Server?

Run sudo apt install nodejs npm for Ubuntu's version, or add the NodeSource repository for a current one.

How do I get the latest LTS version of Node.js on Ubuntu?

Use the NodeSource setup script for setup_lts.x and then sudo apt install nodejs, or use NVM.

Is npm installed with Node.js?

With NodeSource and NVM, yes. With Ubuntu's own packages, install npm as well.

How do I keep a Node.js app running after logout?

Run it as a systemd service, or with a process manager such as PM2.