;

How to install NodeJS and run node applications on Ubuntu 14

Try it in our public cloud & Get $5 Credit
CLAIM NOW

How to install NodeJS and run node applications on Ubuntu 14

NodeJS is a single-threaded, asynchronous runtime for server-side JavaScript-based applications. By being lightweight, easy to install, and simple to scale, Node is gaining popularity as a fast and powerful platform for internet-facing apps, from API backends to microservices and beyond. Ubuntu 14.04 is a stable Linux distribution with years of support remaining. If you’re looking for a great platform for your next high-performance app, then it is hard to go wrong with the combination of Node and Ubuntu 14.04 LTS. In this guide, we’ll present two options for installing Node, and will also get you up and running with a simple app to show just how easy developing with this combination of technologies can be.

Getting Started

You’ll need the following in place before getting started with this guide:
• 1 Node (Cloud Server or Dedicated Server) running Ubuntu 14.
• Root access

You’ll be installing everything as root, though we recommend running Node applications under separate user accounts. If your app is compromised and is running with root access, the attacker will have complete access to your server.

Tutorial

Start by ensuring that your Ubuntu installation is up-to-date. Here we’ll update the package cache, and will apply all available bugfixes and security updates. You should perform this step regularly to keep your system updated and running securely.

apt-get update && apt-get upgrade -y

There are a few ways to go about installing Node. The easiest method involves installing the version shipped with Ubuntu’s default package repository. This will leave you with a stable version of Node, though the release available through Ubuntu may lag significantly behind the most recent official Node version.

apt-get install nodejs npm -y

Node is now installed, though unfortunately Debian and Ubuntu use “nodejs” as the official binary name. This is incompatible with how many developers expect Node to be installed. As such, many Node-based scripts will fail if you do not create a symlink from “node” to “nodejs.” We’ll do that now to improve compatibility.

ln -s `which nodejs` /usr/local/bin/node

Another more advanced way to install Node is via the Node Version Manager. NVM lets you pick from all available Node versions, install multiple versions simultaneously, and switch between them dynamically as needed.

Start by installing NVM’s dependencies. These are needed to compile any binary modules, and to build the Node releases themselves.

apt-get install build-essential libssl-dev

NVM ships an installation script that performs all necessary setup. Here we retrieve it from GitHub and execute it from the shell.

cd /root
wget https://raw.githubusercontent.com/creationix/nvm/v0.31.2/install.sh
bash install.sh

The installation process modified your shell startup scripts. To continue, you can either source them directly or log out and back in. This will update your path, in addition to setting up NVM’s necessary support for switching between Node versions.

We can now use NVM to get a list of every single Node version ever released.

nvm ls-remote

As you can see, you have quite a few versions from which to choose. At the time of this writing, the latest version is 6.2.2. We recommend choosing that, or whatever the most recent release is, unless your app or framework specifically requires something older.

(....)
v4.4.2
v4.4.3
v4.4.4
v4.4.5
v4.4.6
v4.4.7
v5.0.0
v5.1.0
v5.1.1
v5.2.0
v5.3.0
v5.4.0
v5.4.1
v5.5.0
v5.6.0
v5.7.0
v5.7.1
v5.8.0
v5.9.0
v5.9.1
v5.10.0
v5.10.1
v5.11.0
v5.11.1
v5.12.0
v6.0.0
v6.1.0
v6.2.0
v6.2.1
v6.2.2

Using a version of Node via NVM involves two steps. First you’ll need to install the desired version. This step will download or compile the necessary binaries, and must only be performed once per version of Node you are installing.

nvm install 6.2.2

Let’s make sure the installation succeeded by listing the versions of Node that NVM has installed.

nvm ls

You should see the following output:

-> v6.2.2
default -> 6.2.2 (-> v6.2.2)
node -> stable (-> v6.2.2) (default)
stable -> 6.2 (-> v6.2.2) (default)
iojs -> N/A (default)

The next step is activating the version you wish to use. Activating a release makes that version the default against which any Node commands are run.

nvm use 6.2.2

Having done this, let’s verify that the new version is active:

node -v
v6.2.2

Node is now installed. To show just how easy working with Node can be, we’ll next build a web server based on the popular Express web framework. Start by creating a directory to contain the application and its dependencies.

mkdir /home/nodeapp
cd /home/nodeapp

Now create the script itself.

nano nodetest.js

Your script should contain the following code:

!/usr/bin/env node
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Globo.Tech says Hello!');
});
app.listen(6915, function () {
console.log('Basic NodeJS app listening on port 6915.');
});

Save and exit. Next we need to install the Express framework, along with all of its dependencies.

npm install express

Run your newly-created script as follows:

node /home/nodeapp/nodetest.js

If everything worked, you’ll see a message on the console indicating that the server is listening. If you don’t, ensure that the contents of your file matches the above code exactly.

node nodetest.js
Basic NodeJS app listening on port 6915.

Given that we have setup an environment path in the beginning of our script, we can make it executable to run it without specifying “node” in front of the script.

chmod +x /rhome/nodeapp/nodetest.js
./nodetest.js

To confirm that everything worked, visit the URL and port shown in the console message. If you see “Globo.Tech says Hello!” your NodeJS setup is working properly.

Conclusion

You now have a sophisticated development platform for building complex, scalable services and back-ends. If you know anyone needing to install Node on their own Ubuntu server, be sure to do them a favor by letting them know about this article.