Setup WSL2 Ubuntu the right way

Satvik Dandale
5 min readOct 12, 2021

We have at least heard, if not participated in a good old Linux vs Windows debate. Let’s settle that once and for all.

If you have a love-hate relationship with Windows and if you’re not living under a rock, I am pretty sure you know WSL2 on Windows 10. But for those who still like to live the old-fashioned way, WSL(Windows Subsystem for Linux) is basically running a Linux kernel on a hypervisor on Windows 10. It has much less overhead as compared to running a Virtual Machine on Windows.

WSL includes most command-line tools and applications although you can add other tools that you like. There are many articles that can guide you through the installation of WSL2 Ubuntu including this one from Microsoft so I will explain how I set up my WSL Ubuntu after the installation.

If you follow along, you will have a complete GUI for Ubuntu and you will not have to use the command line for everything. Before starting the setup, I installed Windows Terminal as my terminal application. Please note that I have installed WSL2 with the latest Linux Kernel and Ubuntu 20.04.

You will also need to download VcXsrv.

Start your Ubuntu

If you have set up Ubuntu for the first time after installation, you can easily start Ubuntu as an app. But like I said, I will be using Windows Terminal to access the Ubuntu command line. Starting Ubuntu from Windows Terminal automatically opens the command line from the Windows home directory and not the Ubuntu home directory (which I personally prefer).

Starting Ubuntu from Windows Terminal
You can also use Ctrl+Shift+4 as a shortcut in Terminal

You can easily start Ubuntu 20.04 in Terminal by clicking on the down arrow at the top and selecting Ubuntu.

Update everything

Before we go ahead with the setup, let’s make sure we have the latest package lists and upgrade already installed packages to the latest versions.

sudo apt update && sudo apt upgrade -y

Speed up downloads and installations

We will download some large packages and even with good networks, these downloads take time. We can speed it up by downloading multiple packages at the same time in parallel.

Install axel

sudo apt install axel

This will install axel (a download manager) which will help download multiple files simultaneously by using multiple connections.

Install apt-fast

  1. Add custom PPA (Personal Package Archive) to the resources list
sudo add-apt-repository ppa:apt-fast/stable

2. Install apt-fast

sudo apt -y install apt-fast

This will ask for some configurations:

Package configuration of apt-fast
I use “apt” more often so I will use that.

First, it will ask which package manager to replace apt-fast with.

Notice that I have been using apt and I would like to use apt-fast instead of that.

The maximum number of connections to use at the same time.

Now it will ask for a maximum number of connections. You can just use 5 as default.

If you fancy taking things into your own hands, you can play around with the ${_MAXNUM} variable anytime!

Suppressing Confirmation Dialog

Finally, it will ask for suppressing confirmation dialog. You can select Yes.

Don’t worry there are no Terms and Conditions here that you are bluntly ignoring as you do for Google Services 😉

Configuring apt-fast

We are getting to the interesting part soon, I promise! Now let’s configure apt-fast just once.

  1. Open the apt-fast.conf file
sudo nano /etc/apt-fast.conf

2. Scroll down until you see mirrors configuration. Remove the comment on the first array of mirrors like this:

Configuration of mirrors for apt-fast
Configuring mirrors for apt-fast

And voilà, we are done configuring apt-fast and now we can finally get to the real stuff!

Install Gnome-Session

To get Native Ubuntu GUI, we will download the gnome-session desktop environment.

sudo apt-fast install ubuntu-desktop gnome

Make sure you use apt-fast because it WILL TAKE TIME! Feel free to take a nap if you’d like. Once it is done, we need to configure it before we can start our GUI session.

Open your .bashrc file which has all the commands that are executed at the startup:

nano ~/.bashrc

And paste these 2 lines at the end of the file:

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

And special thanks to this gist, we can just use a simple script which will do all the hard work for us. Please note that you might be using ubuntu in your Windows directory space. It's completely fine but if you don’t want ubuntu files in your windows workspace, use “cd ~” to navigate to the ubuntu home directory.

git clone https://github.com/DamionGans/ubuntu-wsl2-systemd-script.git
cd ubuntu-wsl2-systemd-script/
bash ubuntu-wsl2-systemd-script.sh

After successful execution of this script, we need to restart ubuntu. Open a new PowerShell window in Windows Terminal and type:

wsl --shutdown

You can go ahead and close the existing ubuntu tab and open a new one.

I hope you have downloaded VcXsrv already. Fire up the X server (XLaunch) and do these steps:

X server config step 1
The first step of X Server

Select One large window or One window without titlebar.

Put 0 in the display number.

X server config step 2
The second step of X Server

In this screen, proceed with default. We will start the client manually.

X server config step 3
The third step for X Server

This is an important step. Make sure you check the “Disable Access Control” option else our gnome-session client will not be able to reach Server X.

And click finish in the last step.

After you finish these steps, you will see a black window with nothing in it. We will start our gnome-session inside this window now.

Go to Ubuntu terminal and type:

sudo gnome-session

Wait for the gnome to connect and you’re done!

Ubuntu GUI Screen shot
Now you literally have Ubuntu in your Windows!

You can use a different GUI experience if you want. One that I have personally used before is kubuntu-desktop. Refer to this for installation steps.

References:

  1. WSL2 Gnome Desktop Configuration
  2. Installing and configuring apt-fast
  3. Our friend Google

--

--