How to Install Node.js on Ubuntu 20.04
Node.js is a JavaScript runtime. In this tutorial, we'll go over the different ways to install Node.js on Ubuntu 20.04 and their benefits.
How to install Node.js on Ubuntu 20.04:
Installing Node.js with apt
This is the easiest and most sufficient installation method for many purposes. In addition to Node.js itself, we will immediately install its package manager - npm. Node.js packages are libraries or tools that can be downloaded using npm.
sudo apt update && sudo apt install nodejs npmYou can check the success of the installation, and at the same time, you can find out the version using the command:
nodejs -vInstalling Node.js with PPA
Installing via the PPA gives you a choice of version. Check out the available versions in the documentation. We'll also need curl to continue.
sudo apt install curlNow we will download and run the script of the corresponding version, which will add the PPA to the system repositories.
curl -fsSL https://deb.nodesource.com/setup_15.x | sudo -E bash -And install Node.js from the added repositories.
sudo apt install nodejsLet's check the installed version.
node -vIn this case, npm is already installed.
Installing Node.js with NVM
NVM is Node.js Version Manager. It allows you to install different versions of Node.js with npm packages at the same time and switch between them at runtime.
To install the latest version of the NVM installation script, go to the Installing and Updating part of the project page on GitHub, copy the link and run it on your Ubuntu 20.04.
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bashNow you need to log out of your account and log back in. The second option is to execute the following commands to ensure the operation of NVM:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" bash_completionAt this point, the NVM is ready to use. To get a list of versions available for installation:
nvm list-remoteChoose a version and install:
nvm install 14.16.0View the list of installed versions:
nvm listOutput:
v12.21.0
-> v14.16.0
default -> 14.16.0 (-> v14.16.0)
...To select a Node.js version, use the command indicating the required version. Thus, you can switch between different versions if there are several installed.
nvm use v14.16.0If, in addition to the versions installed using nvm, the system also contains the version installed using apt, it will be marked as system. To switch to it, use:
nvm use systemIn addition, the nvm list output shows LTS releases of Node.js. Not installed ones look like this:
lts/dubnium -> v10.24.0 (-> N/A)To install them, specify the release alias:
nvm install lts/dubniumTo check the currently used version, use the command:
node -vOutput:
v14.16.0
700
300
700
300
700
300