If you are using Node and NPM and need to upgrade Node to a newer version on Ubuntu, this tutorial will help you.
Please note that I am not talking about regular minor updates (node 16.x to 16.y) to your Node install. Like most software on Ubuntu, Node also gets minor updates along with the system update.
I am talking about major version upgrades here. For example, if you are using Node version 16.x and would like to upgrade it to version 18 or version 20.
It is unlikely that Ubuntu will provide a major new version upgrade to Node. This is why you should install Node directly from the official Nodesource repositories.
Node versions are a complicated mechanism. You should play attention to what version your Node application requires and what versions your distribution supports.
In this tutorial, I'll discuss these methods of Node version upgrade:
- Installed using NodeSource Repository (recommended)
- Update Node.js using the n package
- Use NVM to get new Node versions
Method 1: Update Node.js through NodeSource Repository
This is the method we used when we had to upgrade our Ghost CMS to a newer version that required using Node version 18 instead of version 16.
You should visit the NodeSource Repository and check for the supported versions for your current distribution.
Node provides a script for each active major version. You just have to replace YY
with the version you want in the following command:
curl -fsSL https://deb.nodesource.com/setup_YY.x | sudo -E bash -
Letβs say you have installed version 18 of Node.js using the NodeSource repository, and you want to update it to version 21.
To do it, just run:
curl -fsSL https://deb.nodesource.com/setup_21.x | sudo -E bash -
This will replace the sources file to point to version 21. Now, just update your system, to update the Node.js version to 21.
sudo apt update && sudo apt upgrade
To upgrade the NPM to the latest version:
sudo npm install -g npm@latest
Method 2: Upgrade Node.js through NPM and N package
For obvious reasons, most of us install the Node Package Manager (NPM), during the installation of Node.js. You can update the Node.js using NPM.
To achieve this, you need the n
package, which is available via NPM.
sudo npm install -g n
Now, to upgrade to an LTS version, you can use:
sudo n lts
Run node --version
to check the version. If the change is not reflected, close the terminal and reopen it. Similarly, use the command below to reflect the change in the current shell itself:
hash -r
To install the latest version of Node.js, run:
sudo n latest
You can change between the downloaded versions by making use of the n interactive selection.
sudo n
Scroll through the previously downloaded versions by you, select one, and press enter to make that version.
Thatβs it. You are good to go.
It is possible to remove all other versions except the currently installed version. Open a terminal and run:
sudo n prune
Method 3: Update using Node Version Manager (NVM)
To get the latest LTS version of node and migrate your existing installed packages, use:
nvm install --reinstall-packages-from=current 'lts/*'
You can look for the latest releases available using:
nvm ls-remote --lts
To use the version you installed, you can use:
nvm use --lts
Now, if you need the latest supported NPM version on the current Node version, you can run on a terminal:
nvm install-latest-npm
Conclusion
As you can see, the entire Node version is a complex structure. You need to approach is carefully. See which version you need, which version is supported by your Linux system and then upgrade it. Production servers should be backed up properly before you do such changes.
I hope you find this tutorial helpful. Let me know if you have any questions.