How to Install Node.js on Ubuntu, CentOS?
Procedure to install Node.js 11.x, 12.x, 14.x on Ubuntu 16.x/18.x, CentOS 7.x/8.x through binary distribution or from the source.
Node.js popularity is growing faster than ever. If you recently started learning Nodejs development, then one of the first things you need to do is to install them.
Technically, there are multiple ways to get things installed but following the easy and right process will make life much easier.
The following, I’ve tested on the DigitalOcean server. Let’s get it started.
Ubuntu 16.x or 18.x
The latest version of Nodejs is not available through the default repository. But not to worry, you can use NodeSource distribution as the following.
- Log in to the server as root
- Execute the following
To install Node.js 11.x
curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
To install Node.js 12.x
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
To install Node.js 14.x
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
The above will download and install the NodeSource Node.js repository. At the end of the above output, you should see something like this.
Reading package lists... Done
## Run `sudo apt-get install -y nodejs` to install Node.js 11.x and npm
## You may also need development tools to build native addons:
sudo apt-get install gcc g++ make
## To install the Yarn package manager, run:
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn
- Next, you will have to install the nodejs with the following command.
apt-get install -y nodejs
It will take a few seconds and once done; you should be able to verify the version.
root@geekflarelab:~# nodejs -v
v11.7.0
root@geekflarelab:~#
And, as you can see, it has installed 11.7.0 version.
CentOS/RHEL 7.x or 8.x
First, you need to install the NodeSource repository with the following command.
Install Nodejs 11.x
curl -sL https://rpm.nodesource.com/setup_11.x | bash -
Install Nodejs 12.x
curl -sL https://rpm.nodesource.com/setup_12.x | bash -
Install Nodejs 14.x
curl -sL https://rpm.nodesource.com/setup_14.x | bash -
And then, install the Nodejs as below.
yum install -y nodejs
If you are using CentOS 8.x then you may also try DNF.
dnf install -y nodejs
It will take a few seconds, and in the end, you should see something like below.
Running transaction
Preparing : 1/1
Installing : python3-setuptools-39.2.0-5.el8.noarch 1/4
Installing : python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64 2/4
Running scriptlet: python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64 2/4
Installing : python3-pip-9.0.3-16.el8.noarch 3/4
Running scriptlet: nodejs-2:14.9.0-1nodesource.x86_64 4/4
Installing : nodejs-2:14.9.0-1nodesource.x86_64 4/4
Running scriptlet: nodejs-2:14.9.0-1nodesource.x86_64 4/4
Verifying : python3-pip-9.0.3-16.el8.noarch 1/4
Verifying : python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64 2/4
Verifying : python3-setuptools-39.2.0-5.el8.noarch 3/4
Verifying : nodejs-2:14.9.0-1nodesource.x86_64 4/4
Installed:
nodejs-2:14.9.0-1nodesource.x86_64 python3-pip-9.0.3-16.el8.noarch python3-setuptools-39.2.0-5.el8.noarch python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64
Complete!
[root@lab ~]#
This means the Node.js is installed and can be verified with -v
syntax.
[root@geekflarelab ~]# node -v
v11.7.0
[root@geekflarelab ~]#
The above instruction should also work for Fedora 29 or later.
Installing from Source Code
What if you are in a DMZ environment where you can’t connect to the Internet? You still can install it by building the source code. The procedure is not as easy as above through binary distribution but doable.
- Login to Ubuntu or CentOS server
- Download the latest or the one you want from here using
wget
. I am trying the latest one.
wget https://nodejs.org/dist/v11.7.0/node-v11.7.0.tar.gz
- Extract the downloaded file with tar command
tar -xvf node-v11.7.0.tar.gz
- You should see a new folder created in the present working directory
drwxr-xr-x 9 502 501 4096 Jan 17 21:27 node-v11.7.0
- Go to the newly created folder
cd node-v11.7.0/
And, its time to build the Node.js from source now.
But, before you proceed, ensure you have the pre-requisites installed.
If using Ubuntu then install the pre-requisites with below
apt-get update
apt-get install gcc g++ clang make
and for CentOS
yum update
yum install gcc clang gcc-c++
- Let’s built it now
./configure
- Ensure no error and then next
make
make install
It will take some time to build and once done; you can verify the installed version by executing below.
root@geekflarelab:~# node --version
v11.7.0
root@geekflarelab:~#
You see, installing Nodejs is easy.
Next, you may want to explore the Nodejs framework to become a professional programmer.