Install a Node.js and Launch a Node App on Ubuntu 18.04
In this article, we would like to discuss “how to install Node.js on Ubuntu 18.04”. This method can be performed on Dedicated Server/Bare metal and Virtual Private Servers or Virtual machines like ServerStadium Cloud.
However, this article will guide you on installing Node.JS on Virtual Machine with Ubuntu 18.04 installed.
Node.js is a JavaScript platform for general-purpose programming that allows users to build network applications quickly. By leveraging JavaScript on both the front and backend, Node.js makes development more consistent and integrated.
Prerequisites
Before we begin the tutorial, the following are the stack versions that we are going to install:
- OS: Ubuntu 18.04
- Latest Node.js stable version for Ubuntu 18.04
- Latest NPM
Install Node.js
apt update
apt install nodejs
By default, from upstream, it will install Node.js 8.10.0 version
Please type “y” to continue the installation process.
nodejs -v
Or, If you would like to install with another or specific Node.js version do you prefer you can use the following steps
Node.js v14.x:
# Using Ubuntu curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - sudo apt-get install -y nodejs
Node.js v12.x:
# Using Ubuntu curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash - sudo apt-get install -y nodejs
Node.js v10.x:
# Using Ubuntu curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash - sudo apt-get install -y nodejs
Node.js LTS (v12.x):
# Using Ubuntu curl -sL https://deb.nodesource.com/setup_lts.x | sudo -E bash - sudo apt-get install -y nodejs # Using Debian, as root curl -sL https://deb.nodesource.com/setup_lts.x | bash - apt-get install -y nodejs
Node.js Current (v14.x):
# Using Ubuntu curl -sL https://deb.nodesource.com/setup_current.x | sudo -E bash - sudo apt-get install -y nodejs
Install NPM
Please run the following command to install NPM:
apt install npm
Many packages and libs will be required to be installed, and you can type “y” to continue the NPM installation. By default, from upstream, it will install NPM 3.5.2 version.
Create Demo and Test Script
After installing Node.js and NPM, we test whether it was installed properly. We can create a test file and append the following simple test script:
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(3000, "0.0.0.0"); console.log('Server running at http://your_ip_address:3000/');
Please pay attention to the following syntax; please change with your Public IP that’s accessible from public and browser:
http://your_ip_address:3000/
Please run the script that was just created by using the following command:
node server.js
Open your browser and copy the following URL, then paste it into your browser. You will see:
http://your_ip_address:3000/
If you see the above message. Congratulation!! You have done the setup Node.JS on your Server.