What is Node.js?

Node.js is an open-source, multiplatform JavaScript runtime environment built on Chrome’s JavaScript to run JavaScript code outside a web browser. It is often employed to design efficient and scalable networking and server-side applications. The default package manager for Node.js is called npm, the world’s largest software registry.

Three Ways to Install Node.js on Ubuntu

There are three simple ways to install Node.js on Ubuntu. These include:

  • Installing Node.js packages using apt from Ubuntu’s default software repository.
  • Installing specific versions of the node.js package using apt and an alternate PPA (Personal Package Archive) software repository.
  • Using the Node Version Manager (nvm) to install and manage multiple versions of Node.js.

More often, developers install node.js using apt and the default repository. However, you should use the PPA repository if you require a particular older (or newer) version of Node. The nvm method should be used if you are constantly developing Node apps and need to transition between different Node versions.

How to Install Node.js on Ubuntu

Installing node.js on Ubuntu is easy and straightforward. However, before starting the installation, you need to set up a non-root user account on your system with sudo privileges.

Installing Node.js with Apt from the Default Repositories

Node.js is included in Ubuntu 20.04’s default repository, allowing the creation of a unified user interface for different platforms. Using the apt package manager will allow you to obtain this version. 

Step 1: Reload your local package index by typing:

sudo apt update 

Step 2: Install node with the command below:

sudo apt install nodejs   

Step 3: Request node for its version number to confirm the installation was successful:

node -v
Output
v10.19.0

Step 4: Installing npm (Node Package Manager) is usually advised. This allows you to install Node.js modules and packages. You can install the npm package with apt using this command:

sudo apt install npm

This is all that is required to get your Node.js and npm installed if the package in the repositories satisfies your requirements.

Install Specific Versions of Node.js Package with Apt Using a NodeSource PPA

Step 1: Install the PPA to allow access to its packages. Get the installation script for your desired version from your home directory using curl and ensure to change 16.x with the version string you want.

cd ~
curl -sL https://deb.nodesource.com/setup_16.x -o /tmp/nodesource_setup.sh

Step 2: Use nano (or your desired text editor) to browse the script’s contents after downloading it:

nano /tmp/nodesource_setup.sh

Step 3: Once you are certain that the script can run without risk, close your editor and run the script with sudo.

sudo bash /tmp/nodesource_setup.sh

Step 4: Your settings will change to include the PPA, and your local package cache will be immediately updated. You can now install the Node.js package, as explained in the last section.

sudo apt install nodejs 

Step 5: Run Node using the -v version flag to confirm that you have installed the new version:

node -v  
Output
v16.6.1

Note: You don’t need to install npm differently because the NodeSource nodejs package already includes the npm and node binary.

The output above shows that Node.js has been successfully installed on Ubuntu 22.04.

Installing Node Using the Node Version Manager

With the help of the Node Version Manager software package, you can simultaneously install and maintain numerous independent versions of Node.js and their Node packages.

Step 1: Copy the curl command from the README file that appears on the home page. This provides you with the installation script’s most recent version.

Step 2: Check the script to ensure it does not execute anything you disagree with before placing the command to bash. To do this, delete the | bash segment from the end of the curl command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh

Step 3: Assess the modifications it makes to ensure that you are satisfied with them. Again, run the command with | bash inserted at the end.

The URL you use will differ based on the latest version of the nvm. At this point, you can download and run the script by entering this command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

Step 4: Install nvm script on your user account. You need to source your .bashrc file first before using it:

source ~/.bashrc

Step 5: Request NVM about the Node versions that are supported:

nvm list-remote   
Output
. . .
       v14.16.0   (LTS: Fermium)
       v14.16.1   (LTS: Fermium)
       v14.17.0   (LTS: Fermium)
       v14.17.1   (LTS: Fermium)
       v14.17.2   (LTS: Fermium)
       v14.17.3   (LTS: Fermium)
       v14.17.4   (Latest LTS: Fermium)
        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
        v15.7.0
        v15.8.0
        v15.9.0
       v15.10.0
       v15.11.0
       v15.12.0
       v15.13.0
       v15.14.0
        v16.0.0
        v16.1.0
        v16.2.0

Step 6: You can install a version of Node by entering any of the release versions above. For example, you can type the following command below to get version v14.10.0:

nvm install v14.10.0    

Step 7: Entering the command below will display all of the installed node versions:

nvm list
Output
->     v14.10.0
         system
default -> v14.17.4 (-> N/A)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v14.10.0) (default)
stable -> 14.10 (-> v14.10.0) (default))
. . .

The above displays the active version on the first line (-> v14.10.0), followed by some named aliases and their related versions.

Below, you will find aliases for the various Node long-term support (LTS) releases:

Output
. . .
lts/* -> lts/fermium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.4 (-> N/A)
lts/fermium -> v14.17.4 (-> N/A)

Step 8: To install a release based on these aliases, you will need to install the latest LTS version, fermium, by running this command:

nvm install lts/fermium
Output
Downloading and installing node v14.17.4...
. . .
Now using node v14.17.4 (npm v6.14.14))

Step 9: To switch between installed versions with nvm, run this command:

nvm use v14.10.0
Output
Now using node v14.10.0 (npm v6.14.8)
```

You can verify that the install was successful using the same technique from the other sections, by typing:

```command
node -v
Output
v14.10.0

Now, your computer runs the correct Node version with a supported version of npm.

See also: How to Install Node.js and NPM on Windows

Conclusion

You can install Node.js Ubuntu 20.04 server in different ways. You can choose the method that works for you based on your specific needs. However, if you are uncertain about which method to use, you should consider using the packaged version in Ubuntu’s repository method, which is the easiest. But if flexibility is your priority, you should consider using the nvm method because it allows you to add and uninstall various Node.js versions for different users.

Visit ServerMania to find the best prices for your Ubuntu dedicated server and learn more about our Ubuntu Server system requirements. You can also find out more about installing Ubuntu Desktop on your server by booking a free consultation with us today.