In my previous blog post, Getting Started with Terraform and IBM Cloud, I used the Windows Subsytem for Linux (WSL) as my development environment. In this post I want to explore WSL a little more and show how easy it is to setup a WSL development environment with Visual Studio Code.
Prerequisites
- Windows 10 version 2004 (Build 19041) or later
- Visual Studio Code
Getting Started
View available Linux distro that you can install by running the following command from an Administrator command-line.
wsl --list --online
You can enable, download and install WSL using the Ubuntu distro by default running a single command.
wsl --install
Alternatively, you can specify a particular distro.
wsl --install -d <Distro>
wsl --set-default <Distro>
You can enter the distro by running the WSL command, optionally specifying the distro.
wsl <Distro>
I highly recommend installing Windows Terminal to allow you to easily access the WSL command-line.
I will go ahead an install an instance of Ubuntu 20.04 (LTS).
wsl --install -d "Ubuntu-20.04"
wsl --set-default "Ubuntu-20.04"
When prompted, enter a username and password for your Linux distro.
Open Visual Studio Code. If you have not already done so, download it here. Navigate to Extensions (Ctrl+Shift+X
). Find and install the extension for Remote - WSL. This extension will allow us to work in our Linux environment directly in Windows!
Launch your WSL command-line from Windows Terminal, or switch from Powershell by issuing the wsl
command.
Open Visual Studio code from the Linux command-line, as you normally would in Windows.
code .
This will trigger a one-time installation of Visual Studio Code Server, which enables communication between Windows and the WSL environment.
I am now remotely connected as my Linux user, wsluser
, to my Ubuntu 20.04 environment.
.NET SDK
Next, let's install the .NET SDK directly from the Terminal window in VS Code. Copy the instructions as documented in Install .NET on Ubuntu - .NET | Microsoft Docs. When prompted, enter the password to run the commands that require sudo
(administrator) privileges.
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
sudo apt-get update; \
sudo apt-get install -y apt-transport-https && \
sudo apt-get update && \
sudo apt-get install -y dotnet-sdk-6.0
Create a Web application.
dotnet new web -o SampleApp
Navigate to SampleApp/Program.cs
and replace the "Hello World!"
text with System.Runtime.InteropServices.RuntimeInformation.OSDescription
, which will show a description of the host operating system instead.
Launch the application from the terminal.
cd SampleApp/
dotnet run
Access the application by opening the URL printed to the console in your browser. For now, ignore the SSL certificate error.
You can clearly see the application is running under a Linux host, even though we are using VS Code in Windows and accessing the application in our local browser.
To fix the SSL certificate issue, we need to generate a localhost certificate on Windows, export it, and import the certificate into the WSL, as documented here.
In a Windows command-line run:
dotnet dev-certs https -ep c:/temp/https.pfx --trust -p password
We can access the Windows filesystem in Linux from the /mnt
directory, so we can easily import the certificate from a WSL command-line.
dotnet dev-certs https --clean --import /mnt/c/temp/https.pfx -p password
Run the application again, notice that there is no longer an SSL certificate issue.
You can easily unregister an instance of WSL.
wsl --unregister <Distro>
Use the list command to see what you currently have installed.
wsl --list
In this blog post we installed a WSL distro and setup VS Code for remote development. We created a sample .NET Core Web application, made code changes in VS Code as we normally would, deployed and tested the application, which clearly showed that it was running under a Linux host. Finally, we resolved the SSL certificate issue between Windows and the remote WSL environment.
WSL environments are easy to create, destroy and recreate, which make them an excellent option for development.
#wsl#Windows#Linux#VsCode