Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ammarsufyan/51dd12d9471eb73b2348d373b605b45a to your computer and use it in GitHub Desktop.
Save ammarsufyan/51dd12d9471eb73b2348d373b605b45a to your computer and use it in GitHub Desktop.
How to install CUDA-11.8 and CUDNN-8.6 for TensorFlow-2.13 in WSL2-Ubuntu-22.04-LTS
  1. Install CUDA 11.8
$ 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 first)
$ 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. Run the command below in Windows Command Prompt as admin (Thanks to Roy Shilkrot) and restart your WSL2
> cd \Windows\System32\lxss\lib
> del libcuda.so
> del libcuda.so.1
> mklink libcuda.so libcuda.so.1.1
> mklink libcuda.so.1 libcuda.so.1.1
  1. Setup some WSL2 paths
$ echo 'export LD_LIBRARY_PATH=/usr/lib/wsl/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
$ source ~/.bashrc
$ sudo ldconfig
  1. Update some dependencies
$ sudo apt update
$ sudo apt upgrade
  1. (Optional) Install Python 3.9.2 using pyenv

I used Python 3.9.2 for the exam, so I had to use pyenv in Ubuntu to not change Ubuntu's default Python. As for you, you can use Python 3.10.* and have no problem.

$ curl https://pyenv.run | bash
$ sudo apt install curl -y 
$ sudo apt install git -y
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(pyenv init - --path)"' >> ~/.bashrc
$ exec $SHELL
$ 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
$ pyenv install 3.9.2
$ pyenv global 3.9.2
  1. Install TensorFlow 2.13
$ pip install --upgrade pip
$ pip install tensorflow==2.13
  1. Verify the GPU Setup
$ python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

If a list of GPU devices is returned, you've installed TensorFlow successfully.

@ammarsufyan
Copy link
Author

ammarsufyan commented Dec 26, 2023

Hi @AtomVoyager,

screenshot20231226 (1)
I don't know about the blue square things (I haven't experienced that). For TensorRT, I think you can install it using this tutorial. For Numa things, you can ignore it, and it works well. If you dont want to see an error, try using TensorFlow 2.10.0 on Windows only (no WSL). You can follow this tutorial for an easy way. (Now, I'm using this in Windows, except I installed it without Conda).

By the way, after searching for some information about the new version of TensorFlow (I think >2.14) in WSL2, it now supports installation and includes Cuda in PIP. This is interesting, you can try it here.
image

@baiyanghor
Copy link

Works for me, thank you very much!!

@ammarsufyan
Copy link
Author

@baiyanghor your welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment