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.
You can check the success of the installation, and at the same time, you can find out the version using the command:
Installing 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.
Now we will download and run the script of the corresponding version, which will add the PPA to the system repositories.
And install Node.js from the added repositories.
Let's check the installed version.
In 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.
Now 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:
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" bash_completion
At this point, the NVM is ready to use. To get a list of versions available for installation:
Choose a version and install:
View the list of installed versions:
Output:
-> 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.
If, 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:
In addition, the nvm list output shows LTS releases of Node.js. Not installed ones look like this:
To install them, specify the release alias:
To check the currently used version, use the command:
Output: