Installing the Distro-Stable Version of Node.Js
Prior to the installation, you must update the package repository:
apt-get update && apt-get upgrade
After it is updated, you can install the Node.js via the APT package manager:
apt-get install nodejs
If you aren’t sure about the Node.js version running on your server, you can run the command line below to check:
The version of the Node.js will display below the command line.
Some people prefer to keep multiple versions of Node.js packages. In order to manage multiple Node.js version, you must install the NPM:
app-get install npm
After installing it, you can confirm its version by running the command line:
Installing a Higher Node.Js Version with PPA
To install a higher Node.js version, you must have a PPA (Personal Package Archive). PPA can be downloaded on NodeSource. PPA always provide higher versions of Node.js that are not available in the repositories of your Ubuntu system.
If you don’t have a Personal Package Archive, you must install it with the following command line:
$ cd ~ $ curl -sL https://deb.nodesource.com/setup_8.x -o nodesource_setup.sh
If you don’t have curl on your system, you can install it:
apt-get istall curl
Now, you can install Node.js:
apt-get install nodejs
Once it is installed, you can recheck the version:
Both the nodejs and npm are included in the nodejs package. The npm contains a file used primarily in tracking the latest updates. If you want to check the version of npm installed, you must type:
Installing the build-essential package is important if you want to access a number of the npm packages.
sudo apt-get install build-essential
Installing Node.js with NVM
If you don’t want to install the Node.js via APT, you can install it through the NVM. NVM works independently from your home directory. It allows you to install a number of Node.js versions on the Ubuntu system. With NVM, you can conveniently access the latest Node.js version as well as manage earlier versions.
The versions you can install in the NVM are not the same as those found in the repositories.
To install build-essential, you must run the command line:
sudo apt-get install build-essential libssl-dev
With the prerequisites met, you can download the nvm installation script at the project GitHub page:
curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh -o install_nvm.sh
Now, you must execute the script with the command line:
It will install all the important files of the software in the .nvm directory of the home directory. The installation will also add a couple of lines to the .profile file.
In order for the nvm to function fully, you must refresh the login by logging out and logging back again. Alternatively, you can source the ~/.profile file to validate all the changes in the session.
Installing Isolated Node.js Version
To install the isolated Node.js version, you must enter the command below to discover the available versions of Node.js:
After entering this command line, you will be able to see all the versions available for install. The last version is the latest version. To install the newest version, you must enter the command line followed by the version number:
nvm install x.x.x
By default, it will use the version that you’ve installed most recently, If you want, you can specify in the command line on which version to use:
nvm use 8.9.4
To check which version is being used, you can type the command line:
To make a certain version the default version, the following command line can be used:
nvm alias default 8.9.4
Every Node.js has its own npm that keep track of the packages. Whenever you want to install the npm, you simply type npm install followed by the module name. For example, if you want to install the express module, you must type:
npm install express
You simply need to attach the -g flag if you want to use the module in other projects that run on similar node.js:
npm install -g express
If you want to use the module, you must link it by typing:
npm link express
The help file of nvm can be accessed by typing:
Node.js can be uninstalled with apt-get. If you want to get the distro-stable version removed, you must type the command line:
sudo apt-get remove nodejs
When you execute the command line, it will attempt to remove the package. However, the configuration files will no6 be deleted. You might not want to delete the configuration files as they will be useful in the near future. To delete the configuration files, you can run the command:
sudo apt-get purge nodejs
You should also implement the line to delete all unused packages:
sudo apt-get autoremove
If you want to uninstall the current Node.js version, you can query the computer by typing the command line:
Before you can uninstall the current version, you need to deactivate it by typing the command:
You can uninstall a specified version of the node with the command line:
nvm uninstall node_version
The above command will have the specified node.js uninstalled. It will attempt to remove the version of node.js but retain the cached files.
In conclusion, there are several ways to install Node.js on your Ubuntu system. Installing the package via the repository is the most convenient method for beginners. If you prefer a more flexible method, you should install the Node.js through the NVM method.