ubuntu安装cuda,cudnn,pytorch

1.conda创建虚拟环境

conda create -n pytorch pip python=3.8

激活虚拟环境

source activate pytorch

2.虚拟环境中安装cuda和cudnn

添加国内镜像


添加清华源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
并设置搜索时显示通道地址:
conda config --set show_channel_urls yes

注:使用以下命令恢复默认源
conda config --remove-key channels

安装cuda

conda install cudatoolkit=10.1 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/linux-64/

也可用:

conda install cudatoolkit=10.1
也可以不带通道,因为前面已经配置了通道,使用conda命令安装是,会自动从配置的通道中找安装源

安装成功:

Downloading and Extracting Packages
cudatoolkit-10.1.243 | 347.4 MB  | ##################################################################################### | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

安装cudnn

conda install cudnn=7.6.5 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64/

报错:

Downloading and Extracting Packages
cudnn-7.6.5          | 179.9 MB  | ####5                                                                                 |   5%

CondaError: Downloaded bytes did not match Content-Length
  url: https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64/cudnn-7.6.5-cuda10.1_0.conda
  target_path: /data/tyb/.conda/pkgs/cudnn-7.6.5-cuda10.1_0.conda
  Content-Length: 188683093
  downloaded bytes: 10206893

修改等待时间即可解决

conda config --set remote_read_timeout_secs 5000.0

注:这里timeout时间为5000s,表示有5000s的时间从镜像源下载cuda,如果时间不够,读者再加即可。

安装成功:

在这里插入代码片

这里的安装路径没有单独形成cuda文件夹,都是统一存放在envs/pytorch[虚拟环境]/lib or include 文件夹下。

安装pytorch
使用此链接组成安装命令
https://pytorch.org/get-started/locally/
在这里插入图片描述
把命令复制出来,去掉-c pytorch,安装的时候才会默认从清华源下载相应的包

conda install pytorch torchvision torchaudio cudatoolkit=10.2 

注:使用此命令会安装cuda 10.2,安装pytorch

########################################################

分割线,后面的不用看

########################################################

安装cuda

conda install cudatoolkit=11.0 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/linux-64/

如果报错:

CondaError: Downloaded bytes did not match Content-Length
  url: https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64/cudatoolkit-11.0.221-h6bb024c_0.conda
  target_path: /data/tyb/.conda/pkgs/cudatoolkit-11.0.221-h6bb024c_0.conda
  Content-Length: 653169762
  downloaded bytes: 515129004

则,修改等待时间(有的时候需要更长的等待时间)

conda config --set remote_read_timeout_secs 5000.0

注:这里timeout时间为5000s,表示有5000s的时间从镜像源下载cuda,如果时间不够,读者再加即可。

安装cudnn

conda install cudnn=8.2.0 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64/

注:注意cuda和cudnn的版本对应关系,可参考:https://developer.nvidia.com/rdp/cudnn-archive
在这里插入图片描述

报错,提示从以下源中找不到cudnn安装包:


PackagesNotFoundError: The following packages are not available from current channels:

  - cudnn=8.2.0

Current channels:

  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/noarch
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/linux-64
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/noarch
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/linux-64
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/noarch
  - https://repo.anaconda.com/pkgs/main/linux-64
  - https://repo.anaconda.com/pkgs/main/noarch
  - https://repo.anaconda.com/pkgs/r/linux-64
  - https://repo.anaconda.com/pkgs/r/noarch

To search for alternate channels that may provide the conda package you're
looking for, navigate to

    https://anaconda.org

and use the search bar at the top of the page.

分析原因:应该是笔者使用的cudnn版本过新,那些镜像源中还没把最近的cudnn版本加到镜像源中,自然在那些镜像源中找不到

从cudnn官网下载对应的cudnn版本
在这里插入图片描述

注:红框中的四个文件都下载下来

这个需要注册才能下载,笔者本来准备不安装了

看到官网介绍,想想还是安吧
在这里插入图片描述

当然,注册是不可能滴!!!
看我骚操作:
在下载链接上右键,复制链接,然后用迅雷下载!

安装1:

通过命令解压文件
tar -xzvf cudnn-11.0-linux-x64-v8.0.5.39.tgz

安装2:

$ sudo dpkg -i libcudnn8_8.0.5.39-1+cuda11.0_amd64.deb
$ sudo dpkg -i libcudnn8-dev_8_8.0.5.39-1+cuda11.0_amd64.deb
$ sudo dpkg -i libcudnn8-samples_8_8.0.5.39-1+cuda11.0_amd64.deb

注:使用此命令时,首先要把安装包放在当前目录下

查看cudnn版本信息

dpkg -l | grep cudnn

dpkg -l 命令详解 dpkg -l 命令会列出系统中所有已安装的软件包信息。结合grep,可以过滤出自己想要的内容。

Guess you like

Origin blog.csdn.net/t18438605018/article/details/118311839