Once there was a time where Microsoft and Linux were enemies and who was developing in Windows didn't develop for Linux and vice-versa. Time has passed and things have changed. And how they have changed!
Multi-platform development has evolved, Microsoft released .NET open source, and you can run it both on Linux and Mac (besides Windows, of course). You can use VS Code to develop your code in any of these platforms, and VS Code isn't restricted to .NET Languages: you can develop in Python, Perl or even Clipper/Harbour, Cobol or Fortran. What a change!
Besides that, you can run a Linux distro directly in Windows, with no need of any VM, dual boot or Live CD. The key for that is WSL, the Windows Subsystem for Linux, that allows you to run a full Linux environment, including any Linux application without modification (you can even run Linux graphical apps with WSLg, that's in test - the image below shows GIMP running on Windows)
And, just to make sure, it's not an emulation of Linux. You are running the full distro directly on Windows. WSL uses a translation layer between Linux and Windows, to translate the calls between both OSs and WSL2 uses a Linux kernel for its magic. You can compare both versions of WSL here.
To develop in Linux, the first thing is to get Windows Terminal. This is the successor of the old Windows command line prompt and offers many improvements: customization, tabbed interface and it allows you to use multiple shell types: Windows command, Powershell, Linux prompt or even an Azure Cloud Shell. If you are using Windows 11, you already have it, as it's installed by default. If you are still using Windows 10, you can get it from the Microsoft Store.
Once you have Windows Terminal installed, you must install WSL. Just open a command prompt window and type
wsl --install
This will install WSL in your machine. If that doesn't work, you will have to install it manually, using the installation steps listed here.
With WSL installed, you need to install your preferred Linux distro. You can get it from the Microsoft Store:
Or you can install it directly from the command prompt. wsl --list
lists all the available distros in your machine (you can install and use many distros) and wsl --list --online
lists the available distros online:
To install an online distro, you must use
wsl --install -d <Distro>
If the distro isn't available online or in the store, you still can install it, by getting the appx packages an manually installing it, as described here. One other way is to check if there is an alternate way to install it in WSL. For example, to install Linux Mint, you can go to the LinuxMintWSL Github and download the installer for it.
When you have WLS and the distros installed in your machine, you can click on the down arrow in the Terminal title bar and it will show them in the list, so you can select and open a command prompt for that distro:
The next step is to install dotnet 6.0. To do that, you should follow the instructions for your distro in
For Linux Mint (based on Ubuntu), you should run these commands on the bash prompt:
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
Once installed, you can get the installed version with
dotnet --version
Now, you can create and run a dotnet program in Linux. To show the available app types to create, you can run
dotnet new
This will list the common templates for a new app. To list all templates, you should use
dotnet new --list
We can create a new console app using this command:
dotnet new console -o simple-console
This command will create a new console app in the simple-console folder. We can change to this folder with cd simple-console
and run the program with
dotnet run
This is already great but, unless you are already a VI or Emacs guru (which I am not 😃), you will have some trouble to modify it. I personally, think that the best way to modify a Linux program in WSL is to use VSCode. By typing code .
in the command prompt, VS Code server will be downloaded and installed automatically and VSCode will open in your desktop
In this case, you won't have VS Code for Linux running. When you are using VS Code from WSL, something different happens: VS Code will be split into two parts, a server (which runs on Linux) and a client (that runs on Windows). You will be editing the files on Windows, but saving them in Linux. You can see in VS Code status bar, at the bottom WSL: Mint, indicating that we are editing files in WSL. If you open a terminal in VS Code with Ctrl+`, you will see that it's a bash prompt and not a command line or a Powershell prompt.
We can edit program.cs to show the OS version with:
Console.WriteLine($@"Hello World!
Operating System: {System.Runtime.InteropServices.RuntimeInformation.OSDescription}");
When we save and run the program we will get
This is still great, but I doubt that's what you want to do when your are developing a Linux app. Many times, we'll want to develop a web api to serve our data. For that, we must change to the parent folder and create a new app with
dotnet new webapi -o webapi
If you change to the webapi folder and run it with dotnet run
, you will see the endpoints for the API:
In our case, if we open a browser window and type
https://localhost:7219/swagger
We will see the swagger interface for our api:
We can test it and see that it works fine:
But we can also use the curl command to get the data. In the bash prompt, we can type
curl -X 'GET' \
'https://localhost:7219/WeatherForecast' \
-H 'accept: text/plain'
to get the api data (I got this command line from the swagget test window). If you do that, you will see a certificate error. We didn't set the certificate for the web site, and we can bypass that with the -k flag:
curl -k -X 'GET' \
'https://localhost:7219/WeatherForecast' \
-H 'accept: text/plain'
As you can see, we are getting the json data for the weather forecast sample. You can still consume the api from our console app. All we have to do is change the program.cs file to
using System.Text.Json;
var handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback =
(message, cert, chain, errors) =>
{ return true; };
using var client = new HttpClient(handler);
var json = await client.GetStringAsync("https://localhost:7219/weatherforecast");
var forecasts = JsonSerializer.Deserialize<List<WeatherForecast>>(json);
if (forecasts == null)
return;
foreach (var forecast in forecasts)
Console.WriteLine($"{forecast.date:dd/MM/yyyy} {forecast.temperatureC} {forecast.summary}");
public class WeatherForecast
{
public DateTime date { get; set; }
public int temperatureC { get; set; }
public int temperatureF => 32 + (int)(temperatureC / 0.5556);
public string? summary { get; set; }
}
If you run the program, you will see something like this:
We are consuming the API from our console program in Linux, but we are running .NET, we can run the same program in Windows. One nice thing in WSL is that we can run Windows apps from Linux. If you type
explorer.exe .
It will open Windows Explorer with the current path open. If you copy the address in the address bar, you will see:
\\wsl.localhost\Mint\home\bruno\LinuxProg\simple-console
As you can see, the Linux folder is considered a new network. We can map the drive in Windows to use it, with a command like this one:
net use y: \\wsl.localhost\Mint
If you open a command prompt and type this command, you will have the y: drive available to use. You can then change to the program folder and type dotnet run
to run the same program in Windows. If you do that, you will see that we can run the same program we've created in Linux in the Windows command prompt and consume the API that is still running in Linux.
As you can see, with .NET you can run the same program in Linux or in Windows and you can interact with the APIs that are running in Linux. You will be using the tools that you still know. You can even access one file system from the other seamlessly (the Windows file system is mounted in /mnt/c/ in Linux). You will be using the same C# language that you know and, most of the time, you don't even need to know Linux APIs - .NET will take care of that for you.
The full code of this article will be in https://github.com/bsonnino/LinuxProg