How to install NodeJS on Linux

NodeJS Is an open source, cross-platform, lightweight JavaScript runtime environment that can be used to build scalable network applications. Fast and efficient server-side software built on Chrome’s V8 JavaScript engine. Initially, JavaScript was primarily used for client-side scripting. However, Nodejs makes JavaScript available for server-side scripting and executes the script on the server side to generate dynamic web pages. Another notable thing is that Nodejs has npmA package manager that installs and manages nodejs libraries and applications. The Nodejs package ecosystem is the world’s largest open source library ecosystem. This tutorial describes how to install Node.js on popular Linux operating systems such as Arch Linux, Debian, Ubuntu, RHEL, CentOS, Fedora, etc.

Install NodeJS on Linux

There are quite a few ways to install Nodejs. Here are three methods listed.

The first method is recommended for installing Nodejs. In particular, First method that is The best way to avoid permission issues While installing the package globally

The second way is for those who want to use a stable NodeJS version.

And the third and final method describes how to install Nodejs from source. This is suitable for those who want to get the latest nodejs version.

1. Install Nodejs using NVM (recommended method)

This is the recommended way to install Nodejs. Furthermore, The best way to avoid permissions issues. NVM (Node song VEshon Manager) is a bash script used to manage multiple Node.js versions. This allows you to install and uninstall node.js and switch from one version to another. The good thing is that you can use NVM to install it on any version of Node.js you choose.

Use the latest install script to install nvm from here:

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.1/install.sh | bash

Or

$ wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.1/install.sh | bash

The above command will clone the nvm repository ~ / .Nvm Add source lines to the profile (~ / .bash_profile, ~ / .zshrc, ~ / .profile, or ~ / .bashrc).

Here is a sample output:

% Total % Received % Xferd Average Speed Time Time Time Current
 Dload Upload Total Spent Left Speed
100 12818 100 12818 0 0 11566 0 0:00:01 0:00:01 --:--:-- 11579
=> Downloading nvm as script to '/home/ostechnix/.nvm'

=> Appending nvm source string to /home/ostechnix/.bashrc
=> Appending bash_completion source string to /home/ostechnix/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" # This loads nvm bash_completion

Restart terminal Once before starting to use NVM. If you are using SSH, end your current session and log in again.

To check if NVM is installed, do the following:

$ command -v nvm

Sample output:

nvm

If installation is successful, “nvm” is output.

You can now install Nodejs and npm.

First, run the following command to see a list of available Nodejs versions:

$ nvm ls-remote

Sample output:

[...]
v11.12.0
v11.13.0
v11.14.0
v11.15.0
v12.0.0
v12.1.0
v12.2.0
v12.3.0
v12.3.1
v12.4.0
v12.5.0
v12.6.0
v12.7.0
v12.8.0
v12.8.1
v12.9.0
v12.9.1
v12.10.0
v12.11.0
v12.11.1
v12.12.0
v12.13.0   (LTS: Erbium)
v12.13.1   (Latest LTS: Erbium)
v13.0.0
v13.0.1
v13.1.0
v13.2.0

To install / update the latest version of Nodejs:

$ nvm install node

Sample output:

Downloading and installing node v13.2.0...
Downloading https://nodejs.org/dist/v13.2.0/node-v13.2.0-linux-x64.tar.xz...
############################################################################################# 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v13.2.0 (npm v6.13.1)
Creating default alias: default -> node (-> v13.2.0)

At the time of creating / updating this guide, the latest version v13.2.0.

Install a specific node version

You can also install a specific version of your choice, such as v9.3.0, as follows:

$ nvm install v9.3.0

Sample output:

Downloading and installing node v9.3.0...
Downloading https://nodejs.org/dist/v9.3.0/node-v9.3.0-linux-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v9.3.0 (npm v5.6.0)
Creating default alias: default -> v9.3.0

Similarly, you can install as many versions as you want.

To view a list of installed Nodejs versions:

$ nvm list

Sample output:

->      v13.2.0
default -> node (-> v13.2.0)
node -> stable (-> v13.2.0) (default)
stable -> 13.2 (-> v13.2.0) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/erbium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.16.2 (-> N/A)
lts/dubnium -> v10.17.0 (-> N/A)
lts/erbium -> v12.13.1 (-> N/A)

As you can see in the output above, I installed v13.2.0.

Check the installed node version

To view the currently installed default Nodejs version:

$ node --version

Or

$ node -v

Sample output:

v13.2.0

Check the npm version.

$ npm -v

Sample output:

6.13.1

Switching between different node versions

If you have multiple versions installed, you can switch between different Nodejs versions as follows:

$ nvm use node

Or you can do it more specifically.

$ nvm run node v9.3.0

Set default node version

To set a specific Nodejs version as the default:

$ nvm alias default v9.3.0

Here is a sample output:

default -> v9.3.0

Delete node

Before deleting a Node, first check if the version you are trying to delete is the currently active version using the following command:

$ nvm current

If it is not the currently active version, remove it using the following command:

$ nvm uninstall 

Example:

$ nvm uninstall v9.3.0

If you try to delete the currently active version using a command:

$ nvm uninstall node

An error similar to the following is displayed:

nvm: Cannot uninstall currently-active node version, v13.2.0 (inferred from node).

You must first deactivate nvm using a command.

$ nvm deactivate

Then try uninstalling the node:

$ nvm uninstall node

Sample output:

Uninstalled node v13.2.0

2. Install Nodejs using your distribution’s package manager (stable but old version)

Nodejs is available in the default repository of most Linux distributions. It may not be the latest version, but it is stable. If you want to use stable Node.js on Linux, we recommend that you install it using your distribution’s package manager, as shown below.

On Arch Linux Install derivatives such as Antergos and Manajaro Linux by running the following command:

$ sudo pacman -S nodejs npm

For Debian, Ubuntu, Linux Mint:

$ sudo apt-get install nodejs npm

RHEL, CentOS, There is a need Enable EPEL First the repository.

$ sudo yum install epel-release

Then use the command to install Nodejs.

$ sudo yum install nodejs npm

For Fedora:

$ sudo dnf install nodejs npm

Note: The package in the default repository is out of date, so when trying to install a NodeJS module using npm, you get the following error:

/usr/bin/env: ‘node’: No such file or directory

To resolve this error, you need to create a symbolic link as shown below.

$ sudo ln -s /usr/bin/nodejs /usr/bin/node

3. Install Nodejs from NodeSource

As mentioned, nodejs is available in the default repository, but it may be a bit outdated. To install the latest version, install the latest version from NodeSource as shown below.

For Ubuntu and derivatives:

Add a Nodejs 13.x repository.

$ curl -sL https://deb.nodesource.com/setup_13.x | sudo -E bash -

If you need the Nodejs 12.x version, do the following:

$ curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -

For Nodejs 11.x:

$ curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -

For Nodejs 10.x:

$ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -

For Nodejs 8.x:

$ curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -

After adding the repository, install Node.js using the following command:

$ sudo apt-get install nodejs npm

For RHEL, CentOS:

NodeJS 13.x:

# curl -sL https://rpm.nodesource.com/setup_13.x | bash -

NodeJS 12.x

# curl -sL https://rpm.nodesource.com/setup_12.x | bash -

NodeJS 11.x

# curl -sL https://rpm.nodesource.com/setup_11.x | bash -

NodeJS 10.x

# curl -sL https://rpm.nodesource.com/setup_10.x | bash -

NodeJS 8.x

# curl -sL https://rpm.nodesource.com/setup_8.x | bash -

Next, install Nodejs root:

# yum install nodejs npm

For Fedora, follow the same steps as above.

Install build tool (optional)

To compile and install native add-ons from the npm repository, you also need to install the build tools.

To install the build tools on Debian and Ubuntu distributions, execute the following command:

$ sudo apt-get install -y build-essential

For RHEL based systems:

$ sudo yum groupinstall 'Development Tools'

For Fedora:

$ sudo dnf groupinstall 'Development Tools'

Recommended reading:

  • How to manage NodeJS packages using Npm
  • Ndm-Desktop GUI application for NPM
  • Install MEAN.JS stack on Ubuntu

And that’s it. You now know how to install Nodejs on your Linux distribution. As you can see, installing Nodejs is very easy. Anyone can install and set up Nodejs within minutes.

resource:

.

Sidebar