How to install NodeJS and run node applications on Ubuntu 16
NodeJS is a open source runtime environment for Javascript server-side and networking applications that is designed to be flexible and easily scalable. NodeJS comes with a package manager known as NPM and can be installed via its own version manager, NVM, which can even manage multiple node versions.
In this tutorial, we will cover the setup of NodeJS on an Ubuntu instance and the writing of a basic NodeJS application.
Getting Started
To complete this guide, you will need the following:
• 1 Node (Cloud Server or Dedicated Server) running Ubuntu 16.
Install should be made as root, however we do not recommend running NodeJS applications in production as root. You may create an user to run the applications from to increase security on your infrastructure.
Updating Ubuntu
Make sure your Ubuntu instance is up to date by running the following commands as root or with superuser privileges. To begin, fetch the latest information available about new versions of packages and their dependencies:
sudo apt-get update
With the new information, install the latest versions of packages currently installed on the system. Enter y for yes when prompted and wait for completion:
sudo apt-get upgrade
Install NodeJS
There are several possibilities for how to install NodeJS on your system. Make sure you have updated your packages before proceeding (see Updating Ubuntu section).
Option 1: Beginner Method with apt-get
Install stable (but not necessarily the latest) releases of NodeJS and NPM from Ubuntu’s own repositories:
sudo apt-get install nodejs
sudo apt-get install npm
Option 2: Advanced Method with NVM
NVM is the Node Version Manager and can be used to install NodeJS and NPM.
NVM requires a c++ compiler as well as the version control system Git. Install these NVM dependencies with source packages from the Ubuntu repositories if they are not already present:
apt-get install build-essential libssl-dev
apt-get install git
Check what the latest NVM version released is from the NVM GitHub project page. Fetch and execute the NVM install script below, replacing the version number with your desired or latest version:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.2/install.sh | bash
For the NVM setup to be complete, either restart the terminal, switch users, or refresh the profile. Change user using this command, replacing USER2 with your second user:
su USER2
Input the second user’s password, then exit to return to the first user:
exit
You can also simply refresh the profile instead of switching users:
source ~/.profile
Verify NVM is installed and list all versions of NodeJS that are available:
nvm --version
nvm ls-remote
The output of the second command will contain quite a few versions of NodeJS (basically each and every release). As of the time of this guide, the current latest version is 6.2.2. We recommend you opt for this version unless your application requires an older deprecated version.
Expected output:
v0.1.14
v0.1.15
v0.1.16
...
Install your desired version of NodeJS from the NVM list output:
nvm install 6.2.2
Confirm the installation succeeded by checking all installed versions of NodeJS on your system:
nvm ls
The output should be similar to this:
v6.2.1
-> v6.2.2
node -> stable (-> v6.2.2) (default)
stable -> 6.2 (-> v6.2.2) (default)
iojs -> iojs- (-> N/A) (default)
Activate an installed NodeJS version with:
nvm use 6.2.2
Verify that your desired version of NodeJS is activated:
nvm current
Deploying a Basic NodeJS Application
For the purposes of this guide, we will write a small NodeJS script using the express library to spawn a quick web server with some text.
Create a folder for your NodeJS application and navigate inside it:
mkdir my_first_node_app
cd my_first_node_app
Create your NodeJS script using the terminal text editor vi:
vi hello.js
Within this new file, insert the following code:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.send('Globo.Tech says Hello!\n');
})
var server = app.listen(8080, function () {
console.log(‘Server is up and running’)
})
Save the file and exit with Esc > :wq!
Install the Express library inside your application folder to satisfy dependencies:
npm install express --save
Confirm your application works by checking for the following console message after running this command:
curl http://localhost:8080
You should see “Globo.Tech says Hello!” in the console.
This default app is basically a web server so you can visit the URL and port configured and see the text specified in the script as a webpage.
Run your newly created NodeJS application (this command won’t exit until you quit it):
node hello.js
Then, navigate to the following url in the browser to see your app:
If you see “Globo.Tech says Hello!” your NodeJS setup works properly.
Conclusion
Congratulations! Now you know how to use NodeJS and run basic node applications. If this guide was helpful to you, kindly share it with others who may also be interested.