Setting Up CUDA in WSL

Running CUDA on native Linux is possible, but NVIDIA driver support can be inconsistent across distributions. Using CUDA in Windows Subsystem for Linux (WSL) offers a reliable alternative, providing direct driver support and a smoother experience for AI development.

Prerequisites

  • Windows 10/11 with WSL enabled
  • An NVIDIA GPU with driver support for CUDA

1. Install or Import a WSL Distro

If you don’t have WSL installed, run the following command in PowerShell as administrator:

1
wsl --install

If you want to use a fresh Ubuntu WSL image, download it from Ubuntu WSL Images and import it to a new folder:

1
wsl --import <NewDistroName> <InstallLocation> <PathToTarFile> [--version <Version>]

After importing, launch your new distro:

1
wsl -d <DistroName>

By default, you’ll log in as root. You can create a regular user later if needed.

2. Update and Prepare the System

No need to install the NVIDIA driver inside WSL; just install the CUDA toolkit. First, update your package sources:

1
2
sudo apt-get update
sudo apt-get upgrade

Remove any old NVIDIA GPG keys:

1
sudo apt-key del 7fa2af80

3. Install CUDA Toolkit for WSL-Ubuntu

Download and install the CUDA toolkit using the official NVIDIA repository. Here’s how to use the deb(network) installer:

1
2
3
4
wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt-get update
sudo apt-get -y install cuda-toolkit-13-0

To make the CUDA toolkit available every time you open a new terminal, add the following line to your ~/.bashrc file:

1
echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrc

After editing .bashrc, reload it with:

1
source ~/.bashrc

Now, you can use CUDA commands without manually updating your PATH each session.

4. Verify Installation

Check if the CUDA compiler is installed:

1
nvcc --version

To verify your CUDA and driver versions:

1
nvidia-smi

Conclusion

With these steps, you should have a working CUDA environment in WSL, ready for AI and GPU-accelerated development. If you encounter issues, refer to the NVIDIA CUDA on WSL documentation for troubleshooting tips.