Setting Up and Configuring WSL 2 with CUDA Support on Windows 10
WSL 2 Installation and Configuration
Enabling the Windows Subsystem for Linux
Before installing any Linux distributions, the WSL feature must be enabled. Open PowerShell with administrator privileges and execute:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
Verifying System Requirements for WSL 2
WSL 2 requires specific Windows 10 versions:
- x64 systems: Version 1903 or later with build 18362 or higher
- ARM64 systems: Version 2004 or later with build 19041 or higher
Check your Windows version by pressing Win+R, typing "winver", and pressing Enter. Alternatively, run ver in Command Prompt.
For Windows 10 versions 1903 or 1909, ensure the build number is at least 18362.1049+ or 18363.1049+ with sub-builds higher than .1049. Use Windows Update to obtain the latest patches.
Enabling Virtual Machine Platform
WSL 2 relies on virtualization technology. Enable the Virtual Machine Platform feature via PowerShell with administrator rights:
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Restart your computer after this step to complete the WSL installation and update to WSL 2.
Downloading the Linux Kernel Update Package
Download the WSL 2 Linux kernel update package for your architecture:
- x64 systems: WSL2 Linux kernel update package
- ARM64 systems: Download the ARM64 variant from the official Microsoft documentation
Verify your system type by running:
systeminfo | find "System Type"
Execute the downloaded installer with elevated permissions to complete the kernel installation.
Setting WSL 2 as the Default Version
Configure WSL 2 as the default version for all new Linux distributions:
wsl --set-default-version 2
Installing a Linux Distribution
Open Microsoft Store and select your preferred Linux distribution:
- Ubuntu 16.04 LTS
- Ubuntu 18.04 LTS
- Ubuntu 20.04 LTS
- openSUSE Leap 15.1
- SUSE Linux Enterprise Server 12 SP5
- SUSE Linux Enterprise Server 15 SP1
- Kali Linux
- Debian GNU/Linux
- Fedora Remix for WSL
- Pengwin / Pengwin Enterprise
- Alpine WSL
After installation, the first launch requires one to two minutes for file decompression. Subsequent launches complete in under a second. Create your user account and password when prompted.
Installing Windows Terminal (Recommended)
Windows Terminal provides multi-tab support, custom key bindings, search functionality, and theming capabilities. Install it from the Microsoft Store for a unified command-line experience acros PowerShell, Command Prompt, and Linux distributions.
Configure Windows Terminal to launch directly into WSL by setting the command line to bash -cur_console:p in the startup settings.
Troubleshooting Common Issues
Resolving Error 0x80070032
If you encounter error 0x80070032, the Hyper-V feature may not be properly enabled. Navigate to:
Control Panel → Programs and Features → Turn Windows features on or off
Enable "Hyper-V" and restart your computer.
Recommended Distribusion Version
Ubuntu 18.04 LTS offers the best compatibility and stability. Ubuntu 20.04 LTS may have compatibility issues with certain packages and configurations.
VM Storage Location
The WSL virtual disk is stored at:
C:\Users\<username>\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_<hash>\LocalState\ext4.vhdx
The virtual machine must remain on the C drive, or startup will fail.
Accessing Windows Drives
Windows drives are automatically mounted under /mnt/ in the Linux environment. Each drive (C, D, E, F) appears as /mnt/c, /mnt/d, etc.
Configuring APT Mirrors
The default APT sources connect to海外 servers, resulting in slow downloads. Replace the sources with mirrors closer to your location.
Backup the original sources list:
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
Aliyun Mirror (China):
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
USTC Mirror:
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
Tsinghua Mirror:
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
Replace /etc/apt/sources.list with your chosen mirror, then update:
sudo apt-get update
Setting Up a Graphical Desktop Environment
Prerequisites
Install VcXsrv (XLaunch) on Windows. Launch XLaunch with the following configuration:
- Select "One large window" or "Fullscreen"
- Disable access control
- Keep other settings at defaults
System Updates
sudo apt-get update
sudo apt-get upgrade
Installing Xfce4
sudo apt-get install xfce4
sudo apt-get install xubuntu-desktop
If you encounter processing errors, refer to Debian package management documentation for resolution steps.
Configuring Display Settings
WSL 2 uses Hyper-V virtualization, requiring different DISPLAY configuration than WSL 1. Check the nameserver address in /etc/resolv.conf:
sudo vim /etc/resolv.conf
Note the IP address following nameserver (e.g., 192.168.112.1).
Prevent automatic regeneration of the resolv.conf file:
sudo vim /etc/wsl.conf
Add:
[network]
generateResolvConf = false
Configure the DISPLAY environment variable in ~/.bashrc:
echo 'export DISPLAY=192.168.112.1:0' >> ~/.bashrc
source ~/.bashrc
Launch Xfce4:
startxfce4
Resolution Adjustment
If the display appears blurry, adjust the resolution. First, generate modeline parameters:
cvt 1920 1080
Output will show modeline information. Apply the resolution:
xrandr --newmode "1920x1080_60.00" 173.00 1920 2048 2248 2576 1080 1083 1088 1120 -hsync +vsync
xrandr --addmode Virtual1 "1920x1080_60.00"
xrandr --output default --mode "1920x1080_60.00"
Make these changes persistent by adding them to ~/.profile:
echo 'xrandr --newmode "1920x1080_60.00" 173.00 1920 2048 2248 2576 1080 1083 1088 1120 -hsync +vsync' >> ~/.profile
echo 'xrandr --addmode default "1920x1080_60.00"' >> ~/.profile
echo 'xrandr --output default --mode "1920x1080_60.00"' >> ~/.profile
If blurriness persists, adjust Windows DPI scaling. Right-click the XLaunch shortcut → Properties → Compatibility → Change high DPI settings → Override high DPI scaling behavior.
CUDA Setup in WSL 2
Joining the Windows Insider Program
Enable WSL CUDA support by joining the Windows Insider Program. Access Settings → Update & Security → Windows Insider Program and select the Dev Channel. Restart and install all available updates.
Verify your build version exceeds 20150 using winver.
Verifying WSL 2 Installation
wsl --list --verbose
Ensure the version column shows "2" for your distribution.
Installing NVIDIA Driver for WSL
Download and install the NVIDIA driver specifically designed for WSL from the NVIDIA website. This driver package includes both Windows and WSL-compatible components. Select the appropriate driver for your GPU model.
Important: Do not instal Linux display drivers within WSL. The Windows driver package provides all necessary WSL support.
Installing CUDA Toolkit
Install development tools in WSL:
sudo apt update
sudo apt install build-essential
Download the CUDA toolkit from NVIDIA. For CUDA 10.2, use wget to fetch the .run file and execute it within WSL.
Critical: During CUDA installation, deselect the driver component. Only install the toolkit and SDK. The WSL-compatible driver was already installed in the previous step.
Install required dependencies:
sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev
Verify CUDA installation:
cat /usr/local/cuda/version.txt
To check GPU status within WSL (requires CUDA Driver API), install:
sudo apt install nvidia-utils-390
nvidia-smi
Installing PyTorch with CUDA Support
Install PyTorch using conda with your CUDA version:
conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
If package download fails, manually download the package tarball and install locally:
conda install --use-local pytorch-1.7.1-py3.8_cuda10.2_cudnn7.6.5_0.tar.bz2
conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
Verify PyTorch CUDA configuration:
python
>>> import torch
>>> print(torch.version.cuda)
10.2
Return to Python prompt with exit().
Using Cmder with WSL
Download Cmder Mini from the official GitHub releases page. Configure Cmder to launch directly into WSL:
- Open Cmder settings
- Navigate to Startup → Command line
- Enter
bash -cur_console:p - Save settings
Add Cmder to system PATH for global access. To enable right-click context menu integration, run Command Prompt as administrator and execute:
Cmder.exe /REGISTER ALL
This enables "Open with Cmder here" context menu entries in Windows Explorer.