Skip to content

Blog

Windows subsystem for Linux 2 (WSL2)

Set up Windows subsystem for Linux 2 (WSL2) for Linux development experience in Windows 10 and 11.

Instal WSL2

Open powershell with administrator privilege, run the following command in the host.

wsl --install --no-distribution

or install the components manually

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

After reboot, install the Linux distribution

wsl --update
wsl --set-default-version 2
wsl --install -d Ubuntu  # Or another Linux distribution

WSL2 post-install (optional) setup

Backup/move the virtual disk

If you want to move the WSL virtual disk file to another disk (in this example, D:\), run the following commands in Windows12:

wsl --export Ubuntu .\Ubuntu\ext4.tar
wsl --unregister Ubuntu
wsl --import Ubuntu D:\Ubuntu\ .\Ubuntu\ext4.tar

Default login user

Edit /etc/wsl.conf in the WSL. You may need to set the default user if you have moved the virtual disk file of the WSL distribution.

/etc/wsl.conf
[user]
default=username

Host settings

Edit .wslconfig 3 in your Windows home directory (%USERPROFILE%).

For example,

.wslconfig
[wsl2]
memory=20GB              # How much memory to assign to the WSL2 VM.
swap=8GB                 # How much swap space to add to the WSL2 VM. 0 for no swap file.
swapfile=C:\\temp\\wsl-swap.vhdx # Sets swap file path location, default is %USERPROFILE%\AppData\Local\Temp\swap.vhdx. Useful if your C drive has limited disk space.

Auto reclaim RAM and disk space

Edit .wslconfig 3 in your Windows home directory (%USERPROFILE%). 4

.wslconfig
[experimental]
autoMemoryReclaim=gradual    # Reclaim RAM usage
sparseVhd=true               # Reclaim virtual disk (vhd) usage

Maintenance

Update kernel

To (manually) update the WSL kernel, run the following commands with administrator privileges:

wsl --shutdown
wsl --update

Reclaim virtual disk space

Optimize-VHD

Note

Optimize-VHD is not available in Windows Home edition.

To reclaim disk space from virtual hard disks (VHDs), run the following commands with administrator privileges 5:

wsl --shutdown
Optimize-VHD -Path %path-to.vhdx% -Mode Full
Export and re-import

Alternatively, export the VHD as a tar file and reimport it again.

Caveats about WSL2

Poor filesystem performance across OSes

Cross-OS file access (e.g., accessing /mnt/c in WSL) is at least one order of magnitude (10x) slower than accessing natively (/home/user/).6

Check battery status

Open Windows Powershell with Administrator rights and run:

powercfg /batteryreport /output "C:\battery-report.html"

See the report at C:\battery-report.html.

Windows Postinstall

Things to do after Windows install.

See also The Ultimate Windows Development Environment Guide

CTT Windows Utility

The Chris Titus Tech's Windows Utility provides APP installs, fixes and tweaks.

irm christitus.com/win | iex

Enable MS store and winget in Windows 10 LTSC 2021

Run the following command and ignore the error. MS store will be installed.

wsreset -i

Click Microsoft.DesktopAppInstaller to install winget.

Chocolatey package manager

Install Chocolatey 🍫, a command-line interface (CLI) package manager for Windows.

Open the powershell prompt with admin privilege (e.g. via the Windows + X menu):

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

Install packages

choco feature enable -n=useRememberedArgumentsForUpgrades
choco install -y git.install --params "'/NoShellIntegration'"

choco install -y vscode qbittorrent firefox brave telegram bandizip nanazip honeyview potplayer lavfilters yt-dlp ffmpeg crystaldiskinfo crystaldiskmark directx vcredist-all sudo starship obsidian nerd-fonts-firacode nerd-fonts-hack github-desktop
choco uninstall -n --skipautouninstaller vscode qbittorrent telegram github-desktop brave

See also the  🍫 Chocolatey package list for more packages.

Disable hibernation

Disabling hibernation saves a lot of disk space (No more C:\hiberfile.sys). Run this in powershell with admin rights:

powercfg -h off

(Optional) Compress OS files for a smaller disk space footprint

Compact.exe /CompactOS:always

Install nvidia GPU driver

You can use nvcleaninstall to install Nvidia driver without bloat.

Afterward

Ubuntu postinstall

Things to do after installing

Make software repo point to NCHC for faster network speed

You can replace  archive.ubuntu.com with a closer mirror, e.g., tw.archive.ubuntu.com or free.nchc.org.tw in /etc/apt/sources.list.d/ubuntu.sources. After you are done, run:

sudo apt clean && sudo apt update && sudo apt full-upgrade -y

Install 3rd party apps

First, install required package

sudo apt update && sudo apt install -y apt-transport-https ca-certificates curl git gnupg-agent software-properties-common

Brave browser

Setup Brave browser

sudo curl -fsSLo /usr/share/keyrings/brave-browser-archive-keyring.gpg https://brave-browser-apt-release.s3.brave.com/brave-browser-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/brave-browser-archive-keyring.gpg arch=amd64] https://brave-browser-apt-release.s3.brave.com/ stable main" | sudo tee /etc/apt/sources.list.d/brave-browser-release.list > /dev/null
sudo apt update && sudo apt install -y brave-browser

Git

sudo add-apt-repository -y ppa:git-core/ppa
sudo apt update && sudo apt install -y git git-lfs

qBittorrent

sudo add-apt-repository -y ppa:qbittorrent-team/qbittorrent-stable
sudo apt update && sudo apt install -y qbittorrent

Telegram messenger

Download and run the official binaries.

Xanmod Linux kernel

Xanmod is a general-purpose Linux kernel distribution with custom settings and new features.

curl -fsSL https://dl.xanmod.org/gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/xanmod-keyring.gpg
echo 'deb [signed-by=/usr/share/keyrings/xanmod-keyring.gpg] http://deb.xanmod.org releases main' | sudo tee /etc/apt/sources.list.d/xanmod-kernel.list > /dev/null
sudo apt update && sudo apt install -y linux-xanmod

AMD and Intel open-source GPU library (Mesa)

Install the latest Mesa open source GPU drivers from the kisak PPA

sudo add-apt-repository -y ppa:kisak/kisak-mesa
sudo apt update && sudo apt full-upgrade -y

Wine and 32-bit games support

sudo dpkg --add-architecture i386
sudo mkdir -pm755 /etc/apt/keyrings
sudo wget -O /etc/apt/keyrings/winehq-archive.key https://dl.winehq.org/wine-builds/winehq.key
sudo wget -NP /etc/apt/sources.list.d/ https://dl.winehq.org/wine-builds/ubuntu/dists/noble/winehq-noble.sources
sudo apt update && sudo apt install wine

Kubuntu backports

Kubuntu backports: Latest versions of KDE framework and APPs

sudo add-apt-repository -y ppa:kubuntu-ppa/backports
sudo apt-get update && sudo apt full-upgrade -y

Latest kernel

Install Hardware Enablement (HWE) kernels: https://ubuntu.com/kernel/lifecycle

sudo apt install --install-recommends linux-generic-hwe-$VERSION

Where $VERSION is the Ubuntu version.

Update your system and install packages

sudo apt update && sudo apt full-upgrade -y

Ubuntu:

sudo apt install -y git git-lfs cifs-utils ssh nala gnome-shell-extension-manager parallel baobab ncdu synaptic apt-xapian-index ppa-purge ubuntu-restricted-extras ffmpeg vlc mcomix fonts-wqy-microhei fonts-wqy-zenhei fonts-open-sans ttf-mscorefonts-installer zsh btrfs-compsize

Kubuntu:

sudo apt install -y git git-lfs cifs-utils ssh nala parallel ncdu kio-extras gnome-keyring ppa-purge kubuntu-restricted-extras ffmpeg vlc fonts-wqy-microhei fonts-wqy-zenhei fonts-open-sans ttf-mscorefonts-installer zsh btrfs-compsize synaptic apt-xapian-index

System tweaks

Fix locales

Uncomment the zh_TW line in /etc/locale.gen. Then run:

sudo locale-gen

Finally, install the Traditional Chinese locale in Language Support and then set locale to Taiwan.

(Optional) Remove snap

List snap packages

snap list

Uninstall each Snap package

sudo snap remove --purge $PKG

Stop the snapd service and uninstall it

sudo systemctl stop snapd
sudo apt remove --autoremove --purge snapd
sudo apt-mark hold snapd

Terminal

Temporary files in RAM disk

Setting tmpfs (RAM disk) for /tmp folder:

sudo cp -v /usr/share/systemd/tmp.mount /etc/systemd/system/
sudo systemctl enable tmp.mount

Disable Extended Security Maintenance (ESM)

sudo mv /etc/apt/apt.conf.d/20apt-esm-hook.conf /etc/apt/apt.conf.d/20apt-esm-hook.conf.disabled

Disable cloud-init

sudo touch /etc/cloud/cloud-init.disabled

Automatic updates

sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades

Gnome shell extensions

YOu can also search and install extensions via gnome-shell-extension-manager

Kubuntu System Settings

  • Double click to open files instead of single clicks: Workspace behavior => General behavior => click behavior.
  • Start with an empty session in Desktop session.

Making system more responsive

Fine-tuning the kernel

Replace the line in /etc/default/grub

Gaming:

/etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash preempt=full"

Virtualization:

/etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash preempt=full Nohz_full=all"

Then run:

sudo update-grub

Misc.

Git Commands

Sources:

Ordinary workflows

HEAD: the current state of the repo.

Download a repository

Clone a git repo from a remote repository:

git clone <url>

Checking out a specific branch:

git clone <url> -b <branchname>

If there are submodule(s) in the Git repository, you might want to clone them as well using the --recursive option.

git clone <url> --recursive

See also: SSH login to Git services like GitHub and GitLab.

Make changes and commit

git status      # The current state of the repository.
git add <file>  # Add a new or edited file to the staging area. i.e. telling git to track this file
git add -A      # Track all files at once
git commit -m "Commit message"  # Commit staged (added) file
git commit -am "Commit message" # Commit modified files without having to run git add beforehand
git revert <SHA>                # Make a counter commit to undo the changes. The tracked files will go back to the <SHA> commit.

Synchronize with remote: Push and pull

git fetch # Download objects and refs from another repository without really pull in the changes
git merge # After git fetch, merge the changes done in the remote to the local repo
git push <remote> <branch-name> # Push commits in to remote
git push --set-upstream <remote> <name-of-your-branch>  # Setup remote url before push
git pull <remote>  # Pull changes from the remote

Stash

To temporarily store untracked files.

git stash -u   # Store current work with untracked files
git stash pop  # Bring stashed work back to the working directory

Work with branches

git branch <branch_name>    # Create a new branch
git branch -a               # List all branches
git branch -d <branch_name> # Delete a branch

git checkout <branch_name>    # checkout an existing branch
git checkout -b <branch_name> # Create a new branch and checkout it

git switch <branch_name>    # Switch to a specified branch. If the branch name does not exist, create one.
git merge  <branch_name>    # Merge the branch into the current branch

Orphan branches

Orphan branches are unrelated to others in history. For example, gh-pages branch dedicated to GitHub pages.

git branch --orphan <branchname>  # Create a orphan branch

Install fonts in Linux

Copy the fonts files to ~/.local/share/fonts/. Then, run fc-cache to rebuild fonts cache.

fc-cache -fv

Git submodule

Frequently used commands for Git submodules.

Add a submodule

TO add the reference to another git project as a submodule:

git submodule add $url $path
git submodule update --init --recursive

Alternatively, you can use GUI tools like or GitHub desktop. They download and initiate submodules automatically.

Add you will see the file .gitmodules with information about the submodule(s). For instance,

.gitmodules
[submodule "themes/DoIt"]
    path = themes/DoIt
    url = https://github.com/HEIGE-PCloud/DoIt.git

Track a specific branch in the submodule

With -b $branch option

git submodule add -b $branch $url $path

Or set-branch -b $branch if you already have added a submodule

git submodule set-branch -b  $branch $path

Update all Git submodules to the latest commit

From a stackOverflow post and Git docs

git submodule update --remote --merge

For automated updates by bots, see automatic dependency update.

Remove a submodule

From Git docs

# Remove submodule from config
git submodule deinit $path
# Delete submodule tracking data
git rm <submodule path> && git commit
# Complete removal
rm -rf $GIT_DIR/modules/$name/