DEV Community

Cover image for Development Environment in WSL2
Christian Köberl
Christian Köberl

Posted on • Updated on

Development Environment in WSL2

This article was published originally on derkoe.dev.

This guide shows how to setup a full development environment including UI apps (X11) in WSL2 on Windows 10. WSL2 enables a "full" Linux development environment in Windows.

Why?

The first thing you might ask is: why? First, we have to run Windows on our machines - so the all-in Linux does not work. Second, Linux is a lot faster for building software than Windows - our biggest app compiles in 13 minutes under Windows and in just 2 minutes under WSL on my machine which is a massive improvement.

Enable WSL2 and Install Ubuntu

For WSL2 to work you will need a Windows 10 build 19041 (aka 20H1) or higher (any edition will work, even Home). Then simply follow the guide from Microsoft.

TL;DR:

  1. Enable the features WSL and VM-Platform in Powershell as Admin:
   dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
   dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  1. Restart Windows
  2. Set WSL2 as default:
   wsl --set-default-version 2
  1. Go to the Microsoft Store and install "Ubuntu"
  2. Launch Ubuntu from Start menu (this will ask you for a user account)

You can now re-launch a shell in your Ubuntu instance with "Ubuntu" from the Start menu or execute wsl from the command line. The WSL command line tool has many options which you can find with wsl --help or in the Command Reference.

Update and Install Software

Inside your Ubuntu Linux update all packages:

sudo apt update
sudo apt upgrade

Then install packages you need for your development - in my case it is Java 11, Maven, Node.js/NPM and git:

sudo apt install openjdk-11-jdk openjdk-11-source maven nodejs npm git

Do not install Docker CLI or Docker Compose because these are provided by Docker Desktop (see next section).

Install Docker (optional)

Most developers need Docker for their local setup - the newest Docker Desktop version supports WSL2 out of the box (even on Windows Home).

GUI Apps

Currently there is no support for GUI apps for WSL - Microsoft has announced that they are working on GUI support using Wayland and that it should ship in 2020. So you have to use X11 forwarding or other tricks to enable this - I have tried all of them and there are some issues with any of the solutions:

  1. X11 server on Windows (VcXsrv or X410) and setting the DISPLAY variable in Linux (export DISPLAY=ip route show | grep 'default via' | awk '{print $3}':0.0 in your .bashrc).
    • Works quite well - good performance, shortcuts work, even mouse back/forward buttons work as expected
    • The main problem is that when you go to standby or hibernate the connection between Windows and WSL breaks and all your apps stop. If you can live with that go with this solution!
  2. Running xrdp on Linux and using Remote Desktop to connect
    • In this case you are running a full desktop - that means you have to install Xfce (Gnome does not seem to work).
    • With this solution the whole UI feels a bit laggy and slow - I tried to tune the xrdp params without success.
  3. Xpra - a virtual X11 server to connect via a client on Windows (or other platforms)
    • The solution works but there are many issues like crashes, window positions are completely wrong, extra mouse buttons do not work, etc.
  4. X2Go - also a virtual X11 server with an Windows client
    • This seems to be the most promising solution
    • As with plain X11: good performance, shortcuts work, even mouse back/forward buttons work as expected, plus the reconnect feature
    • The only drawback is the quirky Windows client (but I got used to it)

X2Go

Here is the X2Go setup in more details (since it worked best for me).

Initial setup

  1. Fix SSH host keys
   sudo apt-get remove --purge openssh-server
   sudo apt-get install openssh-server
   sudo service ssh --full-restart
  1. Install X2Go on your Linux distribution
   apt install x2goserver
  1. Download and install the client for Windows.

  2. Configure the

    • Host: localhost
    • Login: <your user>
    • Session type: Published Applications

After each WSL/Windows restart

  1. Launch ssh in Linux (if not started yet): sudo service ssh start
  2. Launch "X2Go Client" on Windows ad connect to the server with user/password
  3. Now you can launch X11 apps via the tray icon (see X2Go Published Applications)

Editors/IDEs

Visual Studio Code

Simply install VSCode and then you can open it from the Linux shell with code or code <path> - most of the time it will be:

code .

IntelliJ IDEA

On a normal Ubuntu you could install IntelliJ IDEA via Snap but Snap does not work on WSL2 (there is a hacky workaround).

So, the easiest way is to install it manually:

  1. Get the latest tar.gz from the downloads page
  2. Extract it an create a symlink (so you can easily switch versions):
   cd /opt
   sudo tar xzf /mnt/c/Users/$USER/Downloads/idea-{edition-version}.tar.gz
   sudo ln -s /opt/idea-{edition-version} /opt/idea
  1. To easily launch the app via the X2Go context menu create an app desktop entry /usr/share/applications/intellij-idea.desktop with the following contents:
   [Desktop Entry]
   Name=Intellij IDEA
   Comment=JetBrains Intellij IDEA Java IDE
   Categories=Development
   Keywords=java;ide
   Exec=/opt/idea/bin/idea.sh
   Type=Application
   Icon=/opt/idea/bin/idea.png

Open Issues

There are still many issues with WSL2 - the biggest problems are:

Resources

Latest comments (2)

Collapse
 
shivammukherjee profile image
Shivam Mukherjee • Edited

I followed your instructions for x2go but somehow couldn't get gedit to open, it spits:

Unable to init server: Could not connect: Connection refused

(gedit:2439): Gtk-WARNING **: 17:22:21.798: cannot open display: <my-ipv4-address>:0.0

I even tried setting DISPLAY - your export command had an error it said, so I used this instead:

export DISPLAY=$(grep -m 1 nameserver /etc/resolv.conf | awk '{print $2}'):

I've tried setting the DISPLAY variable with the host's IPv4 address to no avail - it simply displays the provided address after cannot open display:.

When I set the port to 22, nothing shows up.

Of course, using the Published Applications menu works - how do I add Godot Engine to this list? I tried installing it but running it throws an error saying that X11 Display is not available.

Okay, I included a .desktop file in /usr/share/applications but when I launch Godot I get this in a dialog box:

Your system does not support XInput 2.
Please upgrade your distribution.

I confirmed that at this time there are no packages above version 1.6.3 for xinput for my distro, and that when I did apt install xinput it found this version.

Collapse
 
derkoe profile image
Christian Köberl

x2go creates it's own X display directly on the host - you can find the port via x2golistsessions. Then you can set the DISPLAY variable to use it:

export DISPLAY=:{PORT}