Knowledge Base Hub

Browse through our helpful how-to guides to get the fastest solutions to your technical issues.

Home  >  How-Tos  >  How to Install Node.js on Ubuntu 16.04
Top Scroll

How to Install Node.js on Ubuntu 16.04

 7 min

Introduction

Node.js is the server-side JavaScript runtime platform that performs JS code outside of the browser. Also, it is the open-source platform for JavaScript programming that allows developers to create server-side tools and applications.

In this guide, we’ll show you about installing Node.js on an Ubuntu 16.04 server.

Prerequisites

This guide presumes that you are using Ubuntu 16.04. Before you start, you should have a non-root user account with sudo rights set up on your system.

How to Install the Distro-Stable Version for Ubuntu?

Ubuntu 16.04 contains a Node.js version within its default repositories that could be used to simply give a constant experience over various systems. The version in the repositories is v4.2.6 at the time of writing. It’s not the latest version, but it should be completely stable and enough for fast experimentation with the language.

So as to get this version, we simply need to use the apt package manager. At first, we have to refresh our local package index and after that install from the repositories:

sudo apt-get update
sudo apt-get install nodejs

If the package in the repositories suits your requirements, this is all you have to do to get set up with Node.js. Much of the time, you’ll also need to also install npm, which is the Node.js package manager. You can do this by writing:

sudo apt-get install npm

The executable from the Ubuntu repositories is called nodejs instead of node because of a conflict with another package. Just keep this in mind as you are running the software.

Type the following line to check Node.js version you have installed:

nodejs -v

Once you have set up Node.js version you have installed from the Ubuntu repositories, you can choose whether or not you might want to work with different versions, package archives, or version managers. Next, we’ll talk about these elements along with more flexible and robust methods of installation.

Pick A Node Js Hosting Server Plans for Better Deployment!

How to Install Using a PPA (Personal Package Archive)?

An option that can get you a more current version of Node.js is to add a PPA (Personal Package Archive) managed by NodeSource. This will have more recent versions of Node.js than the official Ubuntu repositories and allows you to select between Node.js v4.x, Node.js v6.x and Node.js v8.x (the current LTS version, supported till December of 2019).

Initially, you have to install the PPA so as to get access to its contents. Ensure you are in your home directory and utilise the curl to recover the installation script for your selected version, ensuring to replace 8.x with your preferred version string (if different):

cd ~
curl -sL https://deb.nodesource.com/setup_8.x -o nodesource_setup.sh

You can inspect the contents of this script with nano (or your text editor would you like to use):

nano nodesource_setup.sh

And run the script under sudo:

sudo bash nodesource_setup.sh

The PPA will be added to your configuration and your local cache is going to be updated automatically. Once running the setup script from nodesource, you’ll install the Node.js package similarly as you did above:

sudo apt-get install nodejs

Type the following line to check Node.js version you have installed after these initial steps:

nodejs –v
 
Output
v8.10.0

The nodejs package includes the nodejs binary and npm, so it’s not necessary to install npm separately.

npm utilizes a configuration file into your home directory to maintain track of updates. It will be built the first time you run npm. Execute the following command to check that npm is installed and to create the configuration file:

npm -v
 
Output
5.6.0

This will help some npm packages to work (for example, those that need compiling code from source), you have to install the build-essential package:

sudo apt-get install build-essential

You now have the essential tools to work with npm packages that need compiling code from source.

How to Install Using NVM?

An option to installing Node.js by apt is to utilise particularly designed tool named nvm (Node.js Version Manager). Somewhat working at the level of operating system, nvm works at the independent directory level within your home directory. It means you can install multiple, independent versions of Node.js without affecting the whole system.

Managing your environment with nvm permits you to access the most recent versions of Node.js and retain and manage previous releases. It’s a separate utility from apt-get, and therefore the versions of Node.js that you just manage by it are distinct from the distro-stable version of Node.js accessible from the Ubuntu repositories.

To start off, we’ll require getting the software packages from our Ubuntu repositories which will permit us to create source packages. The nvm script can leverage these tools to create mandatory components:

sudo apt-get update
sudo apt-get install build-essential libssl-dev

You can pull down the nvm installation script from the project’s GitHub page when the prerequisite packages are installed. The version can be different, but, usually, you’ll download it with curl:

curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh -o install_nvm.sh

And check the installation script with nano:

nano install_nvm.sh

Run the script with bash:

bash install_nvm.sh

It will install the software in the subdirectory of your home directory at ~/.nvm as well as added the important lines to your ~/.profile file to use the file.

To get access to the nvm functionality, you have to log out and log in again, or you can source the ~/.profile file so that your current session is aware of regarding the changes:

source ~/.profile

Now you have nvm installed. Also, you can install remote Node.js versions.

You can run the following command to find out the versions of Node.js that are available for installation:

nvm ls-remote
Output
8.5.0
v8.6.0
v8.7.0
v8.8.0
v8.8.1
v8.9.0
v8.9.1
v8.9.2
v8.9.3
->v8.9.4 (Latest LTS: Carbon)

You can see, the recent LTS version at the time of this writing is v8.9.4. You can install the latest version by typing:

nvm install 8.9.4

Generally, nvm will change to use the most newly installed version. You can clearly tell nvm to use the version we just downloaded by typing:

nvm use 8.9.4

While you install Node.js using nvm, the executable is named node. You can see the version used by the shell at present, type:

node -v
 
Output
v8.9.4

If you have manifold Node.js versions, you can get what is installed by typing:

nvm ls

If you want to default one of the versions, type:

nvm alias default 8.9.4

This version will be automatically selected while a new session is created. You can also refer to it by the alias like this:

nvm use default

Each version of Node.js could keep a track of its individual packages and has npm ready to manage these.

By using the normal format, you can have npm install packages to the Node.js project’s . /node_modules directory. E.g., for the express module:

npm install express

You can add the -g flag if you wish to install it globally. Also, you can make it accessible to other projects by using the same Node.js version.

npm install -g express

This will install the package in:

~/.nvm/node_version/lib/node_modules/package_name

Installing globally will allow you to run the commands from the command line, but you will have to link the package into your local sphere to need it from within a program:

npm link express

Type the following line to get more help about the options available to you with nvm.

nvm help

 

Removing Node.js

You can uninstall Node.js using apt-get or nvm, depending on the version you need to target. You have to work with the apt-get service at the system level to remove the distro-stable version.

Execute the following command to remove the distro-stable version:

sudo apt-get remove nodejs

This command will remove the package and hold the configuration files. These can be of use to you if you think to repeat the installation of the package at a later point. If you don’t require saving the configuration files for later use, then run the following:

sudo apt-get purge nodejs

This will uninstall the package and remove the configuration files linked with it.
At last step, you can remove any unused packages that were automatically installed with the removed package:

sudo apt-get autoremove

To uninstall a version of Node.js that you have enabled using nvm, first decide whether or not the version you’d wish to remove is the current active version:

nvm current

If you are target version is not the current active version, you can execute:

nvm uninstall node_version

This command will uninstall the version of Node.js which you have selected.

If you want to remove the version which is the current active version, you should deactivate nvm first to enable your changes.

nvm deactivate

Using the above command, you can now uninstall the current version. This version will remove all files linked with the targeted version of Node.js excluding the cached files that can be used for reinstallation.

Conclusion

As you’ll see, these are the few ways to get Node.js up and running on your Ubuntu 16.04. Depending on your circumstances, you will know which one is the best way for you. Whereas the prepackaged version in Ubuntu’s repository is the simplest, the nvm method is clearly rather more versatile.

 

For our Knowledge Base visitors only
Get 10% OFF on Hosting
Special Offer!
30
MINS
59
SECS
Claim the discount before it’s too late. Use the coupon code:
STORYSAVER
Note: Copy the coupon code and apply it on checkout.