There are three ways that you can leverage to install Node.JS on Ubuntu and we will explore each of them.
Method 1: Install Node.JS and NPM from Ubuntu Repository
To install Node.JS and NPM from the Ubuntu repository, start off by updating the package lists:
$ sudo apt update
Then use the apt package manager to install Node.JS and NPM from the Ubuntu repository as follows.
$ sudo apt install nodejs npm -y
At the time of publishing this guide, the latest version of Node.JS is v10.19.0 from the default Ubuntu repository. To confirm this, run the command:
$ nodejs -v OR $ nodejs --version
Method 2: Install Node.JS and NPM from Nodesource Repository
The alternative way of installing Node.JS and NPM is to install from NodeSource, which is an organization that is geared towards helping companies run mission-critical Node.JS applications with an emphasis on performance and resource utilization.
To start off, download and run the Nodesource installation script as shown.
$ curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
The script adds a Nodesource signing key, creates a repository of Nodesource in the sources list file, and updates the apt cache.
The output will also provide you will additional instructions on how to proceed.
For example, to install NodeJS, run the command:
$ sudo apt install nodejs -y
The NodeJS package provides both nodejs and npm binaries.
To confirm that Node.JS was installed, run the command:
$ node --version
For npm, run the command:
$ npm --version
Method 2: Install Node.JS and NPM Using NVM
Node Version Manager, abbreviated as NVM, is a shell script that helps in the management of multiple Node.JS versions on a per-user basis. Using NVM, you can install, uninstall and set a specific Node.JS version as the default version.
To download the NVM script, run the command:
$ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
Towards the end, the output will inform you that you need to reopen your terminal to start using nvm. So, close your terminal and launch it once more to begin running nvm commands.
You can verify the version of nvm installed by executing the command:
$ nvm --version
To retrieve a list of all the Node.JS versions that can be installed, run the command:
$ nvm list-remote
The output provides all the possible versions that can be installed including the LTS versions on Node.JS. At the time of publishing this guide, the latest LTS version is v16.14.2.
To install the latest LTS version of Node.JS, execute:
$ nvm install --lts
Just to confirm the latest LTS version has been installed run the command:
$ node --version
To install a specific version of Node.JS, for example, 17.7.2, run the command:
$ nvm install 17.7.2
To list all the versions of Node.JS installed on your system, run the command:
$ nvm ls
To change to another version of Node.JS, for example, 16.14.2, run the command:
$ nvm use 16.14.2
To set the default version of Node.JS, run:
$ nvm alias 16.14.2
And there you have it. We have explored three ways that you can use to install Node.JS and npm on your system. Installing from the Ubuntu repository does not give you the latest version of Node.JS.
To get the latest version, installing from Nodesource and NVM is recommended. The NVM installation method also gives you room for flexibility in terms of installing your preferred version of Node.JS and choosing which version to set as default for your environment.