How To Install Node.js on CentOS 7, Ubuntu 16.04 & Debian 9

2

Node.js is an open source, cross-platform JavaScript runtime environment for developing network tools and web applications. Many of Node.ja modules are written in JavaScript, which simplifies the development of applications using Node.js.

Node.js nothing but a combination of the Runtime environment and JavaScript modules. Node.js interprets JavaScript using Google’s V8 JavaScript engine, and it was developed by Ryan Dahl in 2009.

This guide helps you to install Node.js on CentOS 7, Ubuntu 16.04 & Debian 9.

Node.js Versions

The are two versions of Node.js is available for the users. Check out the official page to get the latest version of Node.js.

  • v10.x (Long Term Supported)
  • v11.x (Current Latest Version)

Install Build Toos (Optional)

You may need to install build tools If you want to compile and install native add-ons from npm.

### CentOS 7 ###

yum groupinstall -y 'Development Tools'

### Ubuntu 16.04 / Debian 9 ###

sudo apt-get install -y build-essential

Configure Node.js PPA

NodeSource maintains Node.js binary packages for CentOS, Ubuntu and Debian operating systems. You can choose either one of the Node.js version to install on your machine.

# Node.js v10.x #

### CentOS 7 ###

yum -y install -y curl
curl -sL https://rpm.nodesource.com/setup_10.x | bash -

### Ubuntu 16.04 / Debian 9 ###

sudo apt-get update && sudo apt-get install -y curl
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -

# Node.js v11.x #

### CentOS 7 ###

yum -y install -y curl
curl -sL https://rpm.nodesource.com/setup_11.x | bash -

### Ubuntu 16.04 / Debian 9 ###

sudo apt-get update && sudo apt-get install -y curl
curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -

Install Node.js

Once after adding the PPA on the server, install the Node.js by using the following command. NPM (Node Package Manager) will also be installed along with Node.js.

### CentOS 7 ###

yum install -y nodejs

### Ubuntu 16.04 / Debian 9 ###

sudo apt-get install -y nodejs

Check the version of Node.js using the following command.

### CentOS 7 ###

node -v

### Ubuntu 16.04 / Debian 9 ###

nodejs -v

Output:

v10.15.3

Check the npm version.

npm -v

Output:

6.4.1

Create Test Web Server

If you want to test your Node.js installation, create a web server and run with Node.js.

Let’s create a file called web_server.js.

vi web_server.js

Place the below content into the web_server.js file.

const http = require('http');
const port = 8080;
const server = http.createServer((req, res) => {
   res.writeHead(200, {'Content-Type': 'text/plain'});
   res.end('Hello World\n');
});
server.listen(port, () => {
  console.log(`Server running at http://your-ip-add-ress:${port}/`);
});

Now, start the web server using the below command.

### CentOS 7 ###

node --inspect web_server.js

### Ubuntu 16.04 / Debian 9 ###

nodejs --inspect web_server.js

You should get the following message on your terminal.

Debugger listening on ws://127.0.0.1:9229/11ff05c0-b3cd-4f94-bbab-53a656d8c9e5
For help, see: https://nodejs.org/en/docs/inspector
Server running at http://your-ip-add-ress:8080/

This confirms that the web server has been started and listening on port 8080.

Firewall (CentOS 7)

Run the following commands in the terminal to allow port 8080 in the firewall.

firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload

Open your web browser and navigate it to the following web address.

http://your-ip-add-ress:8080

You should get the web page like below.

Install Node.js on CentOS 7
Install Node.js on CentOS 7

Conclusion

That’s all. You have successfully installed Node.js on CentOS 7, Ubuntu 16.04 & Debian 9.

You might also like