How to Install Node.js on Windows Subsystem for Linux (WSL2)

SHARE :

If you use Node.js in a professional capacity and consider performance speed and system call compatibility important, or if you want to run Docker containers that utilize Linux workspaces and avoid the need to maintain separate Linux and Windows build scripts, or if you simply prefer working with a Bash command line, then you should consider installing Node.js on the Windows Subsystem for Linux (specifically, WSL 2).

By utilizing the Windows Subsystem for Linux (WSL), you can install your preferred Linux distribution (we default to Ubuntu) which promotes consistency between your development environment (where code is written) and production environment (where the code is deployed).

Note :

If you are new to developing with Node.js and want to quickly get started with learning, it is recommended that you install Node.js on Windows. This recommendation also applies if you intend to use a Windows Server production environment.

Install WSL 2

For professional Node.js development workflows, we highly recommend using WSL 2, the latest version available for Windows. You can enable and install WSL 2 by following the steps provided in the WSL install documentation, which will guide you in choosing a Linux distribution such as Ubuntu.

Once you have successfully installed WSL 2 and a Linux distribution, locate the Linux distribution in your Windows start menu and open it. Next, check the version and codename by entering the command: lsb_release -dc.

To ensure that you have access to the most recent packages, we suggest updating your Linux distribution regularly. It's important to note that Windows doesn't automatically handle these updates, so you will need to execute the command: sudo apt update && sudo apt upgrade to update your distribution.

Install Windows Terminal (optional)

Windows Terminal is an enhanced command-line shell that facilitates running multiple tabs, allowing you to switch swiftly between Linux command lines, Windows Command Prompt, PowerShell, Azure CLI, or any other preferred shell.

It also provides the option of creating custom key bindings, utilizing the search function, personalizing your terminal with various themes including color schemes, font styles, sizes, background images with blur/transparency effects, and more.

For further information, refer to the Windows Terminal documentation.

To ensure automatic updates, it is recommended to install Windows Terminal via the Microsoft Store.

Install nvm, node.js, and npm

In addition to deciding between installing Node.js on Windows or WSL, there are other decisions to be made during the installation process.

It is advisable to use a version manager as Node.js versions change frequently, and you may need to switch between multiple versions depending on the specific requirements of each project you are working on.

The most widely used version manager is Node Version Manager (nvm). We will guide you through the process of installing nvm and then using it to install both Node.js and Node Package Manager (npm).

Alternative version managers will also be discussed in the next section.

Important

Before installing a version manager, it is advisable to remove any previous installations of Node.js or npm from your operating system. This is recommended to prevent potential conflicts that can arise from different types of installations. For instance, the version of Node.js that can be installed using Ubuntu's apt-get command is currently outdated. To get assistance with removing previous installations, please refer to the guide titled "How to Remove Node.js from Ubuntu".

1. Open your Ubuntu command line (or distribution of your choice).

2. Install cURL (a tool used for downloading content from the internet in the command-line) with:  sudo apt-get install curl 

3. Install nvm, with:  curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash 

Note :

By installing a newer version of NVM using cURL, the previous version will be replaced while the version of Node that was installed using NVM will remain unaffected. For additional details, please refer to the GitHub project page to obtain the latest information about the release of NVM.

4. To verify installation, enter:  command -v nvm  ...this should return 'nvm', if you receive 'command not found' or no response at all, close your current terminal, reopen it, and try again. Learn more in the nvm github repo.

5. List which versions of Node are currently installed (should be none at this point):  nvm ls  

6. Install both the current and stable LTS versions of Node.js. In a later step, you'll learn how to switch between active versions of Node.js with an nvm command.

  • Install the current stable LTS release of Node.js (recommended for production applications):  nvm install --lts 
  • Install the current release of Node.js (for testing latest Node.js features and improvements, but more likely to have issues):  nvm install node 

7. List what versions of Node are installed:  nvm ls  ... now you should see the two versions that you just installed listed.

8. Verify that Node.js is installed and the currently default version with:  node --version . Then verify that you have npm as well, with:  npm --version   (You can also use which node or which npm to see the path used for the default versions).

9. To change the version of Node.js you would like to use for a project, create a new project directory mkdir NodeTest, and enter the directory cd NodeTest, then enter nvm use node to switch to the Current version, or  nvm  use --lts  to switch to the LTS version. You can also use the specific number for any additional versions you've installed, like nvm use v8.2.1. (To list all of the versions of Node.js available, use the command:  nvm ls-remote ).

If you are using NVM to install Node.js and NPM, you should not need to use the SUDO command to install new packages.