How to Install Node.js & npm on Ubuntu 22.04: A Step-by-Step Guide

How to Install Node.js & npm on Ubuntu 22.04

Node.js is a popular open-source JavaScript runtime environment used for building scalable web applications. It comes with a package manager, npm, which makes it easy to install and manage packages for your project.

In this article, we will cover three different ways to install Node.js and npm on Ubuntu 22.04. These methods include:

  1. Installing from the Ubuntu Repositories
  2. Installing from the NodeSource Repository
  3. Installing using nvm (Node Version Manager)

Choosing the appropriate installation method will depend on your specific needs and preferences. For most use cases, the first method should suffice as it is the easiest and quickest option. However, if you require a specific version of Node.js or need to have multiple versions installed on the same machine, then the latter two methods may be more suitable.


Installing Node.js and npm from the Ubuntu repository

The easiest and quickest way to install Node.js and npm on Ubuntu 22.04 is to use the standard Ubuntu repositories. The version available in the repositories is v12.22.9, which is the latest LTS (Long-Term Support) version of Node.js.

Run the commands below to install Node.js and npm from the Ubuntu repository:

sudo apt update
sudo apt install nodejs npm

Verify that Node.js and npm are installed correctly by checking their versions. Run the following commands:

nodejs -v

You should see the version number of Node.js and npm printed to the console. For example, if the installation was successful, you should see output similar to the following:

nodejs -v
v12.22.9

Installing Node.js and npm from NodeSource

If you need a different version of Node.js than the one provided by the Ubuntu repositories, you can install it from the NodeSource repository. NodeSource is a popular third-party repository that provides newer versions of Node.js and npm than what is available in the Ubuntu repository.

Run the command below to Download and add the NodeSource PPA:

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

Note: Replace 18.x with the version of Node.js you want to install. The latest LTS version at the time of writing is 18.x.

Next, Install Node.js and npm by running the following command:

sudo apt install nodejs npm

After that, verify that Node.js and npm are installed correctly by checking their versions. Run the following commands:

node -v
npm -v

You should see the version number of Node.js and npm printed to the console. For example, if the installation was successful, you should see output similar to the following:

node -v
v18.2.0

npm -v
8.9.0

Installing Node.js and npm using NVM (Node Version Manager)

nvm (Node Version Manager) is a tool that allows you to install and manage multiple Node.js versions on the same machine. This is useful if you need to test your applications against different Node.js versions or if you want to use a specific version for a specific project.

To install Node.js and npm using nvm, you need to download and install the nvm script. The nvm script is available on the nvm GitHub repository page. You can download the script using either the curl or wget command.

If you prefer to use curl, open a terminal window and run the following command:

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

If you prefer to use wget, open a terminal window and run the following command:

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

When you run the curl or wget command to install nvm, it downloads a script from the nvm GitHub repository and runs it in your terminal. This script clones the nvm repository to your home directory under the ~/.nvm directory.

In addition, the script attempts to add the source lines from the snippet below to the correct profile file (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc), depending on which one exists on your system:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

These lines add nvm to your system’s environment variables so that you can use it to manage your Node.js installations. Specifically, the NVM_DIR environment variable is set to ~/.nvm, and the nvm.sh script is sourced to load nvm.

Once the script has successfully completed, you can start using nvm to install and manage different versions of Node.js on your Ubuntu 22.04 system.

You can check the version of nvm installed on your system by running the following command in your terminal:

nvm -v
0.39.1

This output shows that the version of nvm installed on the system is 0.39.1. The exact version number may vary depending on when you installed nvm and which version you chose to install.

How to Use NVM (Node Version Manager)

To download, compile, and install the latest release of node, run the commands below :

nvm install node # "node" is an alias for the latest version

To install a specific version of node, run the commands below :

nvm install 14.7.0 # or 16.3.0, 12.22.1, etc

The first version installed becomes the default. New shells will start with the default version of node.

To get a list of all Node.js versions that can be installed with nvm, you can run the following command in your terminal:

nvm ls-remote
Output
 
  nvm ls-remote

      v16.13.1   (LTS: Fermium)
      v16.14.0
      v16.14.1
      v16.15.0
...

To install the latest LTS version, Run the command below:

nvm install --lts

To list all the installed Node.js versions using nvm, you can run the nvm ls command in the terminal. This command will display all the Node.js versions that are currently installed on your system.

nvm ls
Output

->     v14.19.3
       v16.15.0
        v18.2.0
default -> node (-> v18.2.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v18.2.0) (default)
stable -> 18.2 (-> v18.2.0) (default)
lts/* -> lts/gallium (-> v16.15.0)
...

The arrow (->) indicates the currently active version of Node.js, which is version v14.19.3.

To change the currently active version of Node.js, use the command below followed by the desired version number:

nvm use 18.2.0

This command will set the active version of Node.js to v18.2.0.

If you want to learn more about using nvm, you can visit the project’s GitHub page at https://github.com/nvm-sh/nvm. Here, you will find detailed information about how to use nvm, including commands for installing and managing Node.js versions, updating nvm, setting default Node.js versions, and more.


Conclusion

Node.js and npm are essential tools for any modern web development project. In this article, we have explored three different methods for installing Node.js and npm on Ubuntu 22.04. Choose the method that best fits your needs and get started building your next web application.

Got questions or suggestions on the article? Don’t hesitate to leave a comment below. Your feedback is valuable to us and helps improve the content quality. And don’t forget to share this article with others who might find it useful. Looking forward to hearing from you!

If our tutorials helped you, please consider buying us a coffee. We appreciate your support!

Thank you for your support.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top