ubuntu18.04 GeForce2070配置cuda 11.3+TensorRT 8环境

1.安装方式

        nvidia的cuda、cudnn和tensorRT的安装文件有tar、deb和tar压缩包三种,分别对应三种安装方式。tensorRT的安装方式必须和cuda安装方式保持一致,不然会出类似下面的错误:

The following packages have unmet dependencies:
 python3-libnvinfer : Depends: libnvinfer8 (= 8.2.2-1+cuda11.4) but it is not going to be installed
                      Depends: libnvinfer-plugin8 (= 8.2.2-1+cuda11.4) but it is not going to be installed
                      Depends: libnvparsers8 (= 8.2.2-1+cuda11.4) but it is not going to be installed
                      Depends: libnvonnxparsers8 (= 8.2.2-1+cuda11.4) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

因此,这里对于cuda、cudnn、tensorRT均采用deb安装方式。

        我笔记本nvidia显卡是GeForce2070,由于不想安装太新的包,所以选择cuda11.3,cudnn8.2.1。

2. 驱动安装

首先是安装nvidia驱动。 

2.1 查看显卡硬件型号

在终端输入:ubuntu-drivers devices,得到

vendor   : NVIDIA Corporation
driver   : nvidia-driver-515-server - distro non-free
driver   : nvidia-driver-450 - third-party non-free
driver   : nvidia-driver-465 - third-party non-free
driver   : nvidia-driver-495 - third-party non-free
driver   : nvidia-driver-460 - third-party non-free
driver   : nvidia-driver-470 - third-party non-free
driver   : nvidia-driver-470-server - distro non-free
driver   : nvidia-driver-440 - third-party non-free
driver   : nvidia-driver-510 - third-party non-free
driver   : nvidia-driver-515 - third-party non-free recommended
driver   : nvidia-driver-450-server - distro non-free
driver   : nvidia-driver-510-server - distro non-free
driver   : nvidia-driver-455 - third-party non-free
driver   : xserver-xorg-video-nouveau - distro free builtin

里面有一行的最右侧写着 recommend,这个版本是系统建议安装的,下面的autoinstall就是自动安装这个,也可以自己选择版本,但是,在后面用deb安装cuda时都会被替换为cuda对应的驱动。

2.2 安装驱动

sudo ubuntu-drivers autoinstall
或者
sudo apt-get install nvidia-470

安装完成后重启。

3. cuda及cudnn安装

     进入 cuda页面,选择cuda 11.3.1版本,并按照官网上11.3步骤安装。

 

ubuntu18.04 GeForce2070配置cuda 11.3+TensorRT 8环境_第1张图片

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin
sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/11.3.1/local_installers/cuda-repo-ubuntu1804-11-3-local_11.3.1-465.19.01-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1804-11-3-local_11.3.1-465.19.01-1_amd64.deb
sudo apt-key add /var/cuda-repo-ubuntu1804-11-3-local/7fa2af80.pub
sudo apt-get update
sudo apt-get -y install cuda

将cuda路径添加到~/.bashrc中,不然nvcc找不到。

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

重启计算机,然后,在终端输入nvidia-smi,显示如下信息表示安装成功: ubuntu18.04 GeForce2070配置cuda 11.3+TensorRT 8环境_第2张图片

 此前我的nvidia驱动是470,从上图可以看到驱动已经替换为11.3对应的465了。

在终端输入命令 nvcc -V,输出

        进入cudnn页面,选择cuda 11.3对应的cudnn版本,一共3个deb文件。

ubuntu18.04 GeForce2070配置cuda 11.3+TensorRT 8环境_第3张图片

按如下顺序安装上面下载的三个文件,不然会出错

sudo dpkg -i libcudnn8_8.2.1.32-1+cuda11.3_amd64.deb
sudo dpkg -i libcudnn8-dev_8.2.1.32-1+cuda11.3_amd64.deb
sudo dpkg -i libcudnn8-samples_8.2.1.32-1+cuda11.3_amd64.deb

测试是否安装成功,

cp -r /usr/src/cudnn_samples_v8/ ~  
cd ~/cudnn_samples_v8/mnistCUDNN/  
make clean && make 
./mnistCUDNN 

出现Test passed!没有报错即安装成功。 

4. TensorRT安装

官网tensorRT页面下载cuda11.3对应的tensorRT版本的deb文件。

ubuntu18.04 GeForce2070配置cuda 11.3+TensorRT 8环境_第4张图片

 按照官网安装指南安装

sudo dpkg -i nv-tensorrt-repo-ubuntu1804-cuda11.3-trt8.0.3.4-ga-20210831_1-1_amd64.deb
sudo apt-key add /var/nv-tensorrt-repo-ubuntu1804-cuda11.3-trt8.0.3.4-ga-20210831/7fa2af80.pub

sudo apt-get update
sudo apt-get install tensorrt

安装python3-libnvinfer,如果使用 Python 3.x:

python3 -m pip install numpy
sudo apt-get install python3-libnvinfer-dev

安装与tensorflow对应的包以及onnx:

python3 -m pip install protobuf
sudo apt-get install uff-converter-tf

python3 -m pip install numpy onnx
sudo apt-get install onnx-graphsurgeon

验证安装是否成功

dpkg -l | grep TensorRT

ubuntu18.04 GeForce2070配置cuda 11.3+TensorRT 8环境_第5张图片

 

5. pytorch和tensorflow安装

        CUDA有两种API,运行时API和驱动API,即所谓的Runtime API与Driver API。nvidia-smi的结果是CUDA Driver API的型号,而nvcc -V得结果对应CUDA Runtime API,安装pytorch、tensorflow时要依据nvcc -V的版本。

6. 安装过程的一些报错

1. W: GPG error: Index of /compute/cuda/repos/ubuntu1804/x86_64 12 InRelease: The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY A4B469963BF863CC

apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/7fa2af80.pub 

你可能感兴趣的:(环境配置,python,linux)