WSL2, Docker, GUI - Manual Installation
Tags: wsl2, docker, wslgui, windows
Published: 2021-04-11 20:45:11

# 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**
- Run 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
```

- Open your PowerShell once again and try typing `wsl`, and if doesn't work, restart your machine

**Upgrade to WSL2**

- [Download](https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi) and install the WSL2 kernel to update

- Set the WSL default version to 2, via Powershell:
```
wsl --set-default-version 2
```
_(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
```

**Tips**
- Access your WSL "machine" through the `wsl` command (via Powershell or cmd), which stands for:
```
C:\Windows\System32\wsl.exe
```

- Access your WSL "space" on Windows through: (`Win+R` or explorer):
```
\\wsl$
```

- Find your Windows "space" on Linux: 
```
/mnt/c/
```
- You can also run 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
```

- Backup your WSL state: 
```
wsl --export <distro> <output_file>                        # export
wsl --import <distro> <installation_location> <input_file> # import
```
_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 
```
- Your custom configs are applied after a WSL restart: 
```
wsl --shutdown
```

# 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](https://docs.microsoft.com/pt-br/windows/terminal/customize-settings/startup) 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
```

- 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>", 
}
```

# 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
```

Install (or overwrite) X-RDP:
```
sudo apt-get purge xrdp
sudo apt install -y xrdp
```

Install a GUI:
```
sudo apt install -y xfce4 # check 'gdm3' option
sudo apt install -y xfce4-goodies
```
_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
```

Edit XRDP startup script:
```
sudo nano /etc/xrdp/startwm.sh
```

- Comment these last lines: 
```
# test -x /etc/X11/Xsession && exec /etc/X11/Xsession 
# exec /bin/sh /etc/X11/Xsession 
```

- Add these lines: 
```
# xfce
startxfce4
```

Start RDP session:
```
sudo /etc/init.d/xrdp start
```

Access it from your Windows:
- Run command `mstsc` (`Win+R`)
```
localhost:3390
```

# 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](https://code.visualstudio.com/docs/remote/wsl)
- IntelliJ also have its ways to deal with WSL integration and run along with your projects, as you can see [here](https://www.jetbrains.com/help/idea/how-to-use-wsl-development-environment-in-product.html#local_project)
- 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