Skip to content

Instantly share code, notes, and snippets.

@kenmori
Created May 30, 2021
Embed
What would you like to do?
Windowsと
[ref](https://gist.github.com/noygal/6b7b1796a92d70e24e35f94b53722219)
# Windows 10 Fall Creators Update - Installing Node.js on Windows Subsystem for Linux (WSL)
Windows just released the [windows subsystem for linux](https://msdn.microsoft.com/en-us/commandline/wsl/about) feature to the public with its latest windows fall creator update, if you are not familiar with this feature it allows you to run linux binaries natively on windows - [F.A.Q](https://msdn.microsoft.com/en-us/commandline/wsl/faq).
## Enabling WSL
The feature is not enabled by default and you need to activate it, you can do it via powershell (with admin rights):
```
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
```
Or you can open: Control-Panel -> Programs -> Turn Windows feature on ro off, and click the "windows subsystem for linux" button.
Now to install your linux version, again two options, you can either install from Windows store (search linux) or you can turn on developer mode and open it via the command line:
Open Settings -> Update and Security -> For developers, and Select the "Developer Mode" radio button.
## Installing linux (ubuntu)
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, the default installation in WSL is ubuntu and I recommend to keep it as it one of the most popular distribution around. If you need different distribution the easiest why is to install it from the windows store.
[reference](https://msdn.microsoft.com/en-us/commandline/wsl/install_guide)
After installing the destribution via windows store click the "launch" button to launch the cli based installer wizard, the ubuntu image will automatically download and you'll be prompt to enter username and password.
Now you can run CMD.exe and type:
```
bash
```
That's it, you're running ubuntu on windows!
If you found yourself in need to reset/reinstall the ubuntu machine you can use `wsl.exe`:
```
wsl.exe --unregister Ubuntu
```
[reference](https://docs.microsoft.com/en-us/windows/wsl/reference) (wsl.exe tool also provide cli tools for WSL managment).
## 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:
```bash
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 build tool for [node-gyp](https://github.com/nodejs/node-gyp) - node binaries build tool.
```bash
sudo apt-get install build-essential
```
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](https://github.com/nodejs/Release) versions).
[NVM](https://github.com/creationix/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:
```bash
# This command install version 0.35.0, check the NVM reference it there are newer versions.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.0/install.sh | bash
```
Restart your terminal after install.
Now you can install the latest the latest LTS version of node:
```bash
nvm install --lts
```
To update node just run the command again.
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:
```bash
nvm use --lts
```
You can also add this command to the end of your `.bash_profile` file:
```bash
echo "nvm use --lts" >> .bash_profile
```
## Developing
Your windows hard disk drivers 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, missing needed tools can be installed via `npm` or `apt-get` for system tools.
IDE terminal integration support is still on its early days, [vscode](https://code.visualstudio.com/) offers support with minimal configuration, [guide](https://code.visualstudio.com/docs/editor/integrated-terminal). If your IDE just point to the executable (like cmd.exe) check if you can point it to `c:\Windows\System32\bash.exe`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment