Linux, 工作, 生活, 家人


Run Keras or TensorFlow in WSL2 with Nvidia GPU

When this article is finished, it will still be unable to run Keras or TensorFlow within Conda or a virtual environment. This is because TensorFlow/Keras will display an error message similar to the one below (cut):

Node: 'model_3/conv1d_24/Conv1D' 
DNN library is not found. 	 [[{{node model_3/conv1d_24/Conv1D}}]] [Op:__inference_train_function_34509]

It seems that there may be compatibility issues between the versions of TensorFlow/Keras installed outside of Conda and those installed within Conda. But it maybe also cause by tensorflow 2.12 has issue, please re-install 2.10 and try again .
Specified version can work well cuDNN 8.9, CUDA 11.8 and TensorFlow 2.10.
Here is how to re-install tensorflow 2.10 instruction.

pip uninstall tensorflow
pip install tensorflow-gpu
pip install tensorflow-gpu==2.10 

TensorFlow 2.12.0 WSL2 GPU support

Have tried someother way, all of them doesn’t work when you use 2.12 tensorflow. Don’t try this. ex:

sudo cp cuda/include/cudnn*.h   /anaconda3/envs/<your environment here>/include
sudo cp cuda/lib64/libcudnn*    /anaconda3/envs/<your environment here>/lib
sudo chmod a+r /usr/local/cuda/include/cudnn*.h    /anaconda3/envs/<your environment here>/lib/libcudnn*

How to install latest cuDNN to conda?



Suppose it already install WSL2 on your system, please follow the CUDA on WSL User Guide to install WSL, for Tensorflow 2.12, suggestion CUDA version is 11.8. Can be found here CUDA Toolkit 11.8 Downloads

sudo mv /etc/apt/preferences.d/cuda-repository-pin-600
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


It still needs to install CUDNN, follow this instruction to download CUDNN 8.9.x, Installing cuDNN on Linux. Download cuDNN needs to register Nvidia account.
Download address : cuDNN Download, it has cuDNN for CUDA 12 and CUDA 11, download for CUDA 11.

Add Path to System Path

Put following seting into ~/.bashrc, thus it can update when get bash console

export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

Follow tensorflow install guide to install tensorflow.

Tensorflow step by step instructions for WSL2

Python Virtual Environment

if use virtual venv, it also can work, just need to install right version tensorflow. and it needs to configure cuDNN path on activate script

vim <path to venv>/bin/activate

# After get VIRTUAL_ENV, include the CUDNN path

CUDNN_PATH=$(dirname $(python -c "import nvidia.cudnn;print(nvidia.cudnn.__file__)"))


WSL2 安裝 CUDA Toolkit、cuDNN