Develop graphical Linux apps with Avalonia UI and run them on WSL2

Develop graphical Linux apps with Avalonia UI and run them on WSL2

In the past I have already blogged about how to develop a graphical application on Windows and run it it WSL.
Some years have passed, and now with WSL2 it's now possible to run graphical applications directly: Run Linux GUI apps on the Windows Subsystem for Linux.

First of all, the WSL2 installation process has also been simplified compared to the past, as documented in Install Linux on Windows with WSL.

  • From a command prompt, run:
    wsl --install
    
    wsl_1
  • This will required administration privileges, and after its completion, a reboot will be necessary:
    wsl_2
  • After the reboot, the installation will continue, asking for user-name and password:
    wsl_3
  • Just for reference, it's possible to delete the current installation with:
    wsl --unregister ubuntu
    

To create our first application, we need to follow the guide at Create a cross platform solution:

  • Download the Avalonia templates:
    dotnet new install avalonia.templates
    
  • DON'T create the solution folder (it's automatically created)
  • Create the application with:
    dotnet new avalonia.xplat --name AvaloniaCross
    
  • Then ensure to have all the necessary dotnet workloads (ios, android and wasm-tools):
    cd AvaloniaCross
    dotnet workload restore
    

At this point, we can open the application and set the AvaloniaCross.Desktop as the starting project:
Avalonia_1

When we run it,
Avalonia_2

the application will start under Windows:
Avalonia_3

As we change to WSL,
Avalonia_4

because it's a fresh installation, Visual Studio will detect that it misses dotnet and will propose to install it:
Avalonia_5

If you will miss it, don't worry: Visual Studio will detect it again:
Avalonia_8

After installing the runtime, it will ask to re-run the application:
Avalonia_9

Trying to run the app now, you will get an exception:
Avalonia_6

Honestly I got stuck here, and I opened a discussion in the Avalonia UI GitHub repo.

Luckily I got an answer: some dependencies are missing, and they must be installed in the Ubuntu virtual machine:

sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y && sudo apt-get autoremove -y
sudo apt install libice6
sudo apt install libsm6
sudo apt install libfontconfig1

Once these are installed, the application can finally start!

Avalonia_7