Back to Linux VPS

How to Install Node.js on Ubuntu 20.04

How to Install Node.js on Ubuntu 20.04
14 Oct, 2024 Linux VPS

JavaScript is one of the most popular programming languages ​​today, contributing to the construction of millions of websites on the Internet. Node.js is a runtime environment that provides all the necessary components to execute a program written in JavaScript. In this article, we will learn what is Node.js ? and how to install Node.js on Ubuntu 20.04.

What is Node.js?

Node.js is an open source platform built on the V8 Javascript engine. Node.js is written in c++ and Javascript. This platform was developed Node.js in 2009 by Ryan Lienhart Dahl.

The purpose of Node.js is to provide an efficient and asynchronous web application running environment. This platform uses a non-blocking I/O (input/output) model, allowing multiple requests to be processed at the same time without being blocked.

Node.js is currently used by many of the world’s leading technology companies, quickly surpassing 1 billion downloads since 2018 and supporting about 1.2% of all websites on the Internet, equivalent to 20 million pages.

Some large companies using this platform are: Netflix, Walmart, Uber, NASA, Paypal, Twitter, Spotify, eBay, Reddit, Linkedin, …

Advantages and disadvantages of Node.JS

Advantages

  • Asynchronous event-driven IO, allowing for processing multiple requests simultaneously.
  • Using JavaScript – an easy-to-learn programming language.
  • Sharing the same code on both the client and server sides.
  • NPM (Node Package Manager) and Node modules are growing stronger and stronger.
  • Active support community.
  • Allows streaming of large files.

Disadvantages

  • Not scalable, so it cannot take advantage of the multi-core model in today’s server-grade hardware.
  • Difficult to work with relational databases.
  • Each callback will come with many other nested callbacks.
  • Requires good knowledge of JavaScript.
  • Not suitable for CPU-intensive tasks.

3 ways to install Node.js on Ubuntu 20.04

Method 1: Use apt to install NodeJS package from Ubuntu’s default software repository

Ubuntu 20.04 contains a version of Node.js in the default repositories to ensure consistency across systems. To install Node.js, you first need to update apt:

sudo apt update

Then install Node.js:

sudo apt install nodejs

To check if the installation was successful, we use the following command to check the version of Node.js:

node -v

If the result returned is the version number of Node.js, you have successfully installed.

v10.19.0

Next, npm is also a useful thing that you should not ignore, this is the Node.js package manager, which helps you install modules and packages when working later. You can install npm using apt with the following command:

sudo apt install npm

Method 2: Install Node.js with apt using NodeSource PPA

To install any version of Node.js, you can use the PPA (Personal Package Archive) maintained by NodeSource. PPA has more versions of Node.js available than the default Ubuntu repositories.

First, install the PPA to have access to its packages. From your home directory, use curl to retrieve the installation script for the version you want, here using version 16.x:

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

Check the contents of the downloaded script with nano or your favorite text editor:

nano /tmp/nodesource_setup.sh

Save and exit. Then run the script with sudo privileges:

sudo bash /tmp/nodesource_setup.sh

The PPA will be added to your configuration and your local package cache will be updated automatically. You can now install Node.js as you did in the previous section:

sudo apt install nodejs

And then use the version check command to make sure the installation was successful:

node -v

Installing Node.js from NodeSource already includes npm, so you don’t need to install it again.

Method 3: How to Install Node.js on Ubuntu 20.04 Using Node Version Manager

Another way to install Node.js is to use nvm (Node Version Manager). This software allows you to install and maintain multiple independent versions of Node.js and their associated Node packages at the same time.

To install nvm on your Ubuntu 20.04 machine, visit the nvm project page on GitHub. Copy the curl command from the README file. This will get you the latest version of nvm.

Before you run the command in bash, make sure that the command doesn’t do anything strange. You can check this by removing the | bash at the end of the curl command and running:

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

Once you’re sure that the command will work, add | bash at the end of the command:

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

This will install the nvm script into your user account. To use it, you can use the command:

source ~/.bashrc

Now, you can look up the current version of Node using nvm:

nvm list-remote

The output can be quite long. You can choose to install a version of Node based on the list provided. For example, to install v14.10.0, you can use the command:

nvm install v14.10.0

You can see the different versions you have installed by listing them:

nvm list

This command will tell you the currently active version on the first line (-> v14.10.0), followed by the alias name and the versions that the aliases point to.

You can also switch between installed versions with the command:

nvm use v14.10.0

Uninstall Node.js

You can uninstall Node.js using apt or nvm, depending on how you installed it. To remove a version of Node from the repository, use the command:

sudo apt remove nodejs

By default, this command retains any local configuration files that have been created since installation. If you do not want to save these configuration files, use the following command:

sudo apt purge nodejs

To uninstall the version of Node.js that you installed using nvm, first determine if it is the current active version:

nvm current

If the version you are trying to remove is not the current active version, you can run the command:

nvm uninstall node_version

This command will uninstall the version of Node.js that you have selected.

If the version you are trying to remove is the current active version, you will first need to deactivate nvm to activate your changes:

nvm deactivate

You can now uninstall the current version using the uninstall command, which will remove all files associated with the specified version of Nodejs.

Conclusion

Above are some ways to install Node.js on Ubuntu 20.04. For many users, using apt with the default repository is enough. If you need newer versions or a specific version of Node, you should use the PPA repository. If you are developing Node applications and need to switch between Node versions frequently, choose the nvm installation method. Good luck.

If you have any questions about VPS services, please contact us for advice and register a free VPS account

Share on