How to install NodeJS on Ubuntu 18.04

NodeJS has quickly become an extremely popular technology for hosting dynamic applications on the internet. It allows the functionality and power of JavaScript to run as a back-end language, opening the doors to an ever-expanding world of programming options. In this tutorial, we’ll show you how to install NodeJS and get started with a NodeJS App on your VPS running Ubuntu 18.04.

Prerequisites for installing NodeJS on Ubuntu

This tutorial will assume the following:

  • Ubuntu 18.04 server (although desktop should still work)
  • Non-root user
  • General understanding of Command-Line tools

Let’s Get Started

To begin, let’s make sure the operating system is up-to-date.

sudo apt update && sudo apt upgrade -y

If you get presented with any GUI-style prompts, the defaults are fine 99% of the time.

Next, we’ll want to install NodeJS. The best way to install depends on what you want to accomplish, but I’ll cover two of the most common installations.

Install Using Apt

Ubuntu 18.04 apt repositories include a relatively recent version of NodeJS that can be installed using apt directly.

sudo apt install -y nodejs

This will also install npm, the Node Package Manager. npm is used to install node modules, frameworks, apps, etc. You can now test the install by passing the version flag.

node -v

Testing npm‘s version as well:

npm -v

This is enough to begin working with NodeJS. But you may want to have a newer version available or have more control over the version in general– and that’s where the node version manager comes into play.

Installing using Node Version Manager

If you prefer to have more control over the version installed, and even in use at any given time Node Version Manager (nvm as referenced in commands) provides that control.

Let’s check out that method.

First, we have to actually install nvm. The best way to install nvm is from the source. You can find the latest install commands for nvm here, which can be used to both install and update your nvm instance.

curl -o- | bash

The install script will run, asking you to close and reopen your terminal to enable nvm. Do so, after which you can check to make sure the command works, and see what nvm version is currently installed.

nvm --version

I highly encourage you to read through the help text of nvm (run nvm --help), as it provides functionality that many will find extraordinarily useful. However, for now, let’s set up the latest version of NodeJS.

nvm install --lts

This is a simple command to tell nvm to install the latest version of the current LTS (long-term support) version of NodeJS. If you have not previously used nvm to install a NodeJS version, this command will furthermore set up the “default” alias to this version. If you wish to install a specific version, you can specify the version you desire, as well.

nvm install 10.10.0

Once you have the version you want to be installed, use nvm to switch to that version as the actively used version.

nvm use 10.10.0 
Now using node v10.10.0 (npm v6.4.1)


nvm use default
Now using node v10.16.1 (npm v6.9.0)

Getting Started using NodeJS

Fantastic! We’re on our way, now!

If you’ve made it this far, you’ve likely already got an idea for what you’re planning to use NodeJS for, so get to it! Your NodeJS framework is ready to run your app.

Here’s a demo “hello world” app, as displayed on the NodeJS getting started page. Replace SERVERIPADDRESS with your server’s public-facing IP Address, or if you’re running this on a local desktop version of Ubuntu 18.04, you can use “” as depicted on the getting started guide.

const http = require('http');

const hostname = 'SERVER_IP_ADDRESS';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);

Save this as app.js in a folder on your NodeJS host, then run:

node app.js
Server running at http://SERVER_IP_ADDRESS:3000/

You should now be able to navigate to your VPS IP Address on port 3000 and view the text displayed.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Create a bootable USB stick on Ubuntu

Install or upgrade Ubuntu Test out the Ubuntu desktop experience without touching your PC...

Apply kernel patches without rebooting

1. Overview The Canonical Livepatch Service applies critical kernel security patches without...

Install and Configure Samba

1. Overview A Samba file server enables file sharing across different operating systems over a...

Configure SSH to use two-factor authentication

1. Overview SSH, the secure shell, is often used to access remote Linux systems. Because we...

How to Install ONLYOFFICE on Ubuntu 18.04

1. INTRODUCTION ONLYOFFICE is an open source office suite that includes online editors and the...