How to Install Node.js on Ubuntu 16.04
In this tutorial we will teach you how to start using Node.js on an Ubuntu 16.04 server
This tutorial relies on you using Ubuntu 16.04. First, make sure you have a non-root account with sudo privileges set up on your system.
How to install the Distro-Stable version for Ubuntu
Ubuntu 16.04 has a version of Node.js in its default repositories which could be used to be easily provided with a consistent experience across several systems. This may not be the latest version for you, however, it is quite stable and will be enough for quick experimentation with the language.
To obtain this version, you will need to use the apt package manager. You must refresh your local package index first and, afterwards, install from the repositories.
sudo apt-get update
sudo apt-get install nodejs
If the package in the repositories fits your needs, that is all you will have to do to be set up with Node.js in many cases; you will also want to install ‘npm’, since it is the Node.js package manager. You can do this by using the command below.
sudo apt-get install npm
This should allow you to install modules easily along with packages that you can use with Node.js
Since there is a conflict with another package, the executable from the Ubuntu repositories is named ‘nodejs’ instead of node, you must make sure that you are aware of this when running software.
Now, we will talk about more flexible and robust methods of installation.
How To Install Using a PPA
A different way to get a more recent version of Node.js is to add a PPA (personal package archive) maintained by NodeSource. This should have a more up-to-date version of Node.js that is separate to the official Ubuntu repository, it also allows you to select between Node.js v4.x (a very old long-term support version, supported until April of 2017), v6.x (the more recent LTS version, is going to be supported until April of 2018), and Node.js v7.x (the latest actively developed version).
Now, you will have to install the PPA so that you can get access to its contents. Ensure you are in your home directory and then, using ‘curl’, be provided with the installation script for your preferred version, be sure to replace 6.x with the correct version string.
You could inspect the contents of this script using ‘nano’ or your preferred text editor.
Next, execute the script under sudo:
sudo bash nodesource_setup.sh
Now you will have PPA added to your configuration and your local package cache should update immediately. Afterwards, once you’re done running the setup script from ‘nodesource’, you may start installing the ‘Node.js’ package in the same way you did before.
sudo apt-get install nodejs
The ‘nodejs’ package has the ‘nodejs’ binary within it, including ‘npm’ as well, so you won’t have to install npm separately. Though, in order to get certain npm packages to work, like the ones which need code compiling from source, you will have to install the build-essential package.
sudo apt-get install build-essential
How To Install Using NVM
A different way for installing Node.js with apt is to use a specially designed tool called ‘nvm’; this stands for ‘Node.js version manager’.
With nvm, you can install several, self-contained versions of Node.js and it will allow you to control your environment in a simpler way. It will grant you on-demand access to the newest versions of Node.js and it will also give you the option to get previous releases that your app might depend on.
To begin, you will have to get the software packages from your Ubuntu repositories which should allow you to build source packages. The nvm script will leverage these tools to build the needed components:
sudo apt-get updatesudo apt-get install build-essential libssl-devAfter the prerequisite packages are installed, you may pull down the nvm installation script from the project’s GitHub page. The version number might not be the same but, nevertheless, you can download it with curl.
Now inspect the installation script using Nano.
And execute the script with bash.
This should install the software into a sub directory of your home directory at ‘~/.nvm’, it should also add the needed lines to your ‘~/.profile’ file to use the file.
To get access to the ‘nvm’ functionality, you will want to log out and then back in again, or you could source the ‘~/.profile’ file in order for your current session to acknowledge the changes:
Since you now have nvm installed, you can install isolated Node.js versions.
To see what versions of Node.js are available for installation, you can type the following.
Output ... v8.7.0 v8.8.0 v8.9.0 v8.9.1 v9.0.0 v9.1.0 v9.2.0
At the time of writing this, the latest version is v9.2.0. You can install it by typing the following.
nvm install 9.2.0
Generally, nvm will switch to use the most recently installed version. You can explicitly tell nvm to use the version you have just obtained by typing.
nvm use 9.2.0
After you install Node.js with NVM, the executable is named ‘node’. You could see the version of it by typing the following.
node -v Output v9.2.0
In case you happen to have several versions of Node.js, you can see what is installed by typing the below.
If you would like to make any of the versions your default, type the below.
nvm alias default v9.2.0
This version should be chosen automatically once a new session spawns. You could also reference it by the alias like this.
nvm use default
Every version of Node.js is going to keep track of its own packages and has npm available to manage it.
You can have npm install packages to the Node.js project’s ‘./node_modules’ directory by simply using the normal format. As an example, the express module shown below.
npm install express
If you want to install this globally, causing it to be available to other projects with the same Node.js version, you need to add the ‘–g’ flag.
npm install -g express
The location of the installed package is in the following.
Installing globally allows you to run the commands from the command line but you would have to link the package to your local sphere to require it from inside a program.
npm link express
You may learn more about the options that are available to you with nvm by typing the following.