Node.js is a powerful and versatile JavaScript runtime environment that enables the execution of JavaScript code outside of web browsers. It is widely used for developing high-performance server-side and networking applications. npm, on the other hand, serves as the default package manager for Node.js, providing access to a vast library of software packages.

In this article, we will explore three distinct approaches to installing Node.js and npm on Ubuntu 22.04:

  1. Standard Ubuntu repositories: The simplest method to install Node.js and npm, as it involves accessing the Ubuntu repositories. This approach is suitable for most scenarios and offers Node.js version 12.22.9.
  2. NodeSource repository: Utilize this repository to install a different Node.js version than what is provided in the Ubuntu repositories. NodeSource supports various versions, including Node.js 18.x, 17.x, 16.x, and 14.x.
  3. nvm (Node Version Manager): This tool is ideal for developers who require the flexibility of managing multiple Node.js versions on the same machine. nvm simplifies the installation process and facilitates switching between different Node.js versions effortlessly.

Select the installation method that best suits your specific requirements. If uncertain about the appropriate Node.js version, consult the documentation of the application you intend to deploy.

Installing Node.js and npm from the Ubuntu repository

At the time of writing, the Node.js version included in the default Ubuntu 22.04 repositories is v12.22.9 which is an older TLS version.

The installation is pretty straightforward. Run the following commands to update the package index and install Node.js and npm:

sudo apt update
sudo apt install nodejs npm

After executing the command mentioned above, several packages will be installed, including the essential tools required for compiling and installing native addons from npm.

To ensure a successful installation, you can verify it by running the following command:

nodejs -v
output
v12.22.9

Installing Node.js and npm from NodeSource

NodeSource is a renowned company that specializes in offering enterprise-grade Node.js support. They maintain an APT repository that includes various versions of Node.js. If your application specifically requires a particular Node.js version, you can utilize this repository.

As of the current writing, the NodeSource repository provides the following versions:

v18.x: The most recent stable version of Node.js.

v17.x

v16.x: The latest Long-Term Support (LTS) version of Node.js.

v14.x

For our installation, we will proceed with installing Node.js version 18. x:

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

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

The provided script will perform the following tasks: it will add the NodeSource signing key to your system, create an apt repository file, install all the required packages, and update the apt cache.

In case you require a different Node.js version, such as 16.x, you can modify the script by replacing setup_18.x with setup_16.x.

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.

To ensure that the installation of Node.js and npm was successful, you can verify it by printing their respective versions using the following commands:

node -v
Output
v18.2.0
npm -v
Output
8.9.0

To compile native addons from npm, you will need to install the necessary development tools. Execute the following command to install the required development tools:

sudo apt install build-essential

Installing Node.js and npm using NVM

NVM (Node Version Manager) is a convenient bash script that enables you to manage multiple Node.js versions on a per-user basis. By utilizing NVM, you can effortlessly install and uninstall any desired Node.js version for testing or usage purposes.

Visit the nvm GitHub repository page and copy either the curl or wget command to download and install the nvm script:

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

Do not use sudo as it will enable nvm for the root user.

The script will clone the project’s repository from Github to the ~/.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

As indicated in the displayed output, you have two options to ensure the changes take effect: either close and reopen the terminal, or execute the commands to add the path to the nvm script directly within the current shell session. You can choose whichever method is more convenient for you.

Once the script is in your PATH, verify that nvm was properly installed by typing:

nvm -v
output
0.39.1

To get a list of all Node.js versions that can be installed with nvm, run:

nvm list-remote

The command will print a vast list of all available Node.js versions.

Output
...
       v14.19.2   (LTS: Fermium)
       v14.19.3   (Latest LTS: Fermium)
...
       v16.14.2   (LTS: Gallium)
       v16.15.0   (Latest LTS: Gallium)
        v17.0.0
        v17.0.1
...
        v18.1.0
        v18.2.0


To install the latest available version of Node.js, simply run the following command:

nvm install node

The output should look something like this:

Output
...
Now using node v18.2.0 (npm v8.9.0)
Creating default alias: default -> node (-> v18.2.0)

Once the installation is completed, verify it by printing the Node.js version:

node -v
Output
v18.2.0

To install two additional versions of Node.js, specifically the latest LTS version (16.15.0) and version 14.19.3, you can execute the following commands:

nvm install --lts
nvm install 14.19.3

You can list the installed Node.js versions by typing:

nvm ls

The output should look something like this:

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)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.19.3
lts/gallium -> v16.15.0

The Node.js version currently being used in the current shell session is indicated by the entry with an arrow on the right (-> v14.19.3). Additionally, the default version is set to v18.2.0, which means that this version will be automatically activated when opening new shells.

If you want to change the currently active version, enter:

nvm use 16.15.0
output
Now using node v16.15.0 (npm v8.5.5)

To change the default Node.js version, run the following command:

nvm alias default 16.15.0

For more comprehensive information on using the nvm script, I recommend visiting the project’s GitHub page. There, you can find detailed documentation and resources that will assist you in understanding and utilizing the features of nvm effectively.

Conclusion

In this guide, we have demonstrated three distinct approaches for installing Node.js and npm on your Ubuntu 22.04 machine. The choice of method relies on your specific requirements and preferences. While the packaged versions from either the Ubuntu or NodeSource repository offer straightforward installation, the nvm method grants you greater flexibility to manage multiple Node.js versions on a per-user basis.

If you have any inquiries or concerns, please feel free to leave a comment below.

Comments to: How to Install Node.js and npm on Ubuntu 22.04

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