How To Install Node - Js On Ubuntu 16.04 - DigitalOcean
How To Install Node - Js On Ubuntu 16.04 - DigitalOcean
Introduction
In this guide, we'll show you how to get started with Node.js on an Ubuntu
16.04 server.
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04 Page 1 of 11
How To Install Node.js on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(42
Prerequisites
This guide assumes that you are using Ubuntu 16.04. Before you begin, you
should have a non-root user account with sudo privileges set up on your
system. You can learn how to do this by completing steps 1-4 in the initial
server setup for Ubuntu 16.04.
In order to get this version, we just have to use the apt package manager.
We should refresh our local package index first, and then install from the
repositories:
If the package in the repositories suits your needs, this is all you need to do
to get set up with Node.js. In most cases, you'll also want to also install npm,
which is the Node.js package manager. You can do this by typing:
This will allow you to easily install modules and packages to use with
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04 Page 2 of 11
How To Install Node.js on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(42
Node.js.
To check which version of Node.js you have installed after these initial
steps, type:
nodejs -v
Once you have established which version of Node.js you have installed from
the Ubuntu repositories, you can decide whether or not you would like to
work with different versions, package archives, or version managers. Next,
we'll discuss these elements along with more flexible and robust methods
of installation.
First, you need to install the PPA in order to get access to its contents. Make
sure you're in your home directory, and use curl to retrieve the installation
script for your preferred version, making sure to replace 8.x with your
preferred version string (if different):
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04 Page 3 of 11
How To Install Node.js on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(42
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 preferred text
editor):
nano nodesource_setup.sh
The PPA will be added to your configuration and your local package cache
will be updated automatically. After running the setup script from
nodesource, you can install the Node.js package in the same way you did
above:
To check which version of Node.js you have installed after these initial
steps, type:
nodejs -v
Output
v8.10.0
The nodejs package contains the nodejs binary as well as npm, so you don't
need to install npm separately.
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04 Page 4 of 11
How To Install Node.js on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(42
npm -v
Output
5.6.0
In order for some npm packages to work (those that require compiling code
from source, for example), you will need to install the build-essential
package:
You now have the necessary tools to work with npm packages that require
compiling code from source.
Controlling your environment with nvm allows you to access the newest
versions of Node.js and retain and manage previous releases. It is a
different utility from apt-get, however, and the versions of Node.js that you
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04 Page 5 of 11
How To Install Node.js on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(42
To start off, 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:
Once the prerequisite packages are installed, you can pull down the nvm
installation script from the project's GitHub page. The version number may
be different, but in general, you can download it with curl:
nano install_nvm.sh
bash install_nvm.sh
To gain access to the nvm functionality, you'll need to log out and log back
in again, or you can source the ~/.profile file so that your current session
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04 Page 6 of 11
How To Install Node.js on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(42
source ~/.profile
Now that you have nvm installed, you can install isolated Node.js versions.
To find out the versions of Node.js that are available for installation, you can
type:
nvm ls-remote
Output
...
v8.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)
As you can see, the newest LTS version at the time of this writing is v8.9.4.
You can install that by typing:
Usually, nvm will switch to use the most recently installed version. You can
explicitly tell nvm to use the version we just downloaded by typing:
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04 Page 7 of 11
How To Install Node.js on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(42
When you install Node.js using nvm, the executable is called node. You can
see the version currently being used by the shell by typing:
node -v
Output
v8.9.4
If you have multiple Node.js versions, you can see what is installed by
typing:
nvm ls
This version will be automatically selected when a new session spawns. You
can also reference it by the alias like this:
Each version of Node.js will keep track of its own packages and has npm
available to manage these.
You can have npm install packages to the Node.js project's ./node_modules
directory by using the normal format. For example, for the express module:
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04 Page 8 of 11
How To Install Node.js on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(42
~/.nvm/node_version/lib/node_modules/package_name
Installing globally will let you run the commands from the command line, but
you'll have to link the package into your local sphere to require it from within
a program:
You can learn more about the options available to you with nvm by typing:
nvm help
Removing Node.js
You can uninstall Node.js using apt-get or nvm, depending on the version
you want to target. To remove the distro-stable version, you will need to
work with the apt-get utility at the system level.
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04 Page 9 of 11
How To Install Node.js on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(42
This command will remove the package and retain the configuration files.
These may be of use to you if you intend to install the package again at a
later point. If you donʼt want to save the configuration files for later use,
however, then run the following:
This will uninstall the package and remove the configuration files associated
with it.
As a final step, you can remove any unused packages that were
automatically installed with the removed package:
To uninstall a version of Node.js that you have enabled using nvm, first
determine whether or not the version you would like to remove is the
current active version:
nvm current
If the version you are targeting is not the current active version, you can
run:
If the version you would like to remove is the current active version, you
must first deactive nvm to enable your changes:
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04 Page 10 of 11
How To Install Node.js on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(42
nvm deactivate
You can now uninstall the current version using the uninstall command
above, which will remove all files associated with the targeted version of
Node.js except the cached files that can be used for reinstallment.
Conclusion
As you can see, 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.
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04 Page 11 of 11