TensorRT版本不匹配致libnvinfer.so.7加载失败求助
解决TensorFlow 2.11.0加载TensorRT 8.x时寻找libnvinfer.so.7的问题
环境信息
当前使用的conda环境名为tf,环境配置如下:
Ubuntu 22.04 python 3.10.8 tensorflow 2.11.0 cudatoolkit 11.2.2 cudnn 8.1.0.77 nvidia-tensorrt 8.4.3.1 nvidia-pyindex 1.0.9
TensorRT库文件路径:/home/dan/anaconda3/envs/tf/lib/python3.10/site-packages/tensorrt,包含文件:
libnvinfer_builder_resource.so.8.4.3 libnvinfer_plugin.so.8 libnvinfer.so.8 libnvonnxparser.so.8 libnvparsers.so.8 tensorrt.so
报错信息
执行命令 python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))" 后出现以下警告:
tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: :/home/dan/anaconda3/envs/tf/lib tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: :/home/dan/anaconda3/envs/tf/lib tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly. [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
解决方案建议
1. 创建软链接兼容版本
直接在TensorRT库目录下创建指向现有8.x库的软链接,让系统识别到7.x的引用:
cd /home/dan/anaconda3/envs/tf/lib/python3.10/site-packages/tensorrt ln -s libnvinfer.so.8 libnvinfer.so.7 ln -s libnvinfer_plugin.so.8 libnvinfer_plugin.so.7
更新系统库缓存:
sudo ldconfig
之后重新执行测试命令,检查警告是否消失。
2. 安装TensorFlow 2.11.0兼容的TensorRT版本
TensorFlow 2.11.0默认依赖TensorRT 7.x系列,卸载当前8.x版本后安装对应版本(以7.6.1为例):
pip uninstall nvidia-tensorrt -y pip install nvidia-tensorrt==7.6.1
若pip安装失败,尝试通过conda-forge安装:
conda install -c conda-forge tensorrt=7.6.1
3. 升级TensorFlow到兼容TensorRT 8.x的版本
TensorFlow 2.12及以上版本开始支持TensorRT 8.x,若业务允许可升级TensorFlow:
pip install tensorflow==2.12.0
注意:升级后需同步检查CUDA/CUDNN兼容性(TensorFlow 2.12推荐CUDA 11.8),当前CUDA 11.2可能需要一并升级。
注意事项
- 软链接为临时兼容方案,后续升级TensorFlow或TensorRT时需清理对应软链接
- 优先遵循官方版本兼容矩阵,确保CUDA、CUDNN、TensorRT、TensorFlow版本完全匹配
- 使用conda环境时,尽量避免混用pip和conda安装NVIDIA相关库,防止版本冲突
内容的提问来源于stack exchange,提问作者Daniel von Eschwege
相关产品推荐
相关产品推荐

