Install cuDNN on WSL2 Ubuntu 22.04

Cheers,

I’ve installed successfully the cuda toolkit by following this link, which points out to wsl-ubuntu/x86_64/ repository.

When trying to install cuDNN, instructions found here point out to ubuntu2204/x86_64/ repository, but not to the one from above. Thus, the repository is not installed. As a result, when issuing the below command, installation fails, as the corresponding file is not found at the wsl repository.

sudo apt-get -y install cudnn9-cuda-12

Please kindly advise. Thanks in advance.

Hi,
Can you please check if you have another wsl instance present on your machine?

Cheers,

Indeed, I do have another two instances, installed by Docker.

Regards,

Ciro.

Bro it says,
E: Unable to locate package cudnn9-cuda-12

Any solutions.

If you first installed CUDA for WSL and then installed cuDNN, note that since cuDNN does not provide a dedicated online repository for WSL, you need to check whether you are still using the software source configured for WSL. The repository specifically designed for WSL does not include cuDNN.

So in that case, what solution is there. No cudnn through wsl2?

@jaai0123210 Amazingly, I cannot run cuDNN on WSL2 directly, but I can run CuDNN inside docker containers inside WSL2.

In WSL2 Install the package docker.io (! important, not docker but docker.io !) and nvidia-container-toolkit (this is required for docker run --gpus to work)
then set up a docker container like

mkdir mountdir;
docker run --gpus all -dit \
    -e DISPLAY=$DISPLAY \
    -v /tmp/.X11-unix:/tmp/.X11-unix \
    --network=host \
    --shm-size=5g \
    --name "${CONTAINER_NAME}" \
    -v "${MOUNT_DIR}":/mountdir \
    ubuntu:24.04 bash

docker exec -i "${CONTAINER_NAME}" bash <<'EOF'

set -eux

export DEBIAN_FRONTEND=noninteractive

apt update;

apt full-upgrade -y;

apt install tzdata;

ln -fs /usr/share/zoneinfo/Europe/Oslo /etc/localtime

echo "Europe/Oslo" > /etc/timezone

dpkg-reconfigure -f noninteractive tzdata;

apt install screen screenie nano vim sudo wget curl htop php-cli python3 python-is-python3 python3-venv git build-essential cmake python3-dev -y;

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2404/x86_64/cuda-keyring_1.1-1_all.deb;

dpkg -i cuda-keyring_1.1-1_all.deb;

apt update;

apt -y install cuda-toolkit-12-9;

apt -y install cudnn;

echo 'export PATH="/usr/local/cuda-12.9/bin:$PATH"' >> ~/.bashrc;

echo 'export LD_LIBRARY_PATH="/usr/local/cuda-12.9/lib64:$LD_LIBRARY_PATH"' >> ~/.bashrc;

echo 'export CUDA_HOME="/usr/local/cuda-12.9"' >> ~/.bashrc;
mkdir -p /root/.local/venv;
python3 -m venv /root/.local/venv;
echo 'source /root/.local/venv/bin/activate;' >> ~/.bashrc;
EOF

Then access the container like

docker exec -it "${CONTAINER_NAME}" bash

and.. inside that container,
cudnn works great!
and nvtop works great!
and python AI libraries work great!

But it does not work outside docker containers (wtf?)

Fwiw here is the exact setup that works for me:

hans@DESKTOP-EE15SLU:~/projects/rembg_model$ cat docker.sh 
#!/bin/bash
set -eux
CONTAINER_NAME="rembg_model_trainer"

# Get script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "${SCRIPT_DIR}"

# Create mount directory if it does not exist
MOUNT_DIR="$(pwd)";
mkdir -p "${MOUNT_DIR}"

# Check if the container exists
if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
    # Check if the container is running
    if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
        echo "Container '${CONTAINER_NAME}' is already running. Attaching..."
        docker exec -it "${CONTAINER_NAME}" bash
    else
        echo "Container '${CONTAINER_NAME}' exists but is not running. Starting and attaching..."
        docker start -ai "${CONTAINER_NAME}"
    fi
else
    # Run the container in detached mode
    #-p 7011:7011/tcp -p 7011:7011/udp \
    docker run --gpus all -dit \
        -e DISPLAY=$DISPLAY \
        -v /tmp/.X11-unix:/tmp/.X11-unix \
        --network=host \
        --shm-size=5g \
        --name "${CONTAINER_NAME}" \
        -v "${MOUNT_DIR}":/mountdir \
        ubuntu:24.04 bash


    echo "Running initialization steps..."
    docker exec -i "${CONTAINER_NAME}" bash <<'EOF'
set -eux
export DEBIAN_FRONTEND=noninteractive
apt update;
apt full-upgrade -y;
apt install tzdata;
ln -fs /usr/share/zoneinfo/Europe/Oslo /etc/localtime
echo "Europe/Oslo" > /etc/timezone
dpkg-reconfigure -f noninteractive tzdata;
apt install screen screenie nano vim sudo wget curl htop php-cli python3 python-is-python3 python3-venv git build-essential cmake python3-dev -y;
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2404/x86_64/cuda-keyring_1.1-1_all.deb;
dpkg -i cuda-keyring_1.1-1_all.deb;
apt update;
apt -y install cuda-toolkit-12-9;
apt -y install cudnn;
wget 'https://raw.githubusercontent.com/divinity76/bashrc/refs/heads/master/install.sh' -O- | bash
echo 'export PATH="/usr/local/cuda-12.9/bin:$PATH"' >> ~/.bashrc;
echo 'export LD_LIBRARY_PATH="/usr/local/cuda-12.9/lib64:$LD_LIBRARY_PATH"' >> ~/.bashrc;
echo 'export CUDA_HOME="/usr/local/cuda-12.9"' >> ~/.bashrc;
mkdir -p /root/.local/venv;
python3 -m venv /root/.local/venv;
#source ~/.bashrc;
source /root/.local/venv/bin/activate;
pip install --upgrade pip;
#pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128 --upgrade;
#cat /root/.bashrc > /home/ubuntu/.bashrc;
#cat /root/.hansrc > /home/ubuntu/.hansrc;
#cat /root/.bash_aliases > /home/ubuntu/.bash_aliases;
cd /mountdir/training/U-2-Net;
sed 's/=.*//' requirements.txt > requirements.tmp
mv requirements.tmp requirements.txt
pip install -r requirements.txt;
EOF
    docker exec -it "${CONTAINER_NAME}" bash
fi

i run

bash docker.sh 

every time i want to access the docker container.

but it includes stuff like

wget 'https://raw.githubusercontent.com/divinity76/bashrc/refs/heads/master/install.sh' -O- | bash

which you probably don’t want (it’s my personal bash init script, configuring bash to my personal preferences),
and that script adds to .bashrc stuff like

if [[ ! "$PATH" =~ "$HOME/.local/bin" ]]; then
    if [[ -d "$HOME/.local/bin" ]]; then
        export PATH="$HOME/.local/bin:$PATH"
    fi
fi
# activate virtualenv if it exists and isn't already activated
if [[ -z "$VIRTUAL_ENV" ]]; then
    if [[ -f "$HOME/.local/venv/bin/activate" ]]; then
        VIRTUAL_ENV_DISABLE_PROMPT=1 source "$HOME/.local/venv/bin/activate"
    fi
fi

which might be relevant to getting it to work.