Oct 2, 2020 8 min read

How to Install Node.js on Ubuntu 20.04

Install Node.js on Ubuntu 20.04 with our step-by-step tutorial. It enables developers to build scalable and high-performance web applications.

Install Node.js on Ubuntu 20.04
Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to install Node.js on Ubuntu 20.04. Let's first understand - What is Node.js?

Node.js is a powerful, open-source JavaScript runtime environment that enables developers to build scalable and high-performance web applications. It uses an event-driven, non-blocking I/O model, making it efficient for handling multiple requests simultaneously.

Node.js is widely used for creating server-side applications, real-time applications, and microservices. Its lightweight nature and extensive library ecosystem make it a popular choice for developers seeking speed and flexibility in web development.

In this tutorial, you will install Node.js on Ubuntu 20.04. We will also address a few FAQs related to the Node.js installation.

Advantages of Node.js

  1. High Performance: Node.js uses a non-blocking, event-driven architecture, allowing it to handle numerous concurrent requests efficiently.
  2. Scalability: It enables the creation of scalable applications, thanks to its ability to handle a large number of connections with minimal resource consumption.
  3. Fast Development: Node.js employs JavaScript on both the client and server, enabling developers to use a single language throughout the entire development process.
  4. Extensive Library Ecosystem: Node.js has a vast collection of libraries and modules, known as the Node Package Manager (NPM), which aids in rapid application development.
  5. Real-time Applications: Node.js is particularly suited for real-time applications, such as chat applications or collaborative tools, where instant data updates are crucial.

Prerequisites to Install Node.js on Ubuntu 20.04

  • Ubuntu 20.04 machine
  • Should have a non-root user account
  • Sudo privileges set up on your system

Step 1 - Install Distro-Stable Version for Ubuntu

1) Ubuntu 20.04 has a version of Node.js in its default repositories and it is useful to provide a consistent experience across various systems. Here, the version in repositories is 8.10.0. This is not the latest version. Still, it is stable and sufficient for immediate experimentation with the language. So, to get the latest version, use an apt package-manager. Then, refresh your local package index using the following command:

sudo apt update

2) Next, you will install Node.js from the repositories using the following command:

sudo apt install nodejs

3) If the package in repositories suits your needs, you need not do anything else. In case you want to install npm, that is node package manager, you can do that using the following command:

sudo apt install npm

It will install modules and packages also, to use along with Node.js.

It has a conflict with another-package, so the executable from Ubuntu repositories is called nodejs, instead of node.

4) Now, to check the version of Node.js installation, type:

nodejs -v

After knowing the version of Node.js installation from Ubuntu-repositories. Next, you can decide if you want to work or not with the other versions, package archives, or version managers.

Step 2 - Install Using PPA

1) Now, for a recent version of Node.js, you will have to add a PPA, which is a Personal Package Archive. It is managed by NodeSource. It has more latest versions of Node.js, than the official Ubuntu-repositories. Moreover, it will allow choosing between Node.js v4.x, Node.js v6.x, and Node.js v8.x version.

Firstly, you need to install the PPA, in order to get access to its contents. From your home directory, use curl. It will retrieve the installation script for the version you prefer. Then, make sure to replace 10.x with your preferred-version string:

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

2) You will inspect the contents of this script with the nano using the following command:

nano nodesource_setup.sh

3) Next, you will need to run the script under sudo:

sudo bash nodesource_setup.sh

4) There will be an addition of PPA to your configuration. Also, in your local-package cache will get updated automatically. You can now install the Node.js package using the following command:

sudo apt install nodejs

5) You can check the version of Node.js installation. Do it by typing:

nodejs -v
Output

v10.22.0

6) The nodejs package includes both nodejs binary and npm. You have no need to install the npm separately.

npm uses a configuration file in your home directory. It enables to keep track of all updates. It will get created the first time you run npm. Execute the command to verify that npm is installed and to create the configuration file.

npm -v
Output

6.14.6

7) After that, you need to install the build-essential package, if you want some npm packages to work.

sudo apt install build-essential

You now have the necessary tools to work with npm packages. It mainly needs to compile code from the source.

Step 3 - Install using NVM

1) nvm is an alternative tool to install Node.js. It stands for the Node.js version manager. Rather than working at the operating system level, nvm works at the level of an independent directory, within your home directory. This means that you will install multiple, self-contained versions of Node.js. It gets it done without affecting the entire system.

Controlling the environment with nvm will allow access to new versions of Node.js. Further retains and manages the previous releases. It is a different utility from apt.

2) Now, download the nvm installation script from the project’s GitHub page. You will use curl. Also, the version number may differ from what is here:

curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.35.3/install.sh -o install_nvm.sh

3) Next, inspect the installation-script with nano.

nano install_nvm.sh

4) After that, run the script with bash.

bash install_nvm.sh

Therefore, the above command installs the software into a subdirectory of your home directory at ~/.nvm. Further, it will add the necessary lines to your ~/.profile file.

If you want to gain access to the nvm functionality, you will need to either log out and log in back again or source the ~/.profile file. It will let the current session know about the changes:

source ~/.profile

5) With the installation of nvm, you will also install isolated Node.js versions. For information about the versions of Node.js available, type:

nvm ls-remote
Output
...
       v12.18.3   (Latest LTS: Erbium)
        v13.0.0
        v13.0.1
        v13.1.0
        v13.2.0
        v13.3.0
        v13.4.0
        v13.5.0
        v13.6.0
        v13.7.0
        v13.8.0
        v13.9.0
       v13.10.0
       v13.10.1
       v13.11.0
       v13.12.0
       v13.13.0
       v13.14.0
        v14.0.0
        v14.1.0
        v14.2.0
        v14.3.0
        v14.4.0
        v14.5.0
        v14.6.0
        v14.7.0  

6) You will see, the current LTS version here is v12.18.3. You will install that by typing:

nvm install 12.18.3

7) Usually, nvm will switch to use the latest version. You can easily tell nvm to use the version by typing:

nvm use 12.18.3

8) When you install Node.js using nvm. The executable here is called as node. You will see the version used by the shell by typing the following command:

node -v
Output

v12.18.3

9) If you have multiple Node.js versions. You will see the installation by typing:

nvm ls

10) Further, if you want to default one of the versions, type:

nvm alias default 12.18.3

11) This version gets selected automatically. It will be at the time when a new session spawns. You will also reference it by alias like:

nvm use default

12) Each version of Node.js keeps track of its own packages and can be managed by npm.

You will have npm to install packages to Node.js project’s ./node_modules directory. Use the below command to install the express module:

npm install express

13) If you want to install the module globally. Then make it available to other projects by the same version of Node.js. You can add the -g flag:

npm install -g express

14) It will install the packages in:

~/.nvm/versions/node/12.18.3/lib/node_modules/express

Installing modules globally, lets you run commands from the command line. Still, you will have to link the package into your local sphere. You will need to require it from within a program:

npm link express

15) You can learn more about available options with nvm using the following command:

nvm help

Step 4 - Removing the Node.js

1) You can uninstall Node.js using apt or nvm. It will depend on the version you want to target. To remove the distro-stable version, you will need to work with apt the utility at the system level.

Use the following command to remove the distro-stable version:

sudo apt remove nodejs

The above command will remove the package, but will retain the configuration files. These may be of use if you want to install the package later.

2) If you do not want to save the configuration files for later use. Then run the following command:

sudo apt purge nodejs

It will uninstall the package. Additionally, remove configuration files associated with it.

3) You can also remove any unused packages if they were automatically installed, with the removed package:

sudo apt autoremove

4) To uninstall a version of Node.js, enabled using nvm. First determine whether the version, you want to remove is the current active version, using the following command:

nvm current

5) So if the version you are targeting is not the currently active version. You can run:

nvm uninstall node_version

The command will uninstall the selected version of Node.js.

6) If the version you want to remove is the currently active version. You should first deactivate nvm, to enable the changes:

nvm deactivate

To uninstall the current version of Node.js, run the uninstall command. This process will erase all files related to the targeted version, excluding the cached files utilized for future reinstallations.

FAQs to Install Node.js on Ubuntu 20.04

How do I check if Node.js is already installed on Ubuntu 20.04?

You can check if Node.js is installed by opening a terminal and typing node -v. If it returns a version number, Node.js is already installed.

Does the package manager install npm (Node Package Manager) along with Node.js?

Yes, when you install Node.js using the package manager, npm is also installed automatically.

Can I install a specific version of Node.js using apt?

By default, apt installs the latest version of Node.js available in the Ubuntu repositories. If you need a specific version, you may need to use other methods like nvm (Node Version Manager).

What is nvm, and how can I use it to install Node.js?

nvm (Node Version Manager) is a tool that allows you to manage multiple installations of Node.js. You can install nvm and use it to install specific versions of Node.js by following the instructions provided in the nvm documentation.

How do I update Node.js to the latest version on Ubuntu 20.04?

To update Node.js to the latest version, you can use the package manager by running the command sudo apt update followed by sudo apt upgrade nodejs.

How can I uninstall Node.js from Ubuntu 20.04?

If you installed Node.js using the package manager, you can uninstall it by running the command sudo apt remove nodejs. This will remove Node.js and npm from your system.

Conclusion

We hope this detailed tutorial helped you to install Node.js on Ubuntu 20.04. To learn more about the Node.js installation, check out the official documentation.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.