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:
- Increased file IO performance - file operation like ``git clone
,
npm install`, etc' could be up to 20x faster compared to `WSL` 1. - Full system call compatibility - the long awaited
docker
support!
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.
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.
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.
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.
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
.
What shell are you using?