NodeJS is a programming language that corresponds to the JavaScript runtime environment. It is generally used in web development and the creation of network applications. It provides an open-source cross-platform to execute the JavaScript programs outside the client/user web browser.
It comes with its latest release after every six months. At this time, its latest LTS stable release is “v18.14.2”. It is recommended to update the NodeJS to its latest release to enhance its functionality and fix the bugs and issues.
This post pens down possible methods to update the NodeJS version in Ubuntu using the command line.
- Method 1: Using Binary Packages
- Method 2: Using Node Version Manager(NVM)
- Method 3: Using Node Package Manager(NPM)
Prerequisites: Remove the Already Installed Version
Following the steps of guidelines are required before the updation of NodeJs in Ubuntu.
Step 1: Check Current NodeJs Version
Execute the “–version” command to check the currently installed version of the “NodeJS” package in Ubuntu:
$ node --version
The installed “NodeJS” version is “v12.22.9”.
Step 2: Remove Older NodeJS
It is necessary to remove the older version of the NodeJS application that is currently installed in the Ubuntu system.
To perform this task, use the default package manager “apt” with “autoremove (remove dependencies)” and “purge (remove configuration files)” in this way:
$ sudo apt autoremove --purge nodejs
The “nodejs v12.22.9” has been completely removed now.
Let’s install NodeJS’ latest or updated version using the following methods.
Method 1: Using Binary Packages
In Linux, the “Binary Files” contains the executable files of packages from the source code. It hides the built-in source code of every package. It is the most recommended way to install the latest NodeJS release.
Step 1: Download Binary Package
Access the official website of “NodeJS” using the provided link https://nodejs.org/en/download/. The NodeJS website looks like this:
Copy the “Linux Binaries(x64)” URL as per system requirement and download it in the system using the “wget” command:
$ wget https://nodejs.org/dist/v18.14.2/node-v18.14.2-linux-x64.tar.xz
The “node-v18.14.2” binary package has been downloaded in the present working “home” directory.
Step 2: Extract the Binaries
The download NodeJS binary package is compressed and has the “.xz” extension. Extract it in the “/usr/local” directory and install the latest NodeJS using this command:
$ sudo tar -C /usr/local --strip-components 1 -xJf node-v18.14.2-linux-x64.tar.xz
- The “-C” flag of “tar” is used to specify the location where NodeJS binary package will be extracted.
- The “strip-component” specifies the leading component from the compressed files before its extraction.
- The “x(extract)”, “j(for “.xz” extension)”, and “f(denotes file)” extract the specified binary package:
The specified node-v18.14.2 package has been extracted into the “/usr/local” directory and also installed successfully.
Step 3: Confirm Updated NodeJS
Now, verify the installed version of NodeJS using the “–version” command in the Ubuntu terminal:
$ node --version
The output confirms that NodeJS’s latest release, “v18.14.2” has been installed successfully.
Method 2: Using Node Package Manager(NVM)
The “NVM” tool provides the different versions of the “NodeJS” application that can be easily installed/uninstalled as per requirements. It effectively manages the node versions for each project.
Step 1: Download NVM Installation Script
Run the “curl” command line tool to download the “nvm” installation script in the Ubuntu system:
$ curl -sL https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.0/install.sh -o install_nvm.sh
Step 2: Run the NVM Script
Execute the “nvm script” in the “bash” shell when its installation is completed. It will update the user profile (~/bash_profile, ~/.bashrc, or “~/.zshrc”) and create the project “nvm” repository clone:
$ bash install_nvm.sh
Reload the Source File
Update/reload the “bashrc” source file manually for making the changes effective in the user profile:
$ source ~/.bashrc
The “~/.bashrc” file has been updated.
Step 3: View NodeJS Available Versions
Check all the available versions of “NodeJS” that can be installed easily via the “nvm” script:
$ nvm ls-remote
The latest LTS(Long Term Support) version of “NodeJS” in “nvm” is “v18.14.2”.
Step 4: Install the Latest NodeJS Release
Install the updated “v18.14.2” “NodeJS” release in Ubuntu via “nvm” followed by the “–lts(latest)” flag:
$ nvm install --lts
The installation of the latest “NodeJS” has been completed.
Step 5: Verify the Latest NodeJS
Again, check the “NodeJS” version for the verification of its latest release:
$ node --version
It is confirmed that the latest “NodeJS” release “v18.14.2”, has been successfully installed.
Method 3: Using Node Package Manager(NPM)
The “npm” stands for “Node Package Manager”, which is the widely used package manager for installing JavaScript software projects, including the NodeJS tool. It comes with NodeJS by default.
Step 1: Install “n” Module
First download the “n module” to the default location $HOME/n that is necessary for “npm”. It will also automatically install the latest LTS NodeJS version:
$ curl -L https://bit.ly/n-install | bash
The NodeJS version manager, along with the latest LTS NodeJS release, has been installed successfully.
Step 2: Install the Latest NodeJS (not stable)
The “node-v18.14.2” is the stable LTS release. However, if the user wants to install the latest release, then use the “latest” with “n module” in this way:
$ sudo n latest
Step 3: Verify the Latest NodeJS
The “version” command verifies that the latest release of NodeJS has been successfully installed:
$ node --version
The latest “node-v19.7.0” has been installed but this version is not stable.
Conclusion
In Ubuntu, to update the NodeJs versions to its latest release, use the “Binary Packages”,
“NVM(Node Version Manager)” and “NPM(Node Package Manager)” tools. The “NVM” and “NPM” command line utilities come pre-installed with NodeJS.
This post has illustrated all possible methods to update the NodeJS version in Ubuntu using the command line.