Node.js makes development more integrated and consistent as it uses an event driven, non blocking I/O model making it efficient and lightweight.
Node.js package manager called npm is by far the biggest open source libraries ecosystem in the world.
In this tutorial, you will learn how to install Node.js (using Ubuntu Official repository, PPA and nvm) on Ubuntu 18.04.
Ubuntu 18.04 has a stable version of Node.js in its official repositories. Although this will be an older version, it should be sufficient and stable enough for general purposes.
You can rely on the apt package manager to install this version. First you refresh your package list index by running the command:
sudo apt update
Next, install Node.js by issuing the command below:
sudo apt install nodejs
Now you can check the Node.js installed version :
Oftentimes, you’ll also need to install npm, the Node.js package ecosystem. This can be done by executing the command :
sudo apt install npm
This will give you the possibility to install various packages and modules that can be used with Node.js .
Adding the PPA directly from NodeSource, will enable you to get a more recent version of Node.js . As mentioned in the previous section, NodeSource maintains up-to-date versions of Node.js unlike that found in the official Ubuntu repositories.
You will first need to install the PPA from NodeSource. Type in the following command to install version 11 :
curl -sL https://deb.nodesource.com/setup_13.x | sudo -E bash –
To install Node.Js version 11, simply replace setup_13.x in the command above with setup_11.x as shown below in the snapshot :
The curl command will retrieve the appropriate installation script for your desired version.
Run the downloaded script using the command below:
sudo bash nodesource_setup.sh
This will add a new repository in /etc/apt/source.list.d , .i.e the source list directory and will update your local package cache.
You can now install the Node.js package once the setup script above has been run. Type in the command below :
sudo apt install nodejs
Now you can verify Node.js installation by typing in :
The Nodejs package contains already the Nodejs binary along with npm. Contrary to the previous installation from Ubuntu repositories, npm does not to be installed separately.
To verify npm version number, issue the command below:
To remove the version that was downloaded from the official Ubuntu repositories, run the command below :
sudo apt remove nodejs
This command will delete Node.js the package but the configuration files will be kept. if you set out to install the Node.js package in the future again, you can retain the configuration files. Otherwise, If you plan not to reinstall Node.js at a later time and therefore you don’t want to keep the configuration files, issue the following command:
sudo apt purge nodejs
Finally, it might be that some unused packages were automatically installed with Node.js. These can be removed by running the command :
sudo apt autoremove
NVM is a bash script for managing multiple active Node.js versions. NVM will allow you to install and uninstall any given Node.js version. NVM provides you access to the latest versions of Node.js . It also helps keep and manage previous releases.
The following steps will help you install Node.js as well as npm on your Ubuntu system using NVM .
The curl command will help download the nvm installation Bash script from the nvm’s GitHub page. Note that the version number may differ from what is highlighted here:
curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh -o install_nvm.sh
Once the script has been transferred to your local machine, run it with bash:
This will install the application into a sub-directory of your home folder at ~/.nvm.
To start using nvm, either quit the current session and open your terminal again or source the file ~/.profile in order for your current session to know about the changes:
You should now be able to verify that nvm was installed properly by typing:
After nvm has been installed in your machine, you can now proceed to the installation of the latest version of Node.js. To do this , run the command below :
nvm install node
Once this is done, display the node.js version using the command:
Notice that the executable is called node since you used nvm .
In most cases, nvm will use the recently installed version. If however you have a Node.js version in mind (10.0.0 say) and want to install it and use it , run the command below :
nvm use 10.0.0
In case you have several Node.js versions, the following command will show what versions are installed:
To default one a given version, run:
nvm alias default 10.0.0
This version, i.e. 10.0.0 will be selected automatically when a new session starts. This can also be referred to like this:
nvm use default
The npm utility will manage each Node.js version so that it keeps track of its own packages.
You have seen three different methods that will help you install Node.js ion your Ubuntu 18.04. Although the two first installations, i.e. using Ubuntu official repository and Nodesource are the easiest, the nvm option will provide you more freedom and flexibility when it comes to dealing with multiple Node.js versions.
To work with Node.js, any reliable Linux code editor can be used. You can find out in this article a list of the best code editors for Linux.