Running .NET Core on Windows Subsystem for Linux

Build .NET Core apps on Windows, run it on Windows Subsystem for Linux.

Introduction

If you do not know what Windows Subsystem for Linux (WSL) is, the short answer is that it allows you to run Bash on Ubuntu on Windows.

I will share my development environment setup where I do the following:

  • Build .NET Core apps on Windows
  • Publish it for Ubuntu 16.04 x64 runtime
  • Run it on Windows Subsystem for Linux

Motivation

I needed to build apps on Windows then publish it to a Linux box to ensure that it works. Publishing it to a Linux VM on cloud is an option although ideally I would prefer the ability to run it locally. Running a Virtual Machine was my preferred option but it is slow to startup, uses more RAM, takes up a lot more disk space, etc. Then I decided to try WSL and it seems to work great.

Installing Windows Subsystem for Linux

Please refer to Bash on Windows installation guide. If you have WSL installed prior to Windows 10 Creators Update, the version of Ubuntu installed is 14.04. I do recommend that you uninstall and reinstall WSL to get Ubuntu 16.04. Then verify the version by running lsb_release -a in Bash and you should be seeing:

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.2 LTS
Release:        16.04
Codename:       xenial

Installing .NET Core into Windows Subsystem for Linux

Run the following commands (copied from .NET Core installation guide):

sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ xenial main" > /etc/apt/sources.list.d/dotnetdev.list'
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 417A0893
sudo apt-get update
sudo apt-get install dotnet-dev-1.0.1

Installing .NET Core for Windows

Download and install .NET Core SDK.

Building .NET Core apps on Windows

.NET Core SDK can generate basic Hello World examples so there is no need to Git clone an example to run. Just follow these simple steps on Windows Command Prompt or PowerShell:

  1. Create a folder and change the current directory to that. For example sake, create DotnetCoreSample on your Desktop.
  2. Generate a new Web template: dotnet new web
  3. Restore NuGet packages: dotnet restore
  4. Run: dotnet run

Browse http://localhost:5000 and you should be seeing Hello World.

Publishing it for Ubuntu 16.04 x64 runtime

From WSL, you can access Windows files and folders.

For example, you can reach C drive using:

$ cd /mnt/c/

Note: The forward slashes

If you are unfamiliar with Linux, here are few hints:

  1. Change directory: cd just like Command Prompt except that you need to use forward slash (/).
  2. List directory: ls whereas Command Prompt is dir.

To run an app on Linux (in this case Ubuntu 16.04 x64), we need to build/publish it for that specific environment:

dotnet publish --framework netcoreapp1.1 --runtime ubuntu.16.04-x64

Running it on Windows Subsystem for Linux

Assuming your Username is JohnDoe, change Bash current directory using:

$ cd /mnt/c/Users/JohnDoe/Desktop/DotnetCoreSample/bin/Debug/netcoreapp1.1/ubuntu.16.04-x64/publish/

To run it:

$ ./DotnetCoreSample

Browse http://localhost:5000 from your browser in Windows and you should be seeing Hello World.