Initech
KB-10 Print

How to Update Node.js and npm on Ubuntu

Quick answer: how you update depends on how Node.js was installed. If it came from the NodeSource repository, sudo apt update && sudo apt upgrade nodejs brings it current. If you use nvm, nvm install --lts gets the newest long-term-support release. To update npm itself, run npm install -g npm@latest. Details and pitfalls below.

First: find out what you have

node -v
npm -v
which node

The path from which node tells you the installation method:

  • /usr/bin/node means a system package (Ubuntu's own repo or NodeSource).
  • A path under ~/.nvm/ means nvm manages it.
  • A path under /snap/ means the snap package.

One warning before anything else: the version of Node.js in Ubuntu's default repositories is often far behind the current releases. If node -v shows something ancient and you installed it with a plain apt install nodejs long ago, the clean fix is to move to NodeSource or nvm rather than to chase updates that will never arrive.

Option 1: NodeSource repository (system-wide install)

NodeSource publishes current Node.js builds as apt packages. Pick the major version line you want on their site, then run their setup script and install:

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

From then on, ordinary system updates keep Node.js current within that major line:

sudo apt update
sudo apt upgrade nodejs

Moving to a newer major line later means running the matching setup script once, then apt install nodejs again. NodeSource bundles npm with the package, so both update together.

Choose this route for servers: one system-wide Node.js that services, cron jobs and every user account see identically.

Option 2: nvm (per-user, multiple versions)

nvm (Node Version Manager) installs Node.js inside your home directory and lets several versions coexist. Install it with the script from the nvm repository, reopen your shell, then:

nvm install --lts      # newest long-term-support release
nvm ls                 # everything installed locally
nvm use --lts          # switch this shell to it
nvm alias default lts/*   # make it the default for new shells

Updating later is the same command again: nvm install --lts fetches the newest LTS and you point default at it. Old versions stay until you nvm uninstall them, which makes rolling back trivial.

Choose this route for development machines, or whenever different projects need different Node.js versions. One caveat for servers: nvm lives in one user's shell profile, so system services started outside that shell will not see it unless you point them at the full binary path.

Updating npm itself

npm releases move faster than Node.js releases, so a current Node.js can still carry an older npm. Update it globally:

npm install -g npm@latest

Under nvm this updates the npm belonging to the currently active Node.js version only, which is exactly what you want. Under a NodeSource install, global packages live in a system directory, so the command needs root once:

sudo npm install -g npm@latest

The sudo npm trap

If you find yourself typing sudo npm install -g for everyday packages, stop and fix the cause instead. Running npm as root for regular global packages leads to root-owned files inside your own npm cache and, over time, to the classic EACCES permission errors. Two clean ways out:

  • Use nvm. Everything lives in your home directory and no global install ever needs sudo.
  • Move npm's global prefix into your home directory: npm config set prefix ~/.npm-global, then add ~/.npm-global/bin to your PATH.

If the damage is already done, reclaim ownership of the cache with sudo chown -R $(whoami) ~/.npm and installs behave again.

If Node.js came from a snap or the stock Ubuntu repo

The snap updates itself on the channel you chose (snap refresh node forces it), but snap confinement causes odd behavior with some native build tools, so most server setups avoid it. If your Node.js is the stock Ubuntu package and you are moving to NodeSource, remove the old one first so apt does not juggle two sources for the same binary:

sudo apt remove nodejs libnode-dev
sudo apt autoremove

Then follow the NodeSource steps above. Globally installed packages do not survive the move, so reinstall the handful you actually use; a quick npm ls -g --depth=0 before removing gives you the shopping list.

Verify

node -v
npm -v
npm doctor

npm doctor checks the registry connection, PATH sanity and cache permissions in one pass. If both version numbers look right and doctor reports no errors, you are done: applications restart on the new runtime the next time their process restarts, so remember to restart long-running services after an upgrade.

Was this answer helpful?
Related Articles