Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save noygal/6b7b1796a92d70e24e35f94b53722219 to your computer and use it in GitHub Desktop.
Save noygal/6b7b1796a92d70e24e35f94b53722219 to your computer and use it in GitHub Desktop.
Installing node via windows subsystem for linux

Windows 10 version 2004 - Installing Node.js on Windows Subsystem for Linux (WSL/WSL2)

UPDATE (Fall 2020): This gist is an updated version to the Windows 10 Fall Creators Update - Installing Node.js on Windows Subsystem for Linux (WSL) guide, I usually just keep here notes, configuration or short guides for personal use, it was nice to know it also helps other ppl, I hope this one too.

Windows updated windows subsystem for linux to version 2, as the F.A.Q stated you can still use WSL version 1 side by side with version 2. I'm not sure about existing WSL machines surviving the upgrade process, but as always backup and 🤞. NOTE: WSL version 1 is not replace/deprecated, and there are some exceptions where you would want to use it over version 2.

There major changes between version 1 to 2, there is a nice comparison table on microsoft site, but if you the gist of it, those two would have the most impact for the day to day user:

  1. Increased file IO performance - file operation like ``git clone, npm install`, etc' could be up to 20x faster compared to `WSL` 1.
  2. Full system call compatibility - the long awaited docker support!

Enabling WSL - mandatory for installing WSL2

The feature is not enabled by default and you need to activate it, you can do it via powershell (with admin rights):

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

Or you can open: Control-Panel -> Programs -> Turn Windows feature on or off, and click the "Windows Subsystem for Linux" checkbox.

Enabling WSL2 - requires windows version 2004 update

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Or you can open: Control-Panel -> Programs -> Turn Windows feature on or off, and click the "Virtual Machine Platform" checkbox.

Restart your machine to complete the WSL install and update to WSL 2.

Now need to explicitly set the WSL version to 2:

wsl --set-default-version 2

On my machine I got the following error:

C:\WINDOWS\system32>wsl.exe --set-default-version 2
Error: 0x1bc
For information on key differences with WSL 2 please visit https://aka.ms/wsl2

The solution was to install this package, following this offical post.

Install linux

For those who are not familiar with linux, fragmentation if part of the fun - there are a lot of linux distributions and package management systems for it, WSL support most of the popular distributions, my personal favorite is debian and I recommend that or ubuntu for those who are new to linux, detailed guide.

After download, click on "Launch" and follow the instructions on screen and enter the user name and password for the linux machine, to launch the default machine just run:

C:\>bash ~

Note: the command will start at the home path - ~, if no path is give it would start at the current path.

You can install multiple machines, to get an overview of your installed machines use the command:

C:\>wsl --list --verbose --all
  NAME            STATE           VERSION
* Debian          Stopped         2
  Ubuntu-20.04    Stopped         2
  kali-linux      Stopped         2

If you want to uninstall a distribution and DELETE the machine, run:

C:\>wsl --unregister Ubuntu-20.04

wsl.exe is the windows cli tool for managing your linux machine, you can export/import (backup) your machines, start/stop and execute commands with specific distribution and user.

C:\>wsl --help
Copyright (c) Microsoft Corporation. All rights reserved.
Usage: wsl.exe [Argument] [Options...] [CommandLine]
Arguments for running Linux binaries:
    If no command line is provided, wsl.exe launches the default shell.
    --exec, -e <CommandLine>
        Execute the specified command without using the default Linux shell.
    --
        Pass the remaining command line as is.
Options:
    --distribution, -d <Distro>
        Run the specified distribution.
    --user, -u <UserName>
        Run as the specified user.
Arguments for managing Windows Subsystem for Linux:
    --export <Distro> <FileName>
        Exports the distribution to a tar file.
        The filename can be - for standard output.
    --import <Distro> <InstallLocation> <FileName> [Options]
        Imports the specified tar file as a new distribution.
        The filename can be - for standard input.
        Options:
            --version <Version>
                Specifies the version to use for the new distribution.
    --list, -l [Options]
        Lists distributions.
        Options:
            --all
                List all distributions, including distributions that are currently
                being installed or uninstalled.
            --running
                List only distributions that are currently running.
            --quiet, -q
                Only show distribution names.
            --verbose, -v
                Show detailed information about all distributions.
    --set-default, -s <Distro>
        Sets the distribution as the default.
    --set-default-version <Version>
        Changes the default install version for new distributions.
    --set-version <Distro> <Version>
        Changes the version of the specified distribution.
    --shutdown
        Immediately terminates all running distributions and the WSL 2 lightweight utility virtual machine.
    --terminate, -t <Distro>
        Terminates the specified distribution.
    --unregister <Distro>
        Unregisters the distribution.
    --help
        Display usage information.

Installing Node.js

First We'll start by updating linux, for those of you that are not familiar with linux this require running the process as root by add the sudo command before the command we need to execute:

sudo apt-get update
sudo apt-get upgrade

You'll need to enter your user password to proceed, if you prompt just press y (or yes) to continue.

We also need to install some basic developer tools and dependencies for node-gyp - node binaries build tool.

sudo apt-get install curl wget git build-essential libssl-dev

I do not recommend on the "official" to install node, node rapid development life cycle (and the entire ecosystem for that matter) leads more then often for the need to have tight control over the node binaries version, or more likely to have to project running on different versions of node (such as LTS versions).

NVM tool gives you the ability to install and use multiple version of node, and prevent the (bad) usage of sudo for running node application, installation is via the command line:

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

Note: This command install version 0.35.3, check for newer versions here.

Exit and enter the machine to load the new changes, and you should be able to install the latest the latest LTS version of node by running:

nvm install --lts

Note: if you still get bash: nvm: command not found after restarting the machine, installer didn't identify the terminal profile files, depending on the configuration you will need to add the following lines to ~/.bashrc, ~/.bah_profile, ~/.zshrc or ~/.profile.

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

One thing you'll have to remember when use NVM is that you'll need to explicitly specify the node version you want to use (installing does it automatically on the end of the install), so next time you'll login to ubuntu you'll need to run the command:

nvm use --lts

Alternative you can add the command to terminal profile file: ~/.bashrc, ~/.bah_profile, ~/.zshrc or ~/.profile.

To update node to the latest just run the install command again.

Developing

Your windows hard disk drives are mounted in linux under /mnt/<drive letter>/ so you can access any folder via linux and run whatever command you need in order to get your project up and running, it is recommended not to relay on cross OSs file operations for WSL2 machines so keep your source file inside the machine.

vscode offers IDE terminal integration support with minimal configuration, guide. If your IDE just point to the executable (like cmd.exe) check if you can point it to c:\Windows\System32\bash.exe.

@Seefer
Copy link

Seefer commented Apr 28, 2019

I constantly get this when running that nvm curl script :(

➜ ~ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 12461 100 12461 0 0 61688 0 --:--:-- --:--:-- --:--:-- 61688
=> nvm is already installed in /home/seefer/.nvm, trying to update using git
=> => Compressing and cleaning up git repository

=> Appending nvm source string to /home/seefer/.zshrc
=> Appending bash_completion source string to /home/seefer/.zshrc
net.js:268
err = this._handle.open(fd);
^

Error: EINVAL: invalid argument, uv_pipe_open
at new Socket (net.js:268:24)
at createWritableStdioStream (internal/process/stdio.js:191:18)
at process.getStdout [as stdout] (internal/process/stdio.js:20:14)
at console.js:454:19
at NativeModule.compile (internal/bootstrap/loaders.js:364:7)
at Function.NativeModule.require (internal/bootstrap/loaders.js:176:18)
at setupGlobalConsole (internal/bootstrap/node.js:407:41)
at startup (internal/bootstrap/node.js:142:7)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
=> 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

The upshot is even though ~/.bashrc contains the lines to run nvm.sh and the .nvm directory exists on my system, typing nvm always results in command not found.

@Seefer
Copy link

Seefer commented Apr 28, 2019

FYI

After much Googling, I found nothing but saw a chat unrelated to NVM about ensuring scripts are marked as executable. On a whim, I went into the .nvm folder and typed chmod +x nvm.sh. Now things seem to be working so far. Unsure why I had to do this on my system when all the instructions I see on the web about that cover installing nvm on a WSL system do not cover this manual step.

@csnowbar
Copy link

csnowbar commented Oct 9, 2019

Wow! Your instructions worked flawlessly. Thank you so much for taking the time to put this gist together!!!

@psychicbologna
Copy link

Thanks so much for this.

@adames
Copy link

adames commented Oct 15, 2019

After the command curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash, I received an error nvm is already installed and simply had to source ~/.bashrc to pick up the installations's new paths (for any users who had something similar).

@noygal
Copy link
Author

noygal commented Oct 25, 2019

Thanks for the comments, I'm glad to see that this post is useful.

It has been a while since I wrote this post (WSL was on beta), so it needed some updates:

  • lxrun.exe is deprecated, replaced it with wsl.exe
  • Update NVM to v0.35.0
  • Change Node.js version to LTS , currently it is v12.13.0
  • Adding nvm use --lts to .bash_profile

Also I see that most of the ppl encounter problems with the NVM installation, I recommend to first see if there is a newer version of NVM - if so install that, if that didn't help try to check NVM repo/docs for known issues.

@thelazyliz
Copy link

Thank you, this worked perfectly!

@minlare
Copy link

minlare commented Nov 29, 2019

In order to get this working for me I first had to uninstall my current version of node using:
sudo apt-get purge --auto-remove nodejs

Works perfectly otherwise, thanks!

@calvimor
Copy link

If you are using zsh, must add following in ~/.zshrc

export NVM_DIR=~/.nvm [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"

@fakesquid
Copy link

This works great!

@aderchox
Copy link

All the steps worked just fine! Thank you very much.

@pedropcruz
Copy link

If you are using zsh, must add following in ~/.zshrc

export NVM_DIR=~/.nvm [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"

This gives an error:
image
I really cant switch node version. Everytime i do, if i open a new terminal I lost all configuration (using WSL on Windows)

Many thanks

@dompascal
Copy link

Splitting into 2 lines should get rid of that error.

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

You will then need to run source ~/.zshrc to update ZSH.

@ameft
Copy link

ameft commented Mar 25, 2020

I followed this tutorial and installed node 10 (over v8) with just this two commands:
$ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
$ sudo apt-get install -y nodejs

@salvimateus
Copy link

Thank you!

@salvimateus
Copy link

If you are using zsh, must add following in ~/.zshrc

export NVM_DIR=~/.nvm [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"

WOW, I needed this! Thank you!

@schnoggo
Copy link

Yup. chmod solved my install issue.
Should probably be added to the install script.

@hscheuerle
Copy link

Feels much nicer than git bash, was running into issues with it after what was probably windows updates.
Enabling wsl with the scripts in Microsoft's guide worked better for me.

@pnosov
Copy link

pnosov commented May 20, 2020

"Your windows hard disk drivers are mounted in linux..."
It's "drives", not "drivers".

@marciojg
Copy link

Hi guys, I did have this problem. Anybody did have too? And anybody know how to solve it? Thanks

devel@DESKTOP-GMOMRKS:~$ nvm install --lts
Installing latest LTS version.
Downloading and installing node v12.16.3...
Local cache found: ${NVM_DIR}/.cache/bin/node-v12.16.3-linux-x64/node-v12.16.3-linux-x64.tar.xz
Checksums match! Using existing downloaded archive ${NVM_DIR}/.cache/bin/node-v12.16.3-linux-x64/node-v12.16.3-linux-x64.tar.xz
mv: cannot move '/home/devel/.nvm/.cache/bin/node-v12.16.3-linux-x64/files/bin' to '/home/devel/.nvm/versions/node/v12.16.3/bin': Permission denied
mv: cannot move '/home/devel/.nvm/.cache/bin/node-v12.16.3-linux-x64/files/include' to '/home/devel/.nvm/versions/node/v12.16.3/include': Permission denied
mv: cannot move '/home/devel/.nvm/.cache/bin/node-v12.16.3-linux-x64/files/lib' to '/home/devel/.nvm/versions/node/v12.16.3/lib': Permission denied
mv: cannot move '/home/devel/.nvm/.cache/bin/node-v12.16.3-linux-x64/files/share' to '/home/devel/.nvm/versions/node/v12.16.3/share': Permission denied
Binary download failed, trying source.
Local cache found: ${NVM_DIR}/.cache/src/node-v12.16.3/node-v12.16.3.tar.xz
Checksums match! Using existing downloaded archive ${NVM_DIR}/.cache/src/node-v12.16.3/node-v12.16.3.tar.xz
$>./configure --prefix=/home/devel/.nvm/versions/node/v12.16.3 <
./configure: 4: exec: python: not found
nvm: install v12.16.3 failed!

@edboyledev
Copy link

Flawless, fantastic, you saved me a lot of reading and hours of my day!

Thanks!

@gibsonbm
Copy link

Thank you very much, the fix worked like a charm.
Has anyone figures out why installing nodejs through apt-get doesn't always work ? I mean we are installing one single version right ?

@jcafowler
Copy link

Hi guys, I'm running into an issue where nvm is not found, but if i type source ~/.nvm/nvm.sh and then type nvm it works fine. When I restart the terminal it goes back to command not found: nvm

@lyctw
Copy link

lyctw commented Aug 27, 2020

Hi guys, I'm running into an issue where nvm is not found, but if i type source ~/.nvm/nvm.sh and then type nvm it works fine. When I restart the terminal it goes back to command not found: nvm

What shell are you using?

@nchhillar
Copy link

Thank you so much for this, Worked like a charm! 👍

@noygal
Copy link
Author

noygal commented Sep 12, 2020

Added instructions to WSL2 and a solution to the command not found: nvm problem that a lot of people seem to encounter.

@NiiBotchway
Copy link

Splitting into 2 lines should get rid of that error.

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

You will then need to run source ~/.zshrc to update ZSH.

This worked for me.. Thanks

@danish505
Copy link

Thanks it works perfectly.

@cheng-alvin
Copy link

Thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment