How to Install Node.js and npm on Ubuntu 20.04

We have explored three different methods for installing Node.js and npm on your Ubuntu 20.04 server. You can choose the method that best suits your needs and preferences.

Home > Blog > How to Install Node.js and npm on Ubuntu 20.04

Node.js is a cross-platform JavaScript runtime environment built on the Chrome JavaScript engine. While it is commonly used for building backend applications, it is also a popular choice for full-stack and front-end development. npm, the default package manager for Node.js, is the world's largest software repository. In this article, we will guide you through three different methods to install Node.js and npm on Ubuntu 20.04.

  • From Ubuntu's Standard Software Sources: This is the simplest way to install Node.js and npm on Ubuntu, suitable for most users. The version available in the Ubuntu software sources is 10.19.0.
  • From the NodeSource Repository: By using this repository, you can install different versions of Node.js, providing more flexibility than the Ubuntu repository. Currently, NodeSource supports Node.js versions v14.x, v13.x, v12.x, and v10.x.
  • Using nvm(Node Version Manager): This tool allows you to manage multiple Node.js versions on the same machine, making it a favorite choice for Node.js developers.

1. Installing Node.js and npm from Ubuntu's Standard Software Sources

As of the time of writing, Node.js version 10.19.0 is available in the Ubuntu 20.04 software sources, and it is a long-term support version. The installation process is straightforward. Run the following commands to update the package index and install Node.js and npm:

sudo apt update
sudo apt install nodejs npm

These commands will install a range of packages, including those needed to compile and install local extensions from npm. After the installation is complete, verify the installation by running:

nodejs --version

You should see the version, such as:

v10.19.0

2. Installing Node.js and npm from the NodeSource Repository

NodeSource is a company that focuses on providing enterprise-level Node support. They maintain an APT software repository that contains various versions of Node.js. If your application requires a specific Node.js version, using this repository is a good choice.

At the time of writing, NodeSource provides the following versions:

  • v14.x - Latest stable version
  • v13.x
  • v12.x - Latest long-term support version
  • v10.x - Previous long-term support version

We will install Node.js version 14.x as an example:

1.Run the following command as a sudo user to download and execute the NodeSource installation script:

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

This script will add the NodeSource signing key to your system, create an apt source file, install required packages, and refresh the apt cache. If you need a different Node.js version, simply modify setup_14.x to the desired version, such as setup_12.x.

2.Once the NodeSource repository is enabled, install Node.js and npm:

sudo apt install nodejs

The nodejs package includes both the node and npm binaries.

3.Verify the installation of Node.js and npm by checking their versions:

node --version

You should see the version, for example:

v14.2.0
npm --version

You should see the version, for example:

6.14.4

If you plan to compile local extensions from npm, you will need to install development tools:

sudo apt install build-essential

This method offers more flexibility for users who require specific Node.js versions.

3. Installing Node.js and npm Using Node Version Manager (NVM)

Node Version Manager (NVM) is a Bash script that allows you to manage multiple Node.js versions for each user. With NVM, you can easily install or uninstall any Node.js version you want to use or test.

To install Node.js and npm using NVM, follow these steps:

  1. Start by downloading and installing the NVM script. You can find the installation command on the NVM GitHub page:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

Please avoid using sudo when running this command, as it might enable NVM for the root user. After installation, as per the script's instructions, either close and reopen your terminal or run the following commands to enable NVM for the current shell session:

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
  1. Verify the successful installation of NVM by running the following command to check the NVM version:
nvm --version

You should see the version, for example:

0.35.3
  1. Using NVM, you can list all available Node.js versions:
nvm list-remote

This will display a list of many available Node.js versions. To install the latest available Node.js version, run:

nvm install node

After the installation is complete, you can check the Node.js version by running:

node --version

You should see the version, for example:

v14.2.0

If you need to install other versions, you can use the nvm install command. Use nvm ls to see the Node.js versions you have installed and nvm use to switch to a specific version.

For more information on using the NVM script, please refer to the project's GitHub page.

4. Conclusion

In summary, we have explored three different methods for installing Node.js and npm on your Ubuntu 20.04 server. You can choose the method that best suits your needs and preferences. While installing software package versions through Ubuntu or NodeSource sources is simpler and convenient, NVM offers you more flexibility, allowing you to add or remove Node.js versions as needed. Choose the method that suits you best and embark on your Node.js development journey!