NodeJS refers to the JavaScript runtime environment that helps in web development and the creation of network applications. It is an open-source cross-platform mainly designed for the execution of JavaScript outside the client/user web browser. All the Linux distributions come with the “NodeJS” application by default.
This post pens down various possible methods to install NodeJS on Linux:
- Method 1: Using the Ubuntu Repository
- Method 2: Using Node Source Repository
- Method 3: Install NodeJS Using Node Version Manager
Method 1: Install NodeJS Using the Ubuntu Repository
The most simple and straightforward method is to install a “NodeJS” application using the default repository of Linux distributions. The below-defined steps will help to perform this task efficiently.
Step 1: Update System Packages
First, update the specified system software packages using their default package manager with the help of these commands:
$ sudo dnf check-update #For Fedora
$ sudo yum update #For CentOS/RHEL
$ sudo apt update #For Ubuntu/Debian-Based
Step 2: Install NodeJS
When all the system packages are updated install the “NodeJS” application by typing the below-mentioned command as per distributions:
$ sudo dnf install nodejs #For Fedora
$ sudo yum install nodejs #For CentOS/RHEL
$ sudo apt install nodejs #For Ubuntu/Debian-Based
The above command successfully installed the “NodeJS” packages and its essential tools.
Step 3: Verify the Node JS
Verify the “NodeJS” installation by checking its available installed version in the following way:
$ node --version
It is verified that “NodeJS” with its latest “v12.22.9” release is present in the default repository.
Method 2: Install NodeJS Using Node Source Repository
The “NodeJS” application contains an “APT” repository that maintains a variety of versions. This method provides a few necessary steps to install(Specific/latest) “NodeJS” release via its source repository.
Step 1: Add Node Source Repository
Download and execute the “curl” installation script with the help of the “curl” command line tool having superuser privileges i.e “sudo”:
For Fedora/RHEL/CentOS:
$ curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
For Debian/Ubuntu:
$ curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
Step 2: Install NodeJS
Install “NodeJS” again using the default package managers according to the specified Linux distributions:
$ sudo dnf install nodejs #For Fedora
$ sudo yum install nodejs #For CentOS/RHEL
$ sudo apt install nodejs #For Ubuntu/Debian-based
The “NodeJS” with its recommended library “npm(node package manager)” has been installed.
Step 3: Confirm the NodeJS
Check out the “NodeJS” and the “npm” installed version utilizing this command:
$ node --version
It is confirmed that “NodeJS” latest release “v18.14.0” and “npm” having “9.3.1” has been installed.
Method 3: Install NodeJS Using Node Version Manager
Another method is the “NVM” tool to handle the “NodeJS” application i.e installing/uninstalling its versions as per requirements.
Step 1: Download NVM Script
Download the “nvm” installation script and also install it by executing the “curl/wget” command line tool in this way:
$ curl -sL https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.0/install.sh -o install_nvm.sh
The above command is executed successfully.
Step 2: Run the NVM Script
Once installation is done, run the “nvm script”. This will create the project “nvm” repository clone and update the user profile (~/bash_profile, ~/.bashrc or “~/.zshrc”):
$ bash install_nvm.sh
To make the changes effective in the user profile it is recommended to update/reload the “bashrc” source file manually:
$ source ~/.bashrc
Step 3: View NodeJS Available Versions
Now, list down all the “NodeJS” versions that can be installed easily via the “nvm” script:
$ nvm ls-remote
Step 4: Install the Latest NodeJS Release
The above output shows that “v18.14.0” is the latest LTS “NodeJS” application. Install it in one shot via “nvm” followed by the “–lts(latest)” flag:
$ nvm install --lts
Step 5: Verify the Latest NodeJS
In last, use the “–version” command to verify the “NodeJS” latest installation release:
$ node --version
It is confirmed that the latest “NodeJS” release “v18.14.0” has been successfully installed.
How to Remove NodeJS Via Default Package Manager?
After usage of “NodeJS”, it can be removed from the system without any hassle using the below-mentioned commands:
$ sudo dnf remove nodejs #For Fedora
$ sudo yum remove nodejs #For CentOS/RHEL
$ sudo apt remove nodejs #For Ubuntu/Debian-Based
The “NodeJS” has been removed now.
How to Remove NodeJS Via Source Repository?
Execute the following typed command for the removal of the installed “NodeJS” application:
$ sudo dnf remove nodejs #For Fedora
$ sudo yum remove nodejs #For CentOS/RHEL
$ sudo apt purge nodejs #For Ubuntu/Debian-Based
Now, delete the node source repository and its related content using the “rm(remove)” command followed by “-r(recursively)” flag:
$ sudo rm -r /etc/apt/sources.list.d/nodesource.list
The “node source repository” has been removed completely.
How to Remove NodeJS Via NVM?
First, deactivate/unload the “nvm” using its “deactivate” command that will clear out the path variables:
$ nvm deactivate
After that uninstall the installed “NodeJS” release “v18.14.0” using the “nvm” “uninstall” command in this way:
$ nvm uninstall v18.14.0
The “NodeJS v18.14.0” has been completely uninstalled/removed from the Linux system.
Conclusion
On Linux and its major distros, “NodeJS” can be installed using “Default Repository”, “Node Source Repository”, and the “Node Version Manager”. The “source repository” and “version manager” allows the user to install the “NodeJS” version as per requirements. This guide has illustrated all possible aspects to install NodeJS on Linux.