DEV Community

wx-yz
wx-yz

Posted on

Playing with Windows Subsystem for Linux (WSL)

Windows 10 comes with Windows Subsystem for Linux which provides a seamless experience for running Windows and Linux at the same time. Seamless being the keyword here. Allowing 1) run Windows commands using Linux 2) Run Linux commands through Windows 3) Run HTTP services and access as if everything is accessible through localhost etc. This can be handy if you're a developer and want to work on both Windows and Linux without jumping hoops through VMs.

Enable WSL

First, you need to enable the WSL. Settings > Turn Windows features on or off > Windows Subsystem for Linux. Click OK and restart.

Enable WSL Windows feature

Install a Linux distribution

Next you have to install a Linux distribution from the Windows Store. Let's install Ubuntu.

Install Ubuntu from Windows Store

After the installation, click on Launch and it's going to set up the distro and create a user. You will be asked to enter a username and a password. This will be the Linux user account that'll be used to login.

At this point we have a working Linux system. You can install all other Linux packages necessary for development through apt-get.

Install Windows Terminal (optional)

The new Windows Terminal provides quick access to multiple shells with a tabbed interface. You can install this through Windows Store. Here we're going to install it through Chocolatey (a popular package manager for Windows).

Start an administrative PowerShell and execute this powershell-fu (taken from https://chocolatey.org/install). Skip this step if you already have Chocolatey.

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
Enter fullscreen mode Exit fullscreen mode

Install Chocolatey

Type in

> choco install microsoft-windows-terminal

to install the new terminal. Reboot.

Running Windows commands in Linux

After installing Docker Desktop for Windows and enabling Kubernetes, you can run the .exe (any .exe for that matter) directly within Ubuntu.

> docker.exe run hello-world

Running Windows commands in Linux

Running Linux commands from Windows

Using the wsl command in Windows you can run any Linux command within a Windows command shell / powershell. Such as

> wsl lsb_release -a

Run Linux commands through wsl

Redirect output of a Windows command to a Linux command (piping)

You can also run one command in Windows and pipe the output to a command in Ubuntu which is very nice.

> Invoke-RestMethod http://www.mocky.io/v2/5ebc6fec3100007e005b0b67 | ConvertTo-Json -Depth 5 | wsl jq

Pipe output of one command to another

Using localhost to access services

Another convenience is you can use localhost for accessing services launched from either Windows, Linux, docker or kubernetes. As you can see below,

Start service in Windows, access from Linux

Starting up a simple Java service.

Start Java service in Windows

Accessing the service from Linux. We're just using localhost and the port 9090 (which my sample service is listening on)

Accessing the service from Linux

Start a service in Linux, access it from Windows

Let's create a simple Ruby on Rails project.

> rails new blog
> cd blog
> rails server
Enter fullscreen mode Exit fullscreen mode

Start rails server

Access from browser in Windows,

Access rails app from Windows

Deploy an app in Kubernetes

I've installed Docker Desktop for Windows (Edge) and have enabled Kubernetes. After that docker.exe and kubectl.exe can be executed from PowerShell as well as within Ubuntu without having to do anything.

Let's use following example app that runs Wordpress with MySQL on K8S to test out - https://github.com/kubernetes/examples/tree/master/mysql-wordpress-pd

Deploying K8S app

After we deploy everything the app is available through localhost:80 (look at output of kubectl.exe get services wordpress)

Access wordpress

In conclusion

WSL provide a nicer way of working with Linux without having to go through the trouble of installing VirtualBox, then installing a guest Linux OS, setup file share and access the system through an SSH server.

Seamless integration works really well when working with standalone commands, interactively piping from one tool-set to the other, working with simple HTTP services and working with containers on Kubernetes.

Top comments (0)