DEV Community

loading...

WSL2, Docker, GUI

bzani profile image Bruno Zani Updated on ・4 min read

WSL and WSL2

Make sure that

  • You have Windows 10 version 1903 or higher, with Build 18362 or higher. If not, update it.
    • Check it by using Win+R, command winver
  • Virtualization is enabled inside of your computer's BIOS

Enable WSL on Windows 10

  • Execute the commands below, using PowerShell on Admin mode:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Enter fullscreen mode Exit fullscreen mode
  • Open your PowerShell once again and try typing wsl, and if doesn't work, restart your machine

Upgrade to WSL2

  • Download and install the WSL2 kernel to update

  • Set the WSL default version to 2, via Powershell:

wsl --set-default-version 2
Enter fullscreen mode Exit fullscreen mode

(in case of error, ignore it this time)

Linux Distro

Install the Ubuntu distro (or any other you like)

  • Open your Microsoft Store, search for the Ubuntu App, select the Ubuntu app (which is the latest) and install it
  • After installed, configure your root user and password

Upgrade WSL version for Ubuntu

  • List your distro using wsl --list, via Powershell
  • Set your distro to WSL version 2
wsl --set-version <distro_name> 2
Enter fullscreen mode Exit fullscreen mode

Tips

  • Access your WSL "machine" through the wsl command (via Powershell or cmd), which stands for:
C:\Windows\System32\wsl.exe
Enter fullscreen mode Exit fullscreen mode
  • Access your WSL "space" on Windows through: (Win+R or explorer):
\\wsl$
Enter fullscreen mode Exit fullscreen mode
  • Find your Windows "space" on Linux:
/mnt/c/
Enter fullscreen mode Exit fullscreen mode
  • You can also execute Windows programs from WSL (try explorer.exe, or code . for VS Code, for example), however it doesn't mean all of them will work alright

  • Have in mind that WSL performance is strictly related to Linux usage, i.e., using WSL to manage and work with your files in /mnt/c won't help

  • Find your WSL "disk" on Windows:

    • in the case of Ubuntu, we have something like this:
C:\Users\<User>\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu...\LocalState
Enter fullscreen mode Exit fullscreen mode
  • Backup your WSL state:
wsl --export <distro> <output_file>                        # export
wsl --import <distro> <installation_location> <input_file> # import
Enter fullscreen mode Exit fullscreen mode

or even, create a backup of the WSL "disk" file (previous item)

  • WSL2 uses almost all your machines resources, by default:

    • Disk and CPU
    • 80% of your available RAM
    • 25% of your available memory for Swap
  • You can set up limits, by creating a config file (example):

    • C:\Users\<User>\.wslconfig >
[wsl2] 
options=metadata,umask=22,fmask=11 
memory=8GB 
processors=4 
swap=2GB 
Enter fullscreen mode Exit fullscreen mode
  • Your custom configs are applied after a WSL restart:
wsl --shutdown
Enter fullscreen mode Exit fullscreen mode

Windows Terminal

Windows Terminal is a new feature from Microsoft, improving your experience with terminals in Windows.

Install via Microsoft Store, by searching for Windows Terminal

With Windows Terminal you can:

  • have multiple terminals opened in separate tabs (instances)
  • different terminals running along (Ubuntu, git bash, Powershell, etc)
  • customize it by applying themes, configuring colors and shortcuts, etc
    • change file settings.json by hitting Ctrl+, on Windows Terminal
    • OR, open file from default dir:
C:\Users\<User>\AppData\Local\Packages\Microsoft.WindowsTerminal...\LocalState\settings.json
Enter fullscreen mode Exit fullscreen mode
  • you can change e.g. your Linux starting directory and default shell, when opening Windows Terminal:
"defaultProfile": "{xxxxx-xxxx-xxxx-xxxx}",
...
{ 
    "guid": "{xxxxx-xxxx-xxxx-xxxx}",
    "name": "Ubuntu-18.04", 
    "startingDirectory" : "//wsl$/Ubuntu/home/<ubuntu_user>", 
}
Enter fullscreen mode Exit fullscreen mode

Docker

3 ways to use Docker on Windows

  • Docker Toolbox - bad

    • Uses Oracle VirtualBox
    • Very bad performance
    • Difficult setup and usage
  • Docker Desktop com Hyper-V - better

    • Uses Microsoft Hyper-V instead of VirtualBox
    • Requires Windows 10 PRO
    • Better performance but a heavy consumer of host resources
    • Docker will drop support in the future, as announced
  • Docker Desktop com WSL2 - recommended *

    • Uses Microsoft Virtual Machine Platform
    • Integrates with WSL2 and its distros
    • Better performance and consumes less resources
    • Usage as running on Linux natively

*Download and install Docker Desktop
https://hub.docker.com/editions/community/docker-ce-desktop-windows

Enable Docker on WSL2 distro
- Docker Desktop > Settings > Resources > WSL Integration
> Enable integration with additional distros > Check distro

Play with docker on WSL
- Open WSL and hit docker ps or try out docker run hello-world

WSL Linux GUI

Optional

A way to run Linux apps using WSL "builtin GUIs" is on the way, as one of the WSL improvements of Microsoft's roadmap.

While it still unavailable, we can still install a GUI and use RDP (Remote desktop), to access the Linux VM.

First of all, update and upgrade your Linux:

sudo apt update && sudo apt -y upgrade
Enter fullscreen mode Exit fullscreen mode

Install (or overwrite) X-RDP:

sudo apt-get purge xrdp
sudo apt install -y xrdp
Enter fullscreen mode Exit fullscreen mode

Install a GUI:

sudo apt install -y xfce4 # check 'gdm3' option
sudo apt install -y xfce4-goodies
Enter fullscreen mode Exit fullscreen mode

XFCE is a lightweight GUI for testing purposes

Configure access, create a backup, improve RDP session quality and define session:

sudo cp /etc/xrdp/xrdp.ini /etc/xrdp/xrdp.ini.bak
sudo sed -i 's/3389/3390/g' /etc/xrdp/xrdp.ini
sudo sed -i 's/max_bpp=32/#max_bpp=32\nmax_bpp=128/g' /etc/xrdp/xrdp.ini
sudo sed -i 's/xserverbpp=24/#xserverbpp=24\nxserverbpp=128/g' /etc/xrdp/xrdp.ini
echo xfce4-session > ~/.xsession
Enter fullscreen mode Exit fullscreen mode

Edit XRDP startup script:

sudo nano /etc/xrdp/startwm.sh
Enter fullscreen mode Exit fullscreen mode
  • Comment these last lines:
# test -x /etc/X11/Xsession && exec /etc/X11/Xsession 
# exec /bin/sh /etc/X11/Xsession 
Enter fullscreen mode Exit fullscreen mode
  • Add these lines:
# xfce
startxfce4
Enter fullscreen mode Exit fullscreen mode

Start RDP session:

sudo /etc/init.d/xrdp start
Enter fullscreen mode Exit fullscreen mode

Access it from your Windows:

  • Execute command mstsc (Win+R)
localhost:3390
Enter fullscreen mode Exit fullscreen mode

Dev Projects

There are many softwares that already works integrated with WSL:

  • on VS Code for example, you can find add-ons to work "remotely" with your projects in WSL, as you can see here
  • IntelliJ also have its ways to deal with WSL integration and run along with your projects, as you can see here
  • on the other hand, there may be softwares that are more complex to integrate with WSL (or even don't)

If you have any further info, specially about dev workspaces with WSL, just drop me a line below and help everyone enjoy and experience the benefits of this tool.

See ya.

Source
https://docs.microsoft.com/en-us/windows/wsl/install-win10#manual-installation-steps
https://github.com/codeedu/wsl2-docker-quickstart (pt-br)
https://github.com/davidbombal/wsl2/blob/main/ubuntu_gui_youtube

Discussion (0)

pic
Editor guide