In this guide, we’ll show you about installing Node.js on an Ubuntu 16.04 server.
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:
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:
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:
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):
You can inspect the contents of this script with nano (or your text editor would you like to use):
And run the script under sudo:
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:
Type the following line to check Node.js version you have installed after these initial steps:
The nodejs package includes the nodejs binary and npm, so it’s not necessary to install npm separately.
npm utilises 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:
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:
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:
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:
And check the installation script with nano:
Run the script with bash:
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:
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:
->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:
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:
While you install Node.js using nvm, the executable is named node. You can see the version used by the shell at present, type:
If you have manifold Node.js versions, you can get what is installed by typing:
If you want to default one of the versions, type:
This version will be automatically selected while a new session is created. You can also refer to it by the alias like this:
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:
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.
This will install the package in:
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:
Type the following line to get more help about the options available to you with nvm.
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:
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:
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:
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:
If you are target version is not the current active version, you can execute:
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.
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.
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.