Introduction
Node.js is a free, open-source and cross-platform Javascript run-time environment to execute server-side Javascript code. It can be used to develop intensive and dynamic web applications such as video streaming sites and single-page applications. This tutorial will guide you on how to install Node.js on Ubuntu 16.04.
Prerequisites
- Ubuntu 16.04 server with a non-root user
This tutorial assumes that you are running an Ubuntu 16.04 server that has a non-root user account with sudo
privileges to administer your server.
Preliminary Step
First, log in to your server as your non-root user. Before doing anything else, let's update the package database:
sudo apt update && sudo apt upgrade -y
There are a few methods that can be used to install Node.js on Ubuntu. We shall look at two methods.
Method 1: Installing Bundled Node.js for Ubuntu
You will usually need npm
as well, so to install Node.js and npm
via Ubuntu default depositories, execute:
sudo apt install nodejs npm
Once the installation is completed, you may want to verify the version of Node.js installed:
nodejs -v
[secondary_label output]
v4.2.6
You can also check the version of npm
installed:
npm -v
[secondary_label output]
3.5.2
<$>[note]
Note: If you use Method 1, you will use the command nodejs
instead of node
. This is because there is a conflict with another package name in Ubuntu.
<$>
Method 2: Installing Latest Node.js 8.x on Ubuntu
Instead of installing from Ubuntu default depositories, you may want to install the latest version of Node.js. To do that, you need to add the PPA (Personal Package Archive) maintained by NodeSource for Ubuntu.
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
After adding the PPA, you can proceed to install Node.js which also includes npm
when you install using this method:
sudo apt install -y nodejs
After the installation is completed, you can verify the version of Node.js installed:
node -v
[secondary_label output]
v8.1.3
You can also check the version of npm
installed:
npm -v
[secondary_label output]
5.0.3
For Method 2, in order to allow some npm
packages to work (like compiling code from source), you need to install the build-essential
package:
sudo apt install build-essential
Method 3: Installing Node.js Using NVM
If you require to install multiple, self-contained versions of Node.js, then you need to use the nvm
(Node.js version manager) method. Using the nvm
, you can install and select which Node.js version to use.
Firstly, we need to install some requisite packages from the Ubuntu repositories in order for nvm
to work:
sudo apt install build-essential libssl-dev
Once completed, we want to download and execute the nvm
installation script from the project's GitHub page. The current version is <^>v0.33.2<^>, which you may want to substitute with the latest version by checking out their GitHub page.
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
The script will create and clone the nvm
repository into ~/.nvm
directory inside your home directory. It will also add source lines into your profile (either ~/.bashrc
, ~/.bash_profile
or ~/.profile
).
After the nvm
installation is completed, you need to log out from your Ubuntu and log in again in order for nvm
to work properly.
Next, we want to find out what versions of Node.js are available for installation:
nvm ls-remote
[secondary_label output]
. . .
v8.0.0
v8.1.0
v8.1.1
v8.1.2
v8.1.3
v8.1.4
The latest version at the time of writing this tutorial is v8.1.4. You may see newer versions instead. To install Node.js v8.1.4, enter:
nvm install <^>8.1.4<^>
You may replace 8.1.4
with whichever version number you require. You can also repeat installing other versions as you wish.
By default, nvm
will use the most recently installed version. If you want to switch to another version, for example, 8.0.0
, use the following command:
nvm use <^>8.0.0<^>
To check which versions have been installed, enter:
nvm ls
You can make any installation as the default version. For instance, if you want to make 8.0.0
as the default version:
nvm alias default <^>8.0.0<^>
You can also select the default version by executing:
nvm use default
Optional: Create A Demo Node.js Application
To test your Node.js install, you can create a demo Node.js application to display "Hello World" text. First create a new file:
nano helloworld.js
Then add the code below. Remember to replace SERVER_IP_ADDRESS
with your server's IP address for all the following steps. You may also want to change 8080
to another port number of your choice:
```helloworld.js_file
[label helloworld.js]
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(<^>8080<^>, '<^>SERVER_IP_ADDRESS<^>');
console.log('Server running at http://<^>SERVER_IP_ADDRESS<^>:<^>8080<^>/');
Save the file and exit. To test your application, run this `node` command:
```command
node helloworld.js
[secondary_label output]
Server running at http://<^>SERVER_IP_ADDRESS<^>:<^>8080<^>/
Start your web browser and enter:
http://<^>SERVER_IP_ADDRESS<^>:<^>8080<^>
You should see Hello World
in your browser window.
To stop your Node.js application, just press CTRL + C
on your server terminal.
Conclusion
We have seen three methods to install Node.js on your Ubuntu server. While Method 1 is the easiest, you may want to choose Method 2 if you want to get the latest version of Node.js. If you need to run multiple versions of Node.js, then you need to use Method 3 to install.
Related Tutorial