# Setup Debian or Its Derivatives in WSL 2
This is what I personally use for doing my work. My typical use cases include:
* Software Development (Fortran, Matlab, Python)
* Parallel Programming (MPI, OpenMP)
* Scientific Data Analysis and Visualization
* ...
I prefer Debian-based Linux distributions (e.g., Debian, Ubuntu or Pengwin).
-----
1. Install WSL 2. Choose a method from below.
* Method 1: For Windows 10/11 with the latest cumulative update (LCU) ≥ 2022/11, install the ["Store" version of WSL 2](https://devblogs.microsoft.com/commandline/the-windows-subsystem-for-linux-in-the-microsoft-store-is-now-generally-available-on-windows-10-and-11) then reboot.
With this approach, [WSLg](https://github.com/microsoft/wslg) can be used for running Linux GUI applications. Installing a separate X server (e.g., VcXsrv) on the Windows side is no longer needed.
```batch
wsl --install --no-distribution --no-launch
```
* Method 2: Otherwise, install the "in-Windows" version of WSL 2.
* Enable related optional features then reboot.
```batch
dism /norestart /online /quiet /enable-feature /featurename:microsoft-windows-subsystem-linux /all
dism /norestart /online /quiet /enable-feature /featurename:virtualmachineplatform /all
```
* Install [WSL 2 Linux Kernel Update](https://www.catalog.update.microsoft.com/Search.aspx?q=wsl). Choose the latest version available.
* Set WSL 2 as the default.
```batch
wsl --set-default-version 2
```
Then,
* Install a Linux distribution from Microsoft Store.
* Install Windows Terminal.
* Confirm WSL 2 is in use.
```batch
wsl --list --verbose
```
2. Upgrade and install packages.
```bash
sudo add-apt-repository -y ppa:kisak/turtle
sudo apt update
sudo apt upgrade -y
sudo apt install -y \
build-essential gcc g++ gfortran autoconf automake libtool \
cmake diffstat diffutils git git-lfs gitk make patch patchutils pkgconf \
bash-completion htop less man-db nano procps psmisc python3 python3-pip python3-venv screen tcsh tmux vim \
bind9-dnsutils ca-certificates curl iputils-* ncat ndiff netcat-openbsd nmap rsync ssh traceroute wget \
bzip2 gzip lz4 unzip xz-utils zip zstd \
libegl-mesa0 libegl1 libgl1 libglx-mesa0 libglx0 libasound2t64 libxcomposite1 libxcursor1 libxi6 libxrandr2 libxss1 libxtst6 xkb-data \
mesa-utils x11-apps x11-utils xauth xterm
```
Some [dependencies](https://docs.anaconda.com/anaconda/install/linux) are required for enabling the [`QtAgg` backend](https://matplotlib.org/stable/users/explain/backends.html) in `matplotlib`. It draws plots much faster than the basic `TkAgg` backend.
3. Install Anaconda. Choose a method from below.
* The latest version is `2024.10` at the time of writing.
```bash
export VERSION="2024.10"
```
* Method 1: Use [Anaconda Distribution](https://www.anaconda.com/download/success).
You will get a large monolithic `base` environment that is hard to manage and maintain. If everything you use can be satisfied within the [`defaults` channel](https://docs.anaconda.com/anaconda/packages/pkg-docs), it is okay to take this route.
```bash
wget "https://repo.anaconda.com/archive/Anaconda3-${VERSION}-1-Linux-x86_64.sh"
bash "Anaconda3-${VERSION}-1-Linux-x86_64.sh"
```
* Method 2: Use [Miniforge](https://github.com/conda-forge/miniforge/releases/latest) (Mambaforge and Miniforge have converged and are now identical!).
This approach creates an environment for Anaconda that is separated from `base`. The `mamba` package manager consumes less memory and solves environments much faster than `conda`.
```bash
wget "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
bash "Miniforge3-$(uname)-$(uname -m).sh" -b
miniforge3/bin/conda init bash
miniforge3/bin/mamba shell init --shell bash
```
Log out and log in again. Then,
* Do not activate the `base` environment automatically.
```bash
conda config --set auto_activate_base false
```
* Update the `base` environment.
```bash
mamba update conda mamba
mamba update --all
```
* Create an environment for Anaconda.
```bash
mamba create -c defaults -n "anaconda-${VERSION}" \
python=3.12 "anaconda=${VERSION}"
```
* Confirm the `QtAgg` backend is enabled.
```python
import matplotlib as mpl
mpl.get_backend()
```
4. Install [VcXsrv](https://sourceforge.net/projects/vcxsrv) on the Windows side. This is no longer needed for the "Store" version of WSL 2.
5. Follow [here](https://hackmd.io/@stargazerwang/HkYFVktlY) to setup Dot files.
6. Follow [here](https://hackmd.io/@stargazerwang/S1Wao5ZVn) to setup Docker.
7. Follow [here](https://hackmd.io/@stargazerwang/ryOsp7F4T) to setup Lmod.
8. Follow [here](https://hackmd.io/@stargazerwang/B1g55BLpA) to setup Intel oneAPI Toolkits.
-----
9. (Optional) Move `ext4.vhdx` files to another location (e.g., `D:\WSL2`) to save space on `C:\`.
```batch
wsl --shutdown
cd /d "%LOCALAPPDATA%\Packages\TheDebianProject.DebianGNULinux_76v4gfsz19hv4\LocalState"
md "D:\WSL2\Debian"
copy /b /v /y "ext4.vhdx" "D:\WSL2\Debian"
del "ext4.vhdx"
mklink "ext4.vhdx" "D:\WSL2\Debian\ext4.vhdx"
cd /d "%LOCALAPPDATA%\Packages\CanonicalGroupLimited.Ubuntu24.04LTS_79rhkp1fndgsc\LocalState"
md "D:\WSL2\Ubuntu"
copy /b /v /y "ext4.vhdx" "D:\WSL2\Ubuntu"
del "ext4.vhdx"
mklink "ext4.vhdx" "D:\WSL2\Ubuntu\ext4.vhdx"
cd /d "%LOCALAPPDATA%\Packages\WhitewaterFoundryLtd.Co.16571368D6CFF_kd1vv0z0vy70w\LocalState"
md "D:\WSL2\WLinux"
copy /b /v /y "ext4.vhdx" "D:\WSL2\WLinux"
del "ext4.vhdx"
mklink "ext4.vhdx" "D:\WSL2\WLinux\ext4.vhdx"
```
10. (Optional) Grant `Full Control` permission to `Users` group for these moved `*.vhdx` files. See the following links for why this is needed:
* [WSL Issue Reference 1](https://answers.microsoft.com/en-us/windows/forum/all/kb4565503-installed-then-wsl-2-failed-to-start/25794c4f-0b20-465e-bbdb-a8af3d9e0e88)
* [WSL Issue Reference 2](https://github.com/microsoft/WSL/issues/5605)
```batch
cd /d "D:\WSL2"
icacls "*.vhdx" /grant:r Users:F /t
```