CUDA Setup on WSL2

I had set up Nvidia drivers sometime in the beginning of the year, but somehow configurations just evaporated from my sight. So, I’m writing this article to keep a track of what I did this time.
Installing the Compiler (Actually the Entire Toolkit)
Compiling CUDA programs requires a specialized compiler from Nvidia called nvcc
for your C based CUDA programs. Get the WSL2 (Ubuntu) installation commands from Nvidia here. As of the time of writing, the recommended version is v13.0
.
wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/13.0.0/local_installers/cuda-repo-wsl-ubuntu-13-0-local_13.0.0-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-13-0-local_13.0.0-1_amd64.deb
sudo cp /var/cuda-repo-wsl-ubuntu-13-0-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda-toolkit-13-0
Silent Compilation Issues
To check that I had everything in place, I wrote a toy program. I took the code from Nvidia’s CUDA guide. It compiled and ran without any crashes! But the output was just wrong, all the values in the vector C
were zeros. Ah the silent errors.
__global__ void VecAdd(float *A, float *B, float *C) {
int i = threadIdx.x;
C[i] = A[i] + B[i];
}
As I mentioned before, I had set up CUDA some time back. To check the driver versions that I had installed, I ran nvidia-smi
. The version I had installed back then was v12.8
. And the compiler version that I installed was v13.0
. Classic!
Dealing with Version Mismatch
Now I had two choices to rectify, either upgrade the drivers or downgrade the toolkit. Obviously, I went with the option to upgrade. I do not have any constraints holding me back to a specific version. A simple google search pointed me to the latest driver version which I could manually install or have Nvidia handle the headache entirely. As I have a GeForce graphics card, I upgraded my drivers using Nvidia GeForce Experience. I am lazy, let them handle their mess.
Now both my toolkit and drivers are up to date. And my toy program works as expected :)
Subscribe to my newsletter
Read articles from Swebert Correa directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Swebert Correa
Swebert Correa
I am a software engineer at Rakuten Symphony. On a daily basis, I deal with storage and distributed systems. This involves pumping out features, figuring out race conditions, mitigating deadlocks, and coding by keeping network latency in mind. That's a lot of C and GDB 😛