Translated from haydenb's article "Getting started with cross-platform development using .NET on Ubuntu on WSL" on June 3, 2020 1
.NET is an open source software framework for building cross-platform applications on Linux, Windows and macOS. Ubuntu 2 on WSL allows you to build and test applications for both Ubuntu and Windows at the same time. What happens when we merge these together? This blog will demonstrate how to install the .NET development stack on WSL and build a simple OS-aware application, and then test it on Linux and Windows.
Enable WSL 1
Start PowerShell as an administrator and run:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
If you only want to install WSL 1, you can restart your computer and skip the next step.
Restart-Computer
If you want to install WSL 2, please do not restart, continue to the next step:
Enable WSL 2 (Windows 10 2004+)
For more details about Ubuntu on WSL 2, please check " Ubuntu on WSL 2 Is Generally Available " 3 .
Start PowerShell as an administrator and run:
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Then restart the Windows operating system:
Restart-Computer
Install Ubuntu on WSL
Download Ubuntu from the Microsoft Store:
Ubuntu 20.04 LTS on the Microsoft Store 4
To learn more about how to install Ubuntu on WSL , please check Ubuntu on WSL wiki page 5 .
Install Windows Terminal
Download Windows Terminal from the Microsoft Store:
Windows Terminal on the Microsoft Store 6
You can also download Windows Terminal from GitHub .
Run Ubuntu on WSL
Open Windows Terminal and run:
ubuntu.exe
When you run Ubuntu on WSL for the first time, it will install and prompt you to create a Linux user, which is independent of Windows users.
Exit and reopen Windows Terminal, you will find Ubuntu appears in the drop-down menu:
You can set Windows Terminal in settings.json and set Ubuntu as the default item.
Update Ubuntu on WSL
You should check for updates regularly and run the upgrade in Ubuntu on WSL. We use apt (Ubuntu package manager) to achieve.
To check for updates, run:
sudo apt update
To get the upgrade, run:
sudo apt upgrade
You can use &&
them to connect in the same row and add a -y
label, automatic updates and application upgrades available:
sudo apt update && sudo apt upgrade -y
Add Microsoft's .NET repository and signing key
We need to add Microsoft's .NET repository and signing key to apt. We will download and install a package from Microsoft to do this.
Please make sure you are installing the correct repository for your version of Ubuntu. You can check the current version of Ubuntu with the following command:
cat /etc/os-release
The example below uses Ubuntu 20.04, the latest LTS release from Canonical. If you are still using Ubuntu 16.04, 18.04 or 19.10, you can find the corresponding resource library in Microsoft Document 7 . To learn more about the difference between LTS and intermediate versions, we have a release cycle page 8 .
Download Microsoft's resource library and key package for version 20.04:
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
Use dpkg -i to manually install the Microsoft Resource Pack:
sudo dpkg -i packages-microsoft-prod.deb
Now when you update apt, you will see that the Microsoft Resource Library has been checked and upgraded:
Install .NET SDK
Use apt to install .NET and related dependencies from the Microsoft repository:
sudo apt-get install dotnet-sdk-3.1 -y
New workspace
Create a new working directory and open it:
mkdir dotnetproject
cd dotnetproject/
Create a new .NET project
Use dotnet new
to create a new .NET console project, which will create a named Program.cs
file and some other necessary folders and files:
dotnet new console
Explore our .NET applications
List the files in your new .NET project:
ls
Check Program.cs
the contents:
cat Program.cs
Run the sample program:
dotnet run
Customize our .NET application
Open in your favorite editor Program.cs
: vi, nano, emacs or VS Code with remote WSL extension :
Here, we use nano included in Ubuntu on WSL:
nano Program.cs
First, we add the Interop services namespace :
using System.Runtime.InteropServices;
Then put:
Console.WriteLine("Hello World!");
Replace with:
Console.WriteLine($"Hello {System.Environment.GetEnvironmentVariable("USER")}");
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Console.WriteLine("We're on Linux!");
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Console.WriteLine("We're on Windows!");
}
Console.WriteLine("Version {0}", Environment.OSVersion.Version);
This code can also be found here 9 .
This application tells us: the current user, check whether it is on Windows or Linux, and then give the OS kernel version.
Exit and save, then run:
dotnet run
Make our .NET application cross-platform
We need to update the .NET project file dotnetproject.csproj
to tell .NET to build for both Linux and Windows platforms.
Open our editor dotnetproject.csproj
and add:
<PropertyGroup>
<RuntimeIdentifiers>win10-x64;linux-x64</RuntimeIdentifiers>
</PropertyGroup>
This will guide .NET to build self-contained binaries for both Windows 10 x64 and Linux x64.
Build our cross-platform application
When we have configured the project, building a .NET application becomes so simple:
dotnet publish -r win10-x64
dotnet publish -r linux-x64
The project can be /bin/
found on each platform binary file folder and all of its necessary libraries self-contained:
ls bin/Debug/netcoreapp3.1/
Test the Linux version
You can directly run the Linux binary file as follows:
./bin/Debug/netcoreapp3.1/linux-x64/publish/dotnetproject
Test the Windows version
To run the Windows version, copy it to the Windows file system:
cp -r ~/dotnetproject/bin/Debug/netcoreapp3.1/win10-x64/publish /mnt/c/Users/Hayden/OneDrive/Desktop/
Translator's Note:
Here's/mnt/
root directory for the Ubuntu system, see the Windows file system,/mnt/c/
namely Windows system C drive.
Then run:
/mnt/c/Users/Hayden/OneDrive/Desktop/publish/dotnetproject.exe
So far, we have built and run the same application for Linux and Windows. We can use WSL to test them at the same time.
Author: haydenb
Translator: Technical Zemin
Publisher: Technical Verses
links: English text
https://ubuntu.com/blog/creating-cross-platform-applications-with-net-on-ubuntu-on-wsl Getting started with cross-platform development using .NET on Ubuntu on WSL ↩︎
https://ubuntu.com/wsl Ubuntu on WSL ↩︎
https://ubuntu.com/blog/ubuntu-on-wsl-2-is-generally-available Ubuntu on WSL 2 Is Generally Available ↩︎
https://www.microsoft.com/store/productId/9N6SVWS3RX71 Ubuntu 20.04 LTS on the Microsoft Store ↩︎
https://wiki.ubuntu.com/WSL Ubuntu on WSL wiki ↩︎
https://www.microsoft.com/store/productId/9N0DX20HK701 Windows Terminal on the Microsoft Store ↩︎
https://docs.microsoft.com/en-us/dotnet/core/install/linux-ubuntu ↩︎