Ubuntu how to upgrade Node.js
By Flavio
Published · Updated
Tagged: Node.js
Learn how to upgrade Node.js on Ubuntu with a current NodeSource LTS repository, while checking the setup script before running it as root.
I was running a Node service on Ubuntu and I was stuck on an old Node.js version.
For a server, first choose a supported Node.js release. I normally use the current LTS line rather than the newest Current release.
Check what you have now
Before you change anything, find out which version runs and where it comes from:
node --version
which node
apt policy nodejs
which node tells you how Node.js was installed, and that decides how you upgrade it:
/usr/bin/nodemeans it came from an apt package, either Ubuntu’s ownnodejspackage or NodeSource. Follow the steps below.- A path inside your home folder, like
~/.nvm/versions/node/..., means a version manager installed it. Upgrade it with that tool (see the nvm section at the end). The apt steps won’t change thenodeyou run.
apt policy nodejs shows the installed version, the version apt would install (the “Candidate”), and the repository each one comes from. If you only see Ubuntu’s repositories there, you’re on the nodejs package that ships with Ubuntu. That one is usually an older release line, which is why I use NodeSource.
Upgrade to a new major version with NodeSource
At the time of this update, Node.js 26 is the LTS line. Download the NodeSource setup script, inspect it, then run it:
curl -fsSL https://deb.nodesource.com/setup_26.x -o /tmp/nodesource_setup.sh
less /tmp/nodesource_setup.sh
sudo -E bash /tmp/nodesource_setup.sh
sudo apt install nodejs
The script doesn’t install Node.js itself. It adds the NodeSource apt repository for that release line, and apt install nodejs then installs (or upgrades to) the version from it.
Some details from reading the script, useful when something looks off:
- It deletes any older NodeSource repository file first (
/etc/apt/sources.list.d/nodesource.listornodesource.sources), so running thesetup_26.xscript on a machine that hadsetup_22.xswitches the line. You don’t end up with two NodeSource repositories. - It writes a pin file at
/etc/apt/preferences.d/nodejsthat gives NodeSource priority 600. Ubuntu’s own repositories have the default priority of 500, so apt picks the NodeSource package even when Ubuntu also has anodejspackage. - It only supports
amd64andarm64. On another architecture it stops with an “Unsupported architecture” error.
Verify both executables after the upgrade:
node --version
npm --version
The NodeSource nodejs package already includes npm, so don’t install the separate npm package from Ubuntu’s repositories.
Do not guess the version number. Check the Node.js releases page and NodeSource’s current installation instructions first. Old release lines stop receiving security fixes.
Get the latest patch release of your current line
If you’re already on the NodeSource repository and only want the newest 26.x release, you don’t need the setup script again. A normal apt upgrade does it:
sudo apt update
sudo apt install --only-upgrade nodejs
--only-upgrade upgrades the package if it’s installed and does nothing if it isn’t. A full sudo apt upgrade also picks up Node.js updates, together with everything else on the system.
After the upgrade
A running Node.js process keeps using the old binary until it restarts. Restart your service so it picks up the new version. With systemd that’s:
sudo systemctl restart myapp
where myapp is the name of your service. If you use pm2, restart your apps through it.
A new major version can also break packages with native addons, the ones that compile C or C++ code during npm install. When that happens the app crashes at startup with an error saying the module “was compiled against a different Node.js version using NODE_MODULE_VERSION”. Rebuild them inside the project folder:
npm rebuild
Deleting node_modules and running npm install again works too.
Remove the NodeSource version
To go back to Ubuntu’s package, or to clean up before switching to a version manager, remove the package and the files the setup script created:
sudo apt purge nodejs
sudo rm -f /etc/apt/sources.list.d/nodesource.sources /etc/apt/sources.list.d/nodesource.list
sudo rm -f /usr/share/keyrings/nodesource.gpg
sudo rm -f /etc/apt/preferences.d/nodejs /etc/apt/preferences.d/nsolid
sudo apt update
Use nvm instead
If you only need Node.js for development on your own machine, a version manager is usually more convenient because it lets projects use different Node.js versions without changing the system package.
With nvm, you install it for your user (check the README for the current install command), then install the latest LTS and bring your global npm packages along:
nvm install --reinstall-packages-from=current 'lts/*'
nvm alias default 'lts/*'
nvm alias default makes new shells start with that version. nvm installs Node.js in your home folder, so sudo and system services like systemd won’t see it unless you point them at the full path. That’s the main reason I stick with the apt package on servers.
To figure out which Node.js version you actually need for a given feature, I built a free Node versions tool.
Want me to talk about your product? You can sponsor this site.
Related posts about node: