Knowledge

How to install Node.js on Ubuntu

#Development

Modern PHP sites still need Node.js to build their frontend assets. Here is how to install Node.js on Ubuntu with nvm, so you can switch versions per project without touching the system.

Published by Mark van Eijk on June 30, 2026 · 1 minute read

  1. Install nvm
  2. Install Node.js
  3. Switch versions per project
  4. Building assets on deploy
  5. The alternative: NodeSource
  6. Let Rocketeers handle it

Even a PHP application usually needs Node.js — to compile CSS, bundle JavaScript with Vite or webpack, and run the asset build step during deployment. The cleanest way to install it is nvm, the Node Version Manager, which keeps Node in your home directory so different projects can run different versions without sudo.

Install nvm

Run the official install script (pin it to a specific nvm version so the install is reproducible):

curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash

The script appends the loader to your shell profile. Either open a new shell or load it into the current one:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"

Confirm it's available:

nvm --version

Install Node.js

Install the version you want and set it as the default for new shells:

nvm install 22
nvm alias default 22

Check both Node and npm came along:

node -v
npm -v

Switch versions per project

The whole point of nvm is that one server can run several Node versions. Install another and switch with a single command:

nvm install 20
nvm use 20

Drop a .nvmrc file containing a version number (for example 22) in a project's root and nvm use will pick it up automatically — handy for keeping a deploy script on the right version.

Building assets on deploy

In a deployment you'll typically install dependencies and run a build:

npm ci
npm run build

Use npm ci rather than npm install on a server — it's faster and installs exactly what's in the lockfile. The difference matters more than it looks; see npm ci vs npm install.

The alternative: NodeSource

If you'd rather install Node system-wide (every user, no per-shell loading), the NodeSource apt repository is the common alternative. nvm wins when you need multiple versions or want to avoid sudo; NodeSource wins for a single fixed version on a dedicated box.

Let Rocketeers handle it

Installing nvm, loading it in the right shell profile, pinning versions per project, and making sure the deploy step runs with the correct Node version is one more moving part to keep aligned. Rocketeers installs Node.js during provisioning and runs your asset build as part of every deployment — so the frontend is compiled and live without a separate setup.

Subscribe to our newsletter

Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!