Thursday, July 20, 2017

How To Install Node.js On Linux

How To Install Node.js On Linux

Node.js is an open source, cross-platform, and lightweight JavaScript run-time environment that is used to build scalable network applications. It is fast and efficient server-side software built on Chrome’s V8 JavaScript engine. As you may already know, initially, JavaScript was primarily used for client-side scripting. But, Node.js enables JavaScript to be used for server-side scripting, and runs scripts server-side to produce dynamic web pages. Another notable thing is Node.js has a command-line utility called npm, a package manager to install, manage nodejs libraries and applications. Node.js’ package ecosystem is the largest ecosystem of open source libraries in the world.

In this tutorial, we will see how to install Node.js on major Linux operating systems including Arch Linux, Debian, Ubuntu, RHEL, CentOS, Fedora etc.

Install Nodejs on Linux

There are quite a few ways to install Nodejs. I will explain all of them.

Install Nodejs using your distribution’s package manager

Node.js is available in the default repositories of most Linux distributions. It might not be latest version, but stable. If you want to have a stable Node.js on your Linux, you better install it using your distribution’s package manager as shown below.
On Arch Linux and its derivatives like Antergos, Manajaro Linux, run the following command to install it:
sudo pacman -S nodejs npm
On Debian, Ubuntu, Linux Mint:
sudo apt-get install nodejs npm
On RHEL, CentOS, you need to enable EPEL repository first.
sudo yum install epel-release
And, then install Nodejs using command:
sudo yum install nodejs npm
On Fedora:
sudo dnf install nodejs npm

Install Nodejs from NodeSource

Even though, nodejs is available in the default repositories, it might be bit outdated. To use the most recent version, install the latest version from NodeSource as shown below.
On Debian, Ubuntu distributions:
Add Nodejs 6.x repository:
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
For Nodejs 7.x:
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
Then, install Node.js using command:
sudo apt-get install nodejs npm
On RHEL, CentOS:
Run the following command as root user to add Node.js v6 LTS repository:
curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -
To add Node.js v7 repository:
curl --silent --location https://rpm.nodesource.com/setup_7.x | bash -
To add Node.js 0.10 repository:
curl --silent --location https://rpm.nodesource.com/setup | bash -
Then install, as root:
yum install nodejs npm
For Fedora, follow the same instructions above.
Install build tools (Optional)
To compile and install native addons from npm repository, you may also need to install build tools.
To install build tools on Debian, Ubuntu distributions, run the following command:
sudo apt-get install -y build-essential
On RHEL based systems:
sudo yum groupinstall 'Development Tools'
On Fedora:
sudo dnf groupinstall 'Development Tools'

Install Nodejs using NVM

Alternatively, you can install Nodejs using NVM (Node Version Manager). It is a bash script used to manage multiple released Node.js versions. Using NVM, you can install any available Node.js version of your choice. NVM allows us to install, uninstall node.js, and switch from one version to another.
To install nvm, run the following command as root user:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
The above command will clone the nvm repository to ~/.nvm and add the source line to your profile (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).
Then, source the .bash_profile as root user:
source ~/.bash_profile
NVM has been installed now. To verify whether NVM is installed or not, run:
command -v nvm
Sample output:
nvm
It should output ‘nvm’ if the installation was successful.
Now, we can install Nodejs and npm.
First, run the following command to view the list of available Nodejs versions:
nvm ls-remote
Sample output:
 [...]
 v7.0.0
 v7.1.0
 v7.2.0
 v7.2.1
 v7.3.0
 v7.4.0
 v7.5.0
 v7.6.0
 v7.7.0
 v7.7.1
 v7.7.2
 v7.7.3
 v7.7.4
 v7.8.0
 v7.9.0
To install the most recent Nodejs version, just run:
nvm install node
Alternatively, you can run the following to install any Nodejs version of your choice.
For example, to install Nodejs v7.9.0, run:
nvm install v7.9.0
Sample output:
Downloading and installing node v7.9.0...
Downloading https://nodejs.org/dist/v7.9.0/node-v7.9.0-linux-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v7.9.0 (npm v4.2.0)
Creating default alias: default -> v7.9.0
Similarly, you can install any number of versions you want.
To view the list of installed Nodejs versions, run:
nvm list
Sample output:
-> v7.8.0
 v7.9.0
default -> v7.9.0
node -> stable (-> v7.9.0) (default)
stable -> 7.9 (-> v7.9.0) (default)
iojs -> N/A (default)
lts/* -> lts/boron (-> N/A)
lts/argon -> v4.8.2 (-> N/A)
lts/boron -> v6.10.2 (-> N/A)
As you see in the above output, I have installed two versions v7.8.0 and v7.9.0. And, v7.9.0 is the default version, and v7.8.0 is the currently active version.
You can also switch between different Nodejs versions as below.
nvm use node
Or you can just run it to be more specific:
nvm run node v7.9.0
To set a particular Nodejs version as the default, run:
nvm alias default v7.9.0
Sample output would be:
default -> v7.9.0
To remove the particular Nodejs version, run:
nvm uninstall v7.9.0
Sample output:
Uninstalled node v7.9.0
To view the currently installed and default Nodejs version, run:
node --version
Or,
node -v
Sample output:
v7.8.0
Check npm version:
npm -v
Sample output:
4.2.0

And, that’s all. You know now how to install Nodejs on your Linux distribution. As you can see, installing Nodejs is fairly easy. Anyone can install and setup the Nodejs within few minutes.
Cheers!
Resources:

No comments: