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.

Copy link

ghost commented Dec 3, 2023

This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-12-04 09:53:22.632171: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT

Is it okay for these errors to show?
How would we go about installing tensorRT?

Also thank you so much for this :D

@ammarsufyan
Copy link
Author

@aros181 Are you using an RTX VGA and following the tutorial correctly?

@AtomVoyager
Copy link

Hello,
I contact you because i have this error when i use the command 'del libcuda.so' (i followed the steps like before):

Microsoft Windows [version 10.0.22621.1702]
(c) Microsoft Corporation. Tous droits réservés.

C:\Windows\System32>wsl
silvio@Silvio:/mnt/c/Windows/System32$ cd lxss
silvio@Silvio:/mnt/c/Windows/System32/lxss$ cd lib
silvio@Silvio:/mnt/c/Windows/System32/lxss/lib$ del libcuda.so
Command 'del' not found, did you mean:
command 'den' from snap den (1.2.0-0)
command 'mdel' from deb mtools (4.0.33-1+really4.0.32-1build1)
command 'el' from deb oneliner-el (0.3.6-9.1)
command 'qdel' from deb gridengine-client (8.1.9+dfsg-10build1)
command 'qdel' from deb slurm-wlm-torque (21.08.5-2ubuntu1)
command 'delv' from deb bind9-dnsutils (1:9.18.18-0ubuntu0.22.04.1)
command 'delp' from deb fp-utils-3.2.2 (3.2.2+dfsg-9ubuntu1)
command 'hdel' from deb hfsutils (3.2.6-15build2)
command 'dex' from deb dex (0.9.0-1)
command 'wdel' from deb wput (0.6.2+git20130413-11)
command 'dll' from deb brickos (0.9.0.dfsg-12.2)
command 'deal' from deb deal (3.1.9-12)
See 'snap info ' for additional versions.

Thx you if you have any ideas c;

@ammarsufyan
Copy link
Author

ammarsufyan commented Dec 25, 2023

Hi @AtomVoyager. Thank you for contacting me, to use the "del" command there is no need to enter wsl first (because the del command is a command from the windows command prompt). Just do the command that I have given. Thank you.

*Don't forget to run Windows Command Prompt as admin.

@AtomVoyager
Copy link

Hello, thx for helping me in previous message it worked perfectly. I have the same issue as another user. Here is warnings i receive when I launch the python command to see i have a list of gpu in result:

silvio@Silvio:~$ python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))" 2023-12-26 03:37:55.084581: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable TF_ENABLE_ONEDNN_OPTS=0. 2023-12-26 03:37:55.260788: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered 2023-12-26 03:37:55.260849: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered 2023-12-26 03:37:55.291224: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered 2023-12-26 03:37:55.347500: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags. 2023-12-26 03:37:56.155925: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT 2023-12-26 03:37:57.164251: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node Your kernel may have been built without NUMA support. 2023-12-26 03:37:57.265391: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node Your kernel may have been built without NUMA support. 2023-12-26 03:37:57.265807: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node Your kernel may have been built without NUMA support. [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

As I can see I have a liste of one GPU (which is an RTX 3060 for laptop) but i have errors like this one which is very annoying me:

2023-12-26 03:37:55.347500: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.

Do you have any idea of how can this be fixed (I put a picture of warnings for a better view)?

Thx you very much in advance

Capture d'écran 2023-12-26 034649

@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

@AlekseiSkr
Copy link

@ammarsufyan Thank you! <3

@ammarsufyan
Copy link
Author

@AlekseiSkr your welcome

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