1 Could not load library libcudnn_cnn_infer.so.8. Error libcuda.so cannot open shared object file No such file or directory

Windows WSL2 Ubuntu系统下推理AI模型,出现了以下问题

Could not load library libcudnn_cnn_infer.so.8. Error: libcuda.so: cannot open shared object file: No such file or directory
Please make sure libcudnn_cnn_infer.so.8 is in your library path!

首先,我们先查找libcudnn_cnn_infer.so.8的位置,使用以下命令

apt install mlocate
locate libcuda.so

输出以下信息

/mnt/c/Windows/System32/DriverStore/FileRepository/nv_dispi.inf_amd64_5714f0dca6485379/libcuda.so.1.1
/mnt/c/Windows/System32/DriverStore/FileRepository/nv_dispi.inf_amd64_cc569e59ca39c5fe/libcuda.so.1.1
/mnt/c/Windows/System32/DriverStore/FileRepository/nv_dispi.inf_amd64_ea7f458f0e49497d/libcuda.so.1.1
/mnt/c/Windows/System32/DriverStore/FileRepository/nv_dispig.inf_amd64_0905d5f86c27c241/libcuda.so.1.1
/mnt/c/Windows/System32/lxss/lib/libcuda.so
/mnt/c/Windows/System32/lxss/lib/libcuda.so.1
/mnt/c/Windows/System32/lxss/lib/libcuda.so.1.1
/usr/lib/wsl/drivers/nv_dispi.inf_amd64_5714f0dca6485379/libcuda.so.1.1
/usr/lib/wsl/drivers/nv_dispi.inf_amd64_cc569e59ca39c5fe/libcuda.so.1.1
/usr/lib/wsl/drivers/nv_dispi.inf_amd64_ea7f458f0e49497d/libcuda.so.1.1
/usr/lib/wsl/drivers/nv_dispig.inf_amd64_0905d5f86c27c241/libcuda.so.1.1
/usr/lib/wsl/lib/libcuda.so
/usr/lib/wsl/lib/libcuda.so.1
/usr/lib/wsl/lib/libcuda.so.1.1
/usr/local/cuda-11.8/targets/x86_64-linux/lib/stubs/libcuda.so

可以从输出信息看出,libcuda.so主要位于/usr/lib/wsl/lib目录下,所以我们需要将/usr/lib/wsl/lib加入环境变量。

vim ~/.bashrc

然后增加以下语句

export LD_LIBRARY_PATH=/usr/lib/wsl/lib:$LD_LIBRARY_PATH

保存退出,然后执行以下命令更新环境变量

source ~/.bashrc

再次推理模型,运行成功。