How to Install Node.js and NPM on Ubuntu 20.04

The Node.js is the very well-known JavaScript runtime environment used to JavaScript code server-side execution. It is an Open-source, cross-platform supported environment. It allows you to run the JavaScript code as a standalone application in your machine. It is popular to build back-end server-side application development, but nowadays, it is used in full-stack web development and front-side solutions.

The default package manager for Node.js is called npm; it is the world’s largest software registry.

In this guide, we will show you how to install Node.js and npm on Ubuntu 20.04 using various methods, which you can use to implement in any Ubuntu-based distribution.

Suppose you want to install Node.js as a runtime for deploying or hosting any Node.js application. In that case, you should use to install Node.js from the NodeSource repository, but if you are a developer and wanted to create a development environment for Node.js application development, prefer to go with NVM script to install Node.js.

You can choose the installation process as per your needs. The best idea is to go through the Node.js application documentation to find which version of Node.js is supported and need to install.

Install Node.js and npm from NodeSource

NodeSource is focused on enterprise support for Node.js and then maintains this repository with the latest version of Node.js.

We can use this repository to install any specific stable and the latest version of Node.js.

You need to follow these steps to install Node.js and npm from the NodeSource repository.

Step 01: To enable the NodeSource repository, run the following curl command as a sudo privileged user:

$ curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -

This command will add the NodeSource repository key to your system and create an apt source repository file to install all required packages and dependencies.

The above command will install Node.js 12. If you want to install another version of Node.js, replace “setup_12.x” in the above command with your version; for example, if you need version 14, then write “setup_14.x”.

Step 02: After enabling the NodeSource repository, you can install the Node.js and npm by executing the below command:

$ sudo apt install nodejs

Step 03: After installing Node.js and npm as both contain in the same package (node.js), verify the installation by checking the installed version using the following commands:

$ node --version
output:
v12.16.3
$ npm --version
output:
6.14.4

Install Node.js and npm using NVM

As we know, the NVM (Node Version Manager) installation is used by the developer to install Node.js and npm it helps to install multiple active Node.js version in a system. It helps to install and uninstall any specific version of Node.js to use or test.

You can go through the following steps to install Node.js and npm using NVM script:

Step 01: Install NVM script

Use the following command to download and install the nvm script:

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

The above command will make a clone of the NVM repository from Github to th ~/.nvm directory:

output:
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Now NVM script is installed, but it not show in the same session, and it indicates in the output of the above command either you should close and reopen the terminal for a new session or add the path to nvm script to execute the command.

Once the script is in your PATH, verify the installed NVM by checking the NVM script version:

$ nvm --version
output:
0.34.0

Step 02: Install Node.js and npm

Now you can use the command “nvm” to install the latest version of Node.js by using the following command:

$ nvm install node
output:
Downloading and installing node v12.8.1...
Downloading https://nodejs.org/dist/v12.8.1/node-v12.8.1-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.8.1 (npm v6.10.2)
Creating default alias: default -> node (-> v12.8.1)

After completing the installation of Node.js, you can verify it by checking the version of Node.js:

$ node --version
output:
v12.8.1

Now, let us install another two version of Nod.js, the latest LTS and version 8.10.0 using the nvm command:

$ nvm install --lts
$ nvm install 8.10.0

To list out the installed Node.js version, you can use the following command:

$ nvm ls

The output will look like below:

output:
->      v8.10.0
       v10.16.3
        v12.8.1
default -> node (-> v12.8.1)
node -> stable (-> v12.8.1) (default)
stable -> 12.8 (-> v12.8.1) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/dubnium (-> v10.16.3)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.16.1 (-> N/A)
lts/dubnium -> v10.16.3

To change the currently active version of Node.js using below command:

$ nvm use 10.16.3
output:
Now using node v10.16.3 (npm v6.9.0)

If you want to change the version of default Node.js, use the following command:

$ nvm alias default 10.16.3

Install Node.js and npm from the Ubuntu default repository

The Node.js and npm packages are also available in the default Ubuntu repositories.

To install Node.js and npm from the default Ubuntu repository, use the following commands:

$ sudo apt update
$ sudo apt install nodejs npm

The installed Node.js executable from the Ubuntu repository is named nodejs instead of node to avoid the conflict with another package.

Verify the installation of Node.js by checking the version of nodejs:

$ nodejs --version
output:
v8.10.0

Install development tools

To compile and install native addons from npm, you need to install development tools for the development environment.

To install development tools in the Ubuntu system, use the following command:

The above command will install all necessary packages for development, including GCC compilers:

$ sudo apt install build-essential

Uninstall Node.js

If you want to remove Node.js and npm package for some reasons, you can uninstall using the following command:

$ sudo apt remove nodejs npm

Conclusion

You have learned to install Node.js and npm in three different ways in your Ubuntu 20.04 machine. The methods of installation you can choose as per your requirements and preferences. Where the installation of Node.js and npm package from NodeServer and Ubuntu repository, Ubuntu repository is more comfortable, the NVM repository gives you more flexibility to use any version of Node.js in a single machine.

If you face any issue with installation or have doubt, question, or feedback, please leave a comment below.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Related Articles