Create WSL2 Linux virtual machine(s) on Windows 10 and use it as a development environment

The purpose of this article

I started using WSL2 on Windows about 1,2 years ago. In the era of WSL1, I did not use WSL because it was not a complete Linux, and WSL2 is very good. It is the most native Linux virtual machine on Windows. It can let you Experience the authentic Linux system.

And because of its very good performance, it can run smoothly even on an ultrabook laptop, and it can be used as a development environment.

Just think about it, you no longer need to start a heavy VMware virtual machine as your development environment, and WSL2 and the Windows system itself also have some integration, for example: In the WSL2 virtual machine, /mnt/c is the C drive in your Windows system.

I am writing this article now so that I can deploy WSL2 more orderly in the future.

This is a series of things I will do when I get a brand new Windows 10 computer about setting up the WSL2 environment

👇👇

  1. Install WSL2

    Before installing WSL2, update Windows 10 to the latest version.

    Then you can just use the “Simplified install” approach, by the time so far, this method of is feasible. For example:

    wsl --install -d Ubuntu

  2. Set up GUI support for WSL2

    1. Install Mobaxterm on Windows 10
    2. Do the steps on this document - Run Linux GUI apps on the Windows Subsystem for Linux (preview)

    Since I will not join the Windows Insiders Program, so I will not be able to use the built-in Xserver of Windows 10, instead I will use Mobaxterm’s Xserver for WSL2.

    After installing the required libraries and apps of the above document, set the environment variables to tell WSL2 to use Mobaxterm’s Xserver as followings:

    # On WSL2:
    nano ~/.bashrc
    

    Append the following contents🔖:

    # Using WSL 2? Then use this instead.
    export DISPLAY="$(/sbin/ip route | awk '/default/ { print $3 }'):0.0"
    export LIBGL_ALWAYS_INDIRECT=1
    

    🔖For reference: https://mobaxterm.mobatek.net/documentation.html Search for “Does MobaXterm X server support OpenGL? How can I use hardware acceleration?” on the webpage.

    1. Open the NVIDIA Control Panel, configure to use GeForce graphic card as the GPU of Mobaxterm’s Xserver.

      Explicitly set Nvidia graphic card as GPU for Mobaxterm’s Xserver

    2. At the same time, the GPU is explicitly specified for Mobaxterm Xserver at the Windows level.

      Search for ‘gpu’ in Windows settings

      Explicitly configure GPU for Mobaxterm’s Xserver

  3. Set up VS Code in alliance with WSL2

    💡Strongly recommand you to read the “Tips and Tricks” in the above document, there are a bunch of useful SSH related tricks!

Appendix

When using WSL2 in a VPN environment, if you encounter a problem that the DNS cannot be resolved normally:

Raymond Yan
Raymond Yan
Technical Solution Consultant

I have previously worked as a Java development, Linux System Administrator, DevOps engineer. I’m a former Cloudera employee now doing tech support for HPE Ezmeral products.

Next
Previous