In this tutorial, we’ll discuss how to update Node.js and NPM to their latest versions, which were installed using npm.
First, we’ll look at the npm command approach to update NPM. Next, we’ll discuss the nvm command to perform a similar task. After that, we’ll explore the Snap package manager as a way to update Node.js. Furthermore, we’ll look into PPAs (Personal Packages Archives) for Node.js updates. Lastly, we’ll use binary packages to update the Node.js package to the latest version.
2. Using npm
In this section, we’ll learn how to use npm to update both Node.js and NPM. One pitfall of this method is the fact that it works only for packages installed via the npm command.
2.1. Using the update -g Command
To begin with, we can use the update command present in npm to update all packages to their latest versions:
$ npm update -g
In the above code, the command npm update functions as a directive within the NPM ecosystem.
Additionally, when we pair the update command with the -g flag, it installs the packages globally. Thus, executing this command ensures that system-wide Node.js packages are updated smoothly, integrating any available bug fixes, or new features.
Hence, using this approach helps us maintain a unified and up-to-date development environment across various projects.
2.2. Using the npm install Command to Update Node.js
Similarly, we can use the install command with npm to bring select packages up to their latest versions:
$ npm install -g n
The command sequence npm install -g n initiates the installation of the global Node.js package manager named n. Thus, we fetch and install the most recent stable release of Node.js.
2.3. Use the npm install Command to Update NPM
In addition, the same install command can be used to update the NPM package:
$ npm install npm
3. Using NVM
NVM stands for Node Version Manager. It’s a command-line tool for managing and switching between multiple Node.js versions on a system, making it ideal for working on diverse projects with varying version requirements.
There are several ways to update the Node.js and NPM packages using nvm. We’ll discuss some of the approaches in this section.
3.1. Using nvm install
In this method, we’ll use the install command of nvm to install or update to the latest version of Node.js:
$ nvm install node
Here, we install the last stable version of Node.js, ensuring access to the latest features and facilitating easy version management for project compatibility.
Additionally, we can also use the install command with a specific version of Node.js by specifying only the version number that we want to install:
$ nvm install 20.10.0
Here, we can replace the 20.10.0 with the desired version number.
3.2. Using the –lts Flag
In the same way, we can use the install command to update Node.js to its latest LTS (Long-Term Support) version:
$ nvm install --lts
The –lts flag in the above command designates the installation of the LTS version of Node.js. LTS versions prioritize stability and prolonged support, making them ideal for reliable production environments.
3.3. Using the ls-remote Command
The ls-remote command is a complementary operation when updating Node.js with nvm. It shows the versions currently available, so we can pick and deploy the one we want.
Now, let’s execute the command:
$ nvm ls-remote
The above command fetches and displays a list of available remote Node.js versions from the node repository. This enables us to see which versions are available for installation via nvm. Upon obtaining the list, we can choose and install a specific Node.js version for our projects.
4. Using snap
Another approach to update Node.js is to use the Snap software packaging and deployment system developed by Canonical.
Snap is a containerized package format for Linux, simplifying installation and ensuring consistency across distributions. It offers ease of use, isolation, and automatic updates for software deployment.
To update a package using snap, we include that particular package name with the snap command:
$ sudo snap install node --classic
The above command facilitates the installation of Node.js to the latest version on a Linux system by leveraging the Snap package manager. Furthermore, we use –classic to ensure system-wide installation of the node package.
Notably, we can only update packages using Snap if we installed it using the same package manager. In other words, if we install a package using NPM, we can’t update it using Snap as they’re distinct package managers with separate repositories.
5. Using PPAs
Alternatively, we can also update to the latest Node.js version by leveraging APT and the Node.js PPA.
First, we update the local repository entries via the apt command:
$ sudo apt update
After executing the above command, we should have access to the latest package URLs.
Now, let’s update Node.js and NPM:
$ sudo apt install -y nodejs npm
The command sudo apt install -y nodejs npm instructs the system to use apt with sudo privileges to install both Node.js and NPM. Specifically, the -y flag ensures that the packages are updated without requiring manual confirmation, making it suitable for automated processes or scripts.
After running this command, we should have Node.js and NPM installed and updated on our system.
Similar to Snap, we can only update the packages using PPAs if we have installed them using a PPA.
6. Using Binary Packages
Alternatively, updating Node.js is also possible using its binary packages. To do so, we first go to the official site Node.js downloads page and get the 32-bit or 64-bit Linux binary file URL.
Next, we use the wget command to get the latest node version:
$ wget https://nodejs.org/dist/v20.10.0/node-v20.10.0-linux-x64.tar.xz
Once we download the file using the wget command, we need a way to extract it.
To that end, we install xz-utils for binary package extraction:
$ sudo apt install xz-utils
After the installation of xz-utils, we use the tar command to extract and update to the latest Node.js version:
$ sudo tar -C /usr/local --strip-components 1 -xJf node-v20.10.0-linux-x64.tar.xz
The above command installs the latest node version on a Linux system. We use the tar utility to extract contents from a compressed .tar file.
In addition, the -C /usr/local option specifies the destination directory, while –strip-components 1 ensures the removal of the top-level directory, placing files directly into /usr/local.
Additionally, the -xJf flags denote the extraction operation, specifying the use of the xz algorithm for decompression:
- -x extracts files
- -J indicates that the archive is compressed using the xz compression format
- -f specifies the archive file to be processed
While we can apply this more manual method to almost any installation, it’s advisable to only update a binary archive installation this way.
In this article, we discussed how we can update Node.js and NPM to their latest versions in Linux.
First, we examined the npm method for updating node packages. Subsequently, we discussed the nvm command for updating node packages. Following that, we explored the Snap package approach for updates.
Moreover, we investigated the utilization of PPAs for updating Node.js and NPM. Finally, we used the binary packages to update the node packages to their most recent version.