Hello fellow Windows-using web developers. How you doin’?
I know I know, life isn’t easy. All those Mac users making fun of us with our uncool laptops and operating system that shared a womb with IE6. The Linux people can’t even look at us without chortling, I’m sure they’re making puns that involve obscure commands behind our backs.
We know that Windows is the best looking and fastest operating system, but they don’t seem to care. Apparently being able to type ‘grep’ is more important than all that.
Well happy days, comrades! You can now run Linux Bash in Windows and get
ssh and whatever else might float one’s boat. Right there in your favourite little black window.
Since you’ve no doubt already been distracted by the gaudy gif below, I’ll explain what’s happening. I am opening command prompt (Windows), entering Bash (Linux), running a Node server (Linux) on localhost and opening that in Chrome (Windows).
It’s called WSL, or Windows Subsystem for Linux if you need a tongue workout. There are some system requirements. You’re smart, you’ll work out what they are.
WSL in action
With the flashy gif out of the way, let’s slow things down a little and see what’s happening.
I start with a good old fashioned command prompt, sexy as always. I run
node which of course runs the instance of Node that I have installed in Windows. I use the
platform() method of Node’s
os module to print the platform name so you believe me that this is Windows.
I then exit Node and run
bash to step into Linux. Now I run
node again, but I’m in a Linux bash, so it’s the instance of Node I have installed for this Linux instance (we’ll get to the instructions below, settle down).
Now when I type
os.platform() I see that I’m on Linux. Amazing!
Ctrl+D will break out of Node, then again will break me out of Linux and back to the Windows command prompt.
This pleases me a great deal, so I wrote a poem:
Runnin’ Node in Windows
Runnin’ Node in Linux
Oh I’m runnin’ Node in Windows
[cracks whip] [cracks whip]
I’m going to assume that you’re more excited than you’ve ever been and want to know exactly how you can partake in such wonderment.
Well alright then…
Step by step…
- Hit start (I assume you’re ready to go)
- Type “windows features”
- Hit Enter
- Tick the Windows Subsystem for Linux box
- Click OK
- Hit Start
- Type “powershell”
- Right click it and click Run as administrator
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
- Hit Enter
- Restart. This isn’t one of those things where it says you need to restart but you don’t really need to restart. You really need to restart.
After the restart….
- Hit Start
- Type “cmd”
- Hit Enter
- In the command prompt type
bashand hit Enter.
This is how you launch the instance of Linux. Since it’s the first time you’ve done this, it will kick off the download of the Ubuntu image from Canonical (which is the non-GUI ‘user-mode Linux’, similar in concept to Windows Server Core). In future, you can access Linux bash in one of two ways:
- Open a command prompt and type
- Run the dedicated Bash on Ubuntu on Windows app which I snuck in and installed while you were reading my poem
You now have a nice clean install of Linux ready to go, so let’s…
Installing Node is exactly the same as installing Node on any Linux box, specifically, type these two lines, as explained in the Node docs:
curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
sudo apt-get install -y nodejs
(Or whatever version you want.)
Now you’ve got Linux and Node. This is easy, right?
So what exactly is happening here?
As soon as you type
bash and hit enter, you’re essentially in Linux. There is the usual Linux directory structure of course, but in addition to this you’ll find your Windows directories mapped to
/mnt. For example, if you have a project in
C:/web/my-project you would find this in
/mnt/c/web/my-project. Windows helps you out by keeping your working directory the same when you change into Linux. So if you’re in the command prompt at
C:\Users\David and type
bash, you’ll be taken to
/mnt/c/users/david in Linux.
I found it all a bit mind bending at first; the trick is not to think about it.
You want more?
If you’re super excited about how this works under the hood, there’s quite a few blog posts from Microsoft about it. If you have questions and suspect they are asked frequently, click right here. If you’ve got a problem, check out their GitHub issues, it’s quite an active little place.
What are some random tips on accessing the command prompt?
From Explorer with mouse
If you’re in Explorer looking at your project, hold shift, right click and select Open command window here
From Explorer — weird shortcut
If you’ve got your project folder open in Explorer, hit
alt+D then type
cmd and hit Enter.
Weird, right? I discovered this cleaning my keyboard one day.
Set the default path for a command prompt
Open a command prompt, then in the taskbar, right click the icon, then right click Command Prompt (right click inception) and click properties.
You can then enter a value for Start in, which is the directory command prompt will … start in.
Don’t forget to pin that icon to the taskbar. Now when you click the icon to open command prompt it will open in the correct directory.
Can I set this as my default terminal in WebStorm?
Yep. With some caveats.
How does this interact with the network and filesystem?
I have no idea.
Do all of the filesystem/network feature of NodeJS work with this?
Look, behind you! Is that a bear? Oh my god I think that’s a bear!
Will it work with file watching and hot reloading?
[Looks at watch] I’m afraid that’s all we have time for today, thank you all for coming.