This post provides instructions about how to install Node.js on Ubuntu 16.04 LTS. See this post for Node.js resources. (Node.js Offical Github Repo.)
NPM (Node Package Manager) is the default package manager for the JavaScript runtime environment Node.js. NPM hosts thousands of free node packages. In general, NPM is installed on your computer after you install Node.js.
There are several ways to install Node.js on Ubuntu:
- Method #1 (our choice in this tutorial): Install Node.js with Node Version Manager (NVM) to manage multiple active Node.js versions
Using nvm
, we can install multiple, self-contained versions of Node.js, which will allow us to control our environment, and get access to the newest versions of Node.js, but will also allow us to keep previous releases that our applications may depend on. (nvm is just like Virtualenv in Python, if you are familiar with it, which allows us to install multiple version of the same Python library into “virtual folders” by pip.)
This is the method we will cover later in this tutorial.
- Method #2: Install the bundled Distro-Stable Version Node.js (version 4.2.6) – it is very simple to install, just one or two commands.
Ubuntu 16.04 contains a version of Node.js in its default repositories that can be used to easily provide a consistent experience across multiple systems. At the time of writing, the version in the repositories is version 4.2.6. This will not be the latest version, but it should be quite stable, and should be sufficient for quick experimentation with the language.
This tutorial picked the Node Version Manager (nvm) based method, because it is much more flexible.
See below for the step by step instructions. (Check out the reading list below if you need the install instructions for other methods listed above.)
Step 0: (Before we get started) Remove old Node package to avoid conflicts
Open a terminal (Ctrl + Alt + T), and type the following command.
$ dpkg --get-selections | grep node
# If it says install in the right column, Node is on your system:
#ax25-node install
#node install
Step 1: Install prerequisite packages
We’ll need to get the software packages from our Ubuntu repositories that will allow us to build source packages. The nvm script will leverage these tools to build the necessary components.
First, we need to make sure we have a C++ compiler. Open a terminal window (Ctrl + Alt + T) and install the build-essential and libssl-dev packages. By default, Ubuntu does not come with these tools — but they can be installed by the following commands.
$ sudo apt-get update
$ sudo apt-get install build-essential libssl-dev
Step 2: Install nvm
Once the prerequisite packages are installed, we can install and update NVM(Node Version Manager) using cURL. (Note: to get the latest installation version link, on the page scroll down to “Install script”.)
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
Inspect the installation script with nano
:
$ nano install_nvm.sh
#Note that we DO NOT need to add anything in the opened nano text editor window. We just need to create the .sh file.
# Use Ctrl+O to save the file, and then hit Enter, and then Ctrl +X to close the file.
Run the script with bash (Note that run the following command in your terminal):
$ bash install_nvm.sh
It will install the software into a sub-directory under our home directory at ~/.nvm
. It will also add some necessary lines to our ~/.profile
file in order to use it.
To have access to the nvm functionality, we need to source the ~/.profile
file so that our current session knows about the changes:
$ source ~/.profile
Now that we have nvm installed, we can install isolated Node.js versions.
Step 3: Install Node.js
The following command will tell us which versions of Node.js are available for us to install:
$ nvm ls-remote
Output
...
v6.10.3 (Latest LTS: Boron)
v7.0.0
v7.1.0
v7.2.0
v7.2.1
v7.3.0
v7.4.0
v7.5.0
v7.6.0
v7.7.0
v7.7.1
v7.7.2
v7.7.3
v7.7.4
v7.8.0
v7.9.0
v7.10.0
The newest version when I write this post is v7.10.0. We can install it by the following command:
$ nvm install 7.10.0
By default, nvm will switch to use the most recently installed version. We can explicitly tell nvm to use the version we just installed by the following command:
$ nvm use 7.10.0
When we install Node.js using nvm, the executable is called node (NOT nodejs that you may see in other tutorials)
. We can check the currently used Node and npm version by the following commands:
$ node -v
# OR
$ node --version
# Output
# v7.10.0
$ npm -v
# OR
$ npm --version
# output
# 4.2.0
Step 4: using nvm to manage different versions of installed Node.js
If you have multiple Node.js versions, you can see what are installed by the following command:
$ nvm ls
To set a default Node.js version to be used in any new shell, use the alias default command
$ nvm alias default 7.10.0
# This version will be automatically selected when a new session spawns. You can also reference it by the alias like this:
$ nvm use default
To learn more about the options available to use with nvm, run the following command in your terminal:
$ nvm help
Step 5: using npm to install Node.js modules
Each version of Node.js will keep track of its own packages and has npm
available to manage these.
We can use npm to install packages to the Node.js project’s ./node_modules directory by using the normal format. For example, for the express module:
$ npm install express
If you’d like to install it globally (i.e., making it available to other projects using the same Node.js version), you can add the -g
flag:
$ npm install -g express
This will install the package in:
~/.nvm/node_version/lib/node_modules/package_name
Note that installing globally will allow us to run the commands from the command line, but we will have to link the package within a project in order to use it in that project:
$ npm link express
References and further reading list:
This tutorial covers two methods:
Method #1: Install the bundled distro specif Node.js version 4.2.6
Method #2: Install the latest version of Node.js version 6.x or 7.x
This post is very good — it covers the following ways to install Node.js:
Installing using the Official Repository
Installing using the Github Source Code Clone
Installing using Node Version Manager (NVM)
It covers How to Install Multiple Nodejs version with NVM and also covers how to remove Node.js
This tutorial covers:
- How To Install the Distro-Stable Version for Ubuntu
- How To Install Using a PPA
- How To Install Using NVM
there are a quite a few ways to get up and running with Node.js on your Ubuntu 16.04 server. Your circumstances will dictate which of the above methods is the best idea for your circumstance. While the packaged version in Ubuntu’s repository is the easiest, the nvm method is definitely much more flexible.
This is a pretty good tutorial. It covers 4 Ways to Install Node.js on Ubuntu. There are several ways to do this, but The author of this post recommended Option 1: Node Version Manager (nvm). Here is the full list of options: