How to Install NodeJS on Ubuntu 22.04

nodejs and ubuntu logos on a green gradient background

This tutorial will walk you through installing NodeJS step-by-step on Ubuntu using the three methods. Although we use Ubuntu 22.04 for the demos, you can use your preferred version.

There are three primary ways to install NodeJS on Ubuntu:

  • Install NodeJS from the Ubuntu repository
  • Install NodeJS from the Node source
  • Install NodeJS via the Node Version Manager

Install NodeJS from the Ubuntu repository

The easiest way to install NodeJS is downloading the files from the Ubuntu repository. However, the drawback of this method is that you may get a lower or undesired NodeJS version.

First, check the repository for NodeJS version using the apt-cache command with the show option followed by the program name.

sudo apt-cache show nodejs

Next, update and upgrade the system.

sudo apt update
sudo apt upgrade

Then, install NodeJS.

sudo apt install nodejs
Output
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  nodejs
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 28.3 MB of archives.
After this operation, 187 MB of additional disk space will be used.
Get:1 https://deb.nodesource.com/node_18.x focal/main amd64 nodejs amd64 18.6.0-deb-1nodesource1 [28.3 MB]
Fetched 28.3 MB in 7s (3,868 kB/s)                                             
Selecting previously unselected package nodejs.
(Reading database ... 206165 files and directories currently installed.)
Preparing to unpack .../nodejs_18.6.0-deb-1nodesource1_amd64.deb ...
Unpacking nodejs (18.6.0-deb-1nodesource1) ...
Setting up nodejs (18.6.0-deb-1nodesource1) ...
Processing triggers for man-db (2.10.2-1) ...

Now confirm the installation by checking the version or the binaries’ destination.

Check NodeJS Version
node -v
Output
v18.6.0
Check NodeJS Binaries Path
which node
Output
/usr/bin/node

NodeJS version 18.6.0 was installed, and the binaries are found in the /usr/bin/node folder.

NodeJS successfully installed

Likewise, we can check whether the package manager was installed.

ls /usr/bin | grep ^np
node -v

npm and npx

I have npm and npx in the binaries directory. Also, I successfully installed npm version 8.13.2. Your versions may vary depending on the date of NodeJS installation and the Ubuntu version.

Install NodeJS from the Source

Installing NodeJS from the source permits you to choose the preferred version. For example, we can install the latest version of Ubuntu by copying and running these two commands.

curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
sudo apt-get install -y nodejs

Node source

If the system tells you it cannot find curl, install it first.

sudo apt install curl

Now retry installing the program, then confirm the installation by checking node and npm versions.

sudo apt install -y nodejs
Output
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  nodejs
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 28.3 MB of archives.
After this operation, 187 MB of additional disk space will be used.
Get:1 https://deb.nodesource.com/node_18.x jammy/main amd64 nodejs amd64 18.6.0-deb-1nodesource1 [28.3 MB]
Fetched 28.3 MB in 8s (3,494 kB/s)                                                                                  Selecting previously unselected package nodejs.
(Reading database ... 206165 files and directories currently installed.)
Preparing to unpack .../nodejs_18.6.0-deb-1nodesource1_amd64.deb ...
Unpacking nodejs (18.6.0-deb-1nodesource1) ...
Setting up nodejs (18.6.0-deb-1nodesource1) ...
Processing triggers for man-db (2.10.2-1) ...
Check NodeJS Version
node -v
Output
v18.6.0
Check NPM Version
npm -v
Output
8.13.2

I successfully installed NodeJS version 18.6.0 and npm version 8.13.2 by first downloading the files from the NodeJS source using the curl tool. Let’s see how to install a different version using nvm.

Install NodeJS via the Node Version Manager

The Node Version Manager (nvm) helps you install and switch between multiple NodeJS versions. All you do is specify the version after the nvm command.

nvm use 16.16.0
nvm use 18.6.0

The first step toward using nvm is installing the tool.

Install nvm

The main ways to install nvm are through wget and curl.

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

Let’s use the curl tool.

install nvim

Next, activate the program by exporting the specified tool.

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

The curl tool downloads and runs a script, which clones the nvm repository to a .nvm directory in the current user’s home directory. The script then adds the following snippet’s source lines to an appropriate profile file: ~/.bashrc, ~/.bash_profile, ~/.profile, or ~/.zshrc.

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

Lastly, let’s verify the nvm installation.

command -v nvm
Output
nvm

 

Otherwise, close and reopen the terminal before manually configuring the profile files.

bash: source ~/.bashrc

zsh: source ~/.zshrc

ksh: . ~/.profile

Now that we have installed nvm, let’s use it.

How to Install NodeJS using nvm step-by-step

Step 1: List the available versions

nvm ls-remote

We get a long list of NodeJS versions from the earliest to the latest.

Step 2: Install the desired NodeJS version

Install the long-term support version.

nvm install – lts

Or install a specific version.

nvm install [version]
e.g
nvm install 16.16.0

install NodeJS LTS

Finally, we can verify the installation by checking the NodeJS version.

node -v

verify the installation

Summary

This tutorial taught you how to install NodeJS on Ubuntu 22.04 by downloading the files from the Ubuntu repository, NodeJS source, or using the Node Version Manager. It is your turn to manage NodeJS versions on Ubuntu comfortably.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
Bash Source Command
Read More

Bash Source Command

The Bash source command reads and executes commands from a specific file as an argument within the current…
Rm Command in Linux
Read More

Rm Command in Linux

The rm command is a very important command used to remove files and directories in Linux. The rm…