Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Dadangdut33/5dc713ed38e4c22e192f1468ce44eeaf to your computer and use it in GitHub Desktop.
Save Dadangdut33/5dc713ed38e4c22e192f1468ce44eeaf to your computer and use it in GitHub Desktop.
How to install CUDA 11.8 and CUDNN 8.6 for TensorFlow 2.13 WSL2-Ubuntu 22.04
  1. Install CUDA 11.8 first
$ 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/11.8.0/local_installers/cuda-repo-wsl-ubuntu-11-8-local_11.8.0-1_amd64.deb
$ sudo dpkg -i cuda-repo-wsl-ubuntu-11-8-local_11.8.0-1_amd64.deb
$ sudo cp /var/cuda-repo-wsl-ubuntu-11-8-local/cuda-*-keyring.gpg /usr/share/keyrings/
$ sudo apt-get update
$ sudo apt-get -y install cuda
  1. Install CUDNN 8.6.0 (download from here) - copy to /home/username/
$ sudo apt-get install zlib1g
$ tar -xvf cudnn-linux-x86_64-8.6.0.163_cuda11-archive.tar.xz
$ sudo cp cudnn-*-archive/include/cudnn*.h /usr/local/cuda/include 
$ sudo cp -P cudnn-*-archive/lib/libcudnn* /usr/local/cuda/lib64 
$ sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*
  1. Install Python 3.9.2 using pyenv
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install \
    build-essential \
    curl \
    libbz2-dev \
    libffi-dev \
    liblzma-dev \
    libncursesw5-dev \
    libreadline-dev \
    libsqlite3-dev \
    libssl-dev \
    libxml2-dev \
    libxmlsec1-dev \
    llvm \
    make \
    tk-dev \
    wget \
    xz-utils \
    zlib1g-dev
$ curl https://pyenv.run | bash
$ export PATH="$HOME/.pyenv/bin:$PATH" && eval "$(pyenv init --path)" && echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc
$ exec $SHELL
$ pyenv --version
$ pyenv install 3.9.2
$ pyenv versions
$ pyenv global 3.9.2
  1. Verify CUDA/GPU

Run below in terminal:

python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

Output should end with something like [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]. If output ends with only [], the GPU is not detected.

Troubleshooting

If you encounter Could not load library libcudnn_cnn_infer.so.8. Error: libcuda.so: cannot open shared object file: No such file or directory

add this to your bashrc export LD_LIBRARY_PATH=/usr/lib/wsl/lib:$LD_LIBRARY_PATH

If by any chance pyenv stopped working, edit your .bashrc, remove the previous pyenv init, and add this

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init - --path)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment