Node.js is a JavaScript runtime environment used to run JavaScript code on the server-side. It is a cross-platform designed to build fast and scalable back-end applications.
NPM also know as a “Node Package Manager” is an online repository for developing and sharing JavaScript code. Generally, it is used for automated dependency and package management. You can specify all your projects dependencies in your package.json file then just run npm install command to install all dependencies.
In this tutorial, you will learn –
- Install Node.js and NPM from Ubuntu Repository
- Install Node.js and NPM from the NodeSource Repository
- Install Node.js and NPM using NVM
Prerequisites
- A server running Ubuntu 20.04.
- A root password is configured on your server.
Install Node.js and NPM from Ubuntu Repository
At the time of writing this article, the latest Node.js version available in the Ubuntu default repository is 10.x. This is the simple and easiest method to install Node.js on Ubuntu.
First, update the package cache with the following command:
apt-get update -y
Next, install the Node.js and NPM using the following command:
apt-get install nodejs npm -y
Once the installation if complete, verify the Node.js version with the following command:
node --version
Output:
v10.19.0
You can also verify the NPM version with the following command:
npm --version
Output:
6.14.4
If you want to uninstall Node.js and NPM from your system, run the following commands:
apt-get remove nodejs npm --purge apt-get autoremove apt-get clean
Install Node.js and NPM from the NodeSource Repository
NodeSource repository allows you to install multiple version of Node.js in your system. At the time of writing this article, NodeSource supports Node.js v15.x, v14.x, v13.x, v12.x, and v10.x. This method is very useful if your application requires a specific version of Node.js.
Here, we will install Node.js version 14.x from the NodeSource repository.
First, install the curl command-line tool with the following command:
apt-get install curl -y
Next, download and run the NodeSource installation script using the following command:
curl -sL https://deb.nodesource.com/setup_14.x | bash -
This command will add NodeSource signing key, install all dependencies and add Node.js repository to the APT source file.
Note: Just replace 14.x with 15.x, 12.x or 10.x if you want to install another Node.js version.
Once the repository is added, install the Node.js and NPM with the following command:
apt-get install nodejs -y
Once installed, verify the Node.js version using the following command:
node --version
Output:
v14.15.4
To verify the NPM version, run the following command:
npm --version
Output:
6.14.10
Install Node.js and NPM using NVM
NVM stands for “Node Version Manager” is a tool used to download and install multiple Node.js versions in your system. It helps you manage and switch between different Node versions with ease.
First, go to the NVM Git Repository and download the latest version of NVM script.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash -
This script will clone the nvm repository to ~/.nvm, and attempts to add the source lines from the snippet below to the correct profile file:
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 13527 100 13527 0 0 69369 0 --:--:-- --:--:-- --:--:-- 69015 => Downloading nvm as script to '/root/.nvm' => Appending nvm source string to /root/.bashrc => Appending bash_completion source string to /root/.bashrc => Close and reopen your terminal to start using nvm or run the following to use it now: export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
Next, close and reopen the terminal or run the following command to activate the profile.
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
Next, verify the NVM version with the following command:
nvm --version
Output:
0.37.2
Next, print a list of all available Node.js version using the following command:
nvm list-remote
Output:
v14.15.0 (LTS: Fermium) v14.15.1 (LTS: Fermium) v14.15.2 (LTS: Fermium) v14.15.3 (LTS: Fermium) -> v14.15.4 (Latest LTS: Fermium) v15.0.0 v15.0.1 v15.1.0 v15.2.0 v15.2.1 v15.3.0 v15.4.0 v15.5.0 v15.5.1 v15.6.0
If you want to install the latest stable Node.js version, run the command below:
nvm install --lts
Output:
Installing latest LTS version. Downloading and installing node v14.15.4... Downloading https://nodejs.org/dist/v14.15.4/node-v14.15.4-linux-x64.tar.xz... ######################################################################################################################################## 100.0% Computing checksum with sha256sum Checksums matched! Now using node v14.15.4 (npm v6.14.10) Creating default alias: default -> lts/* (-> v14.15.4)
If you want to install Node.js latest version v15.6.0, run the command below:
nvm install v15.6.0
Output:
Downloading and installing node v15.6.0... Downloading https://nodejs.org/dist/v15.6.0/node-v15.6.0-linux-x64.tar.xz... ######################################################################################################################################## 100.0% Computing checksum with sha256sum Checksums matched! Now using node v15.6.0 (npm v7.4.0)
To list all installed Node.js versions, run the following command:
nvm ls
Output:
v14.15.4 -> v15.6.0
To check the active Node.js version, run the command below:
node --version
Output:
v15.6.0
Now, change the default Node.js version to v14.15.4, run the command below:
nvm use v14.15.4
Conclusion
In the above post, you learned how to install Node.js using three different ways. You can now choose your desired way to install Node.js on Ubuntu 20.04.
If you want to manage your application dependencies. I would recommend installing Yarn to manage the dependency.