How to install Node.js on Ubuntu 22.04 distro running on WSL2 (Windows Subsystem for Linux)?

· 3 min read
How to install Node.js on Ubuntu 22.04 distro running on WSL2 (Windows Subsystem for Linux)?

In this post, we'll see how to install Node.js (open-source, cross-platform JavaScript runtime environment) on Ubuntu 22.04 distro running in WSL2 (Windows Subsystem for Linux).

Option #1 - Install Node.js with APT package Manager from the Official Repositories

NodeJs is already available in default official repositories of Ubuntu 22.04. But it is not the latest version.

Update local package index.

abhijit@AwsJunkie:~$ sudo apt update

Install NodeJs using APT package Manager.

abhijit@AwsJunkie:~$ sudo apt install nodejs -y
[sudo] password for abhijit:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Suggested packages:
  npm
The following NEW packages will be installed:
  nodejs
0 upgraded, 1 newly installed, 0 to remove and 17 not upgraded.
Need to get 122 kB of archives.
After this operation, 932 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu jammy/universe amd64 nodejs amd64 12.22.9~dfsg-1ubuntu3 [122 kB]
Fetched 122 kB in 0s (273 kB/s)
Selecting previously unselected package nodejs.
(Reading database ... 38233 files and directories currently installed.)
Preparing to unpack .../nodejs_12.22.9~dfsg-1ubuntu3_amd64.deb ...
Unpacking nodejs (12.22.9~dfsg-1ubuntu3) ...
Setting up nodejs (12.22.9~dfsg-1ubuntu3) ...
update-alternatives: using /usr/bin/nodejs to provide /usr/bin/js (js) in auto mode
Processing triggers for man-db (2.10.2-1) ...

Check the version to verify.

abhijit@AwsJunkie:~$ node -v
v12.22.9

Option #2 - Install Node.js using NodeSource PPA (Personal Package Archive)

For the latest or different Node.js version (e.g. v16, v18, and v19 etc.), install the corresponding NodeSource Node.js repo using the setup script available at https://github.com/nodesource/distributions/tree/master/deb.

abhijit@AwsJunkie:~$ curl -sL https://deb.nodesource.com/setup_19.x | sudo -E bash -

Installing the NodeSource Node.js 19.x repo…

Populating apt-get cache…

Confirming “jammy” is supported…

Adding the NodeSource signing key to your keyring…

Creating apt sources list file for the NodeSource Node.js 19.x repo…

Running apt-get update for you…

Run sudo apt-get install -y nodejs to install Node.js 19.x and npm

You may also need development tools to build native addons:

 sudo apt-get install gcc g++ make

To install the Yarn package manager, run:

 curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null
 echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
 sudo apt-get update && sudo apt-get install yarn

Run sudo apt-get install -y nodejs to install Node.js 19.x and npm.

abhijit@AwsJunkie:~$ sudo apt-get install -y nodejs

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 17 not upgraded. Need to get 29.3 MB of archives. After this operation, 189 MB of additional disk space will be used. Get:1 https://deb.nodesource.com/node_19.x jammy/main amd64 nodejs amd64 19.8.1-deb-1nodesource1 [29.3 MB] Fetched 29.3 MB in 12s (2492 kB/s) Selecting previously unselected package nodejs. (Reading database … 37929 files and directories currently installed.) Preparing to unpack …/nodejs_19.8.1-deb-1nodesource1_amd64.deb … Unpacking nodejs (19.8.1-deb-1nodesource1) … Setting up nodejs (19.8.1-deb-1nodesource1) … Processing triggers for man-db (2.10.2-1) …

Check the version to verify.

abhijit@AwsJunkie:~$ node -v

v19.8.1 abhijit@AwsJunkie:~$ npm -v 9.5.1

References