Ubantu系统在anaconda中安装pytorch

方式1:进入官网下载安装文件
方式2:pip install命令安装

方式1:官网下载pytorch
Ubantu系统在anaconda中安装pytorch_第1张图片

第一行“PyTorch Build”,我选了稳定版
第二行“Your OS”,我选了Linux系统
第三行“Package”,我选了Conda。因为我系统上已经安装过Anacnda,带有conda环境。读者也可以勾选pip,使用pip命令安装PyTorch
第四行“Language”,我选了python
第五行“CUDA”:安装cuda的选择相应版本,没有安装的选None
然后,拷贝第六行"Run this Command"命令

执行命令之前,先用anaconda创建虚拟环境

conda create -n pytorch pip python=3.5

创建了一个叫做pytorch的虚拟环境(名字自己取),并在这个虚拟环境中安装了python3.5

source activate pytorch

激活虚拟环境

在虚拟环境中安装cuda和cudnn

GPU版本的pytorch安装比较复杂,在安装pytorch之前,通常需要安装显卡驱动,cuda和cudnn,CUDA是NVIDIA推出的用于自家GPU的并行计算框架,也就是说CUDA只能在NVIDIA的GPU上运行;cuDNN(CUDA Deep Neural Network library):是NVIDIA打造的针对深度神经网络的加速库,是一个用于深层神经网络的GPU加速库。如果你要用GPU训练模型,cuDNN不是必须的,但是一般会采用这个加速库。CUDA是必须的,cudnn是可选的。
引自:Ubuntu18.04安装Pytorch

1.查看显卡型号以及是否CUDA
Ubantu系统在anaconda中安装pytorch_第2张图片
“driver”中后面带有“recommanded”就是推荐的显卡驱动型号。根据显卡型号到英伟达官网查询是否支持GPU。
https://developer.nvidia.com/cuda-gpus
Ubantu系统在anaconda中安装pytorch_第3张图片
查到
Ubantu系统在anaconda中安装pytorch_第4张图片
安装recommand的显卡驱动

sudo apt install nvidia-430
nvidia-smi

Ubantu系统在anaconda中安装pytorch_第5张图片
显示以上信息,驱动安装好了。

其实,可以先用nvidia-smi查一下,有的话就别安了

查看gcc版本
Ubantu系统在anaconda中安装pytorch_第6张图片

安装cuda可以到官网下载文件进行安装,也可以执行下面的命令安装
cuda网址
Ubantu系统在anaconda中安装pytorch_第7张图片
拷贝执行下面的两条命令
Ubantu系统在anaconda中安装pytorch_第8张图片

wget https://developer.download.nvidia.com/compute/cuda/11.0.3/local_installers/cuda_11.0.3_450.51.06_linux.run
sudo sh cuda_11.0.3_450.51.06_linux.run

安装时出现

Existing package manager installation of the driver found. It is strongly │
│ recommended that you remove this before continuing.

Ubantu系统在anaconda中安装pytorch_第9张图片
解决办法:

1.若出现’package manager installation of the driver found‘,换为另一种方法
sudo apt install nvidia-cuda-toolkit
引自:Ubuntu下的CUDA和CUDNN的下载及安装
https://blog.csdn.net/iefyy/article/details/102740388

sudo apt install nvidia-cuda-toolkit

Ubantu系统在anaconda中安装pytorch_第10张图片
最后查看cuda版本

nvcc -V

在这里插入图片描述
安装cudnn
网址:https://developer.nvidia.com/rdp/cudnn-archive#a-collapse742-10
Ubantu系统在anaconda中安装pytorch_第11张图片
选择与cuda对应的版本
进去下载,需要注册账号
进去之后,可选的有:
Ubantu系统在anaconda中安装pytorch_第12张图片
这三种的区别,请参考博客:cuDNN的安装(版本选择, Runtime 还是 Developer)

这里我选择cuDNN Library for Linux

吐槽:下载cudnn时,注册让填一大堆东西,太恶心了,最终放弃了注册

cudnn就不安装了,本来它就是个可选项

官方教程,没有试试:https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html#prerequisites

安装pytorch
网址:https://pytorch.org/get-started/previous-versions/

# CUDA 10.1
pip install torch==1.5.1+cu101 torchvision==0.6.1+cu101 -f https://download.pytorch.org/whl/torch_stable.html

安装成功!
Ubantu系统在anaconda中安装pytorch_第13张图片
这里记录下安装过程:

(pytorch) tyb@amax:~$ pip install torch==1.5.0+cu101 torchvision==0.6.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html
Collecting torch==1.5.0+cu101
  Downloading https://download.pytorch.org/whl/cu101/torch-1.5.0%2Bcu101-cp35-cp35m-linux_x86_64.whl (703.8MB)
    100% |████████████████████████████████| 703.8MB 2.0kB/s
Collecting torchvision==0.6.0+cu101
  Downloading https://download.pytorch.org/whl/cu101/torchvision-0.6.0%2Bcu101-cp35-cp35m-linux_x86_64.whl (6.6MB)
    100% |████████████████████████████████| 6.6MB 190kB/s
Requirement already satisfied: numpy in ./.conda/envs/pytorch/lib/python3.5/site-packages (from torch==1.5.0+cu101)
Collecting future (from torch==1.5.0+cu101)
  Downloading https://files.pythonhosted.org/packages/45/0b/38b06fd9b92dc2b68d58b75f900e97884c45bedd2ff83203d933cf5851c9/future-0.18.2.tar.gz (829kB)
    100% |████████████████████████████████| 829kB 204kB/s
Requirement already satisfied: pillow>=4.1.1 in ./.conda/envs/pytorch/lib/python3.5/site-packages (from torchvision==0.6.0+cu101)
Requirement already satisfied: olefile in ./.conda/envs/pytorch/lib/python3.5/site-packages (from pillow>=4.1.1->torchvision==0.6.0+cu101)
Building wheels for collected packages: future
  Running setup.py bdist_wheel for future ... done
  Stored in directory: /home/tyb/.cache/pip/wheels/8b/99/a0/81daf51dcd359a9377b110a8a886b3895921802d2fc1b2397e
Successfully built future
Installing collected packages: future, torch, torchvision
  Found existing installation: torch 0.1.12.post1
    Uninstalling torch-0.1.12.post1:
      Successfully uninstalled torch-0.1.12.post1
  Found existing installation: torchvision 0.1.8
    Uninstalling torchvision-0.1.8:
      Successfully uninstalled torchvision-0.1.8
Successfully installed future-0.18.2 torch-1.5.0+cu101 torchvision-0.6.0+cu101

这个如果安装成功了,下面的就不要看了!!!!

安装pytorch

conda install pytorch torchvision cuda100 -c pytorch

注:cuda100指cuda10.0版本,如果是10.1则cuda101

网址:https://pytorch.org/get-started/locally/
网址:https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html#prerequisites

conda install pytorch==1.5.1 torchvision==0.6.1 cudatoolkit=10.0 -c pytorch

我安装的cudatoolkit是10.0版本,所以这里选择cudatoolkit=10.0

不要使用下面的安装方法,用了可能也不成功,反正,我试了,没有成功!

conda install cuda=10.2

报错:

Fetching package metadata .......
Solving package specifications: .
PackageNotFoundError: Package not found: '' Package missing in current linux-64 channels:
  - cuda 10.2*

You can search for packages on anaconda.org with

    anaconda search -t conda cuda

正确做法

anaconda search -t conda cuda

执行结果:

Using Anaconda API: https://api.anaconda.org
Run 'anaconda show ' to get more details:
Packages:
     Name                      |  Version | Package Types   | Platforms
     ------------------------- |   ------ | --------------- | ---------------
     BioBuilds/barracuda       | 0.7.107e | conda           | linux-ppc64le, linux-64
                                          : GPU-accelerated implementation of the BWA short read aligner
     CannyLab/cuda100          |      1.0 | conda           | linux-64
     CannyLab/cuda101          |      1.0 | conda           | linux-64
     CannyLab/cuda90           |      1.0 | conda           | linux-64
     CannyLab/cuda92           |      1.0 | conda           | linux-64
     CannyLab/tsnecuda         |    2.1.0 | conda           | linux-64
                                          : CUDA Implementation of T-SNE with Python bindings
     HCC/cuda-driver           |   390.46 | conda           | linux-64
                                          : NVIDIA linux driver
     HCC/cuda_driver           |   440.64 | conda           | linux-64
                                          : NVIDIA linux driver
     HCC/cudatoolkit           |  10.2.89 | conda           | linux-64
                                          : NVIDIA CUDA Toolkit
     acellera/cuda-runtime     |  9.0.176 | conda           | linux-64
     anaconda/cudatoolkit      | 11.0.221 | conda           | linux-ppc64le, linux-64, win-64, osx-64
                                          : CUDA Toolkit - Including CUDA runtime and headers
     aoeftiger/pycuda          | 2015.1.2 | conda           | linux-64
                                          : Python wrapper for Nvidia CUDA
     caffe2/caffe2-cuda-cudnn-full |  0.8.dev | conda           | linux-64
                                          : Caffe2 is a lightweight, modular, and scalable deep learning framework.
     caffe2/caffe2-cuda8.0-cudnn7 |  0.8.dev | conda           | linux-64
                                          : Caffe2 is a lightweight, modular, and scalable deep learning framework.
     caffe2/caffe2-cuda9.0-cudnn7 |  0.8.dev | conda           | linux-64
                                          : Caffe2 is a lightweight, modular, and scalable deep learning framework.
     caffe2/caffe2-cuda9.0-cudnn7-full |  0.8.dev | conda           | linux-64
                                          : Caffe2 is a lightweight, modular, and scalable deep learning framework.
     caffe2/pytorch-caffe2-cuda8.0-cudnn7 | 2018.08.26 | conda           | linux-64
                                          : Caffe2 is a lightweight, modular, and scalable deep learning framework.
     caffe2/pytorch-caffe2-cuda9.0-cudnn7 | 2018.08.25 | conda           | linux-64
                                          : Caffe2 is a lightweight, modular, and scalable deep learning framework.
     conda-forge/cudatoolkit-dev | 10.1.243 | conda           | linux-64, osx-64
                                          : Develop, Optimize and Deploy GPU-accelerated Apps
     conda-forge/omniscidb-cuda |    5.0.0 | conda           | linux-64
                                          : The OmniSci database
     conda-forge/pocl-cuda     |      1.5 | conda           | linux-ppc64le, linux-64
                                          : Portable Computing Language -- a CPU OpenCL implementation
     cpbotha/magma-cuda10      |    2.4.0 | conda           | linux-64
     deepcognition/caffe2-cuda9.0-cudnn7 |  0.8.dev | conda           | linux-64
                                          : Caffe2 is a lightweight, modular, and scalable deep learning framework.
     dglteam/dgl-cuda10.0      |    0.5.2 | conda           | linux-64, win-64
     dglteam/dgl-cuda10.1      |    0.5.2 | conda           | linux-64, win-64
     dglteam/dgl-cuda10.2      |    0.5.2 | conda           | linux-64, win-64
     dglteam/dgl-cuda9.0       |    0.5.2 | conda           | linux-64, win-64
     dglteam/dgl-cuda9.2       |    0.5.2 | conda           | linux-64, win-64
     fragcolor/cuda10.0        |      1.0 | conda           | noarch
     free/cudatoolkit          |      8.0 | conda           | linux-ppc64le, linux-64, win-64, osx-64
                                          : development environment for GPU-accelerated applications
     georgedimitriadis/t_sne_bhcuda |    0.2.1 | conda           | linux-64, win-64
     h2oai/h2o4gpu-cuda10      |    0.4.0 | conda           | linux-64
                                          : H2O4GPU is a collection of GPU solvers by H2O.ai with APIs in Python and R.
     h2oai/h2o4gpu-cuda9       |    0.3.2 | conda           | linux-64
                                          : H2O4GPU is a collection of GPU solvers by H2O.ai with APIs in Python and R.
     h2oai/h2o4gpu-cuda92      | 0.3.0.10000 | conda           | linux-64
                                          : H2O4GPU is a collection of GPU solvers by H2O.ai with APIs in Python and R.
     jaikumarm/t_sne_bhcuda    |    0.2.1 | conda           | linux-64
     jjh_cio_testing/cudatoolkit |      9.0 | conda           | linux-ppc64le, linux-64, win-64
     jjhelmus/cudatoolkit      |  10.2.89 | conda           | win-64
     kayarre/pycuda            |   2016.1 | conda           | linux-64, osx-64
                                          : access Nvidia's CUDA parallel computation API from Python.
     kitware-danesfield-df/cudatoolkit |      9.0 | conda           | linux-64
     lukepfister/pycuda        |   2017.1 | conda           | linux-64
     lukepfister/scikits.cuda  |    0.5.2 | conda           | linux-64
                                          : Python interface to GPU-powered libraries
     maccallum_lab/meld-cuda75 |   0.4.15 | conda           | linux-64, osx-64
     maccallum_lab/meld-cuda80 |   0.4.15 | conda           | linux-64, osx-64
     maccallum_lab/openmm-cuda75 |    7.2.2 | conda           | linux-64, osx-64
                                          : A high performance toolkit for molecular simulation.
     maccallum_lab/openmm-cuda80 |    7.2.2 | conda           | linux-64, osx-64
                                          : A high performance toolkit for molecular simulation.
     maccallum_lab/openmm-cuda90 |    7.2.2 | conda           | linux-64
                                          : A high performance toolkit for molecular simulation.
     main/cudatoolkit          | 11.0.221 | conda           | linux-ppc64le, linux-64, win-64, osx-64
                                          : CUDA Toolkit - Including CUDA runtime and headers
     marta-sd/cudatoolkit      |      8.0 | conda           | linux-64
                                          : Testing ground for CUDA Toolkit with Numba
     mutirri/pycuda            | 2013.1.1 | conda           | linux-64
                                          : http://mathema.tician.de/software/pycuda/
     mwojcikowski/cudatoolkit  |      8.0 | conda           | linux-64
                                          : Testing ground for CUDA Toolkit with Numba
     nehaljwani/cudatoolkit    |      8.0 | conda           | win-64
     neurokernel/pycuda        |          | conda           | linux-64
                                          : Python wrapper for NVIDIA CUDA.
     neurokernel/scikit-cuda   |          | conda           | linux-64
                                          : Python interface to GPU-powered libraries.
     numba/cudatoolkit         |      9.1 | conda           | linux-64, win-64, osx-64
     nusdbsystem/singa-cuda7.5-cudnn5 |    1.1.0 | conda           | linux-64
     nusdbsystem/singa-cuda8.0-cudnn5 |    1.1.0 | conda           | linux-64
     nvidia/cudatoolkit        | 11.0.221 | conda           | linux-64, win-64
                                          : CUDA Toolkit - Including CUDA runtime and headers
     oddconcepts/opencv-cuda   |    3.4.2 | conda           | linux-64
                                          : Computer vision and machine learning software library.
     omnia/cuda92              |      1.0 | conda           | noarch
     omnia/openmm-cuda75       |    7.2.1 | conda           | linux-64, osx-64
                                          : A high performance toolkit for molecular simulation.
     peterjc123/cuda80         |      1.0 | conda           | win-64
     peterjc123/cuda90         |      1.0 | conda           | win-64
     pjones/magma-cuda91       |    2.3.0 | conda           | linux-ppc64le, linux-64
     prometeia/cudatoolkit     |  10.2.89 | conda           | linux-64, win-64
     pytorch/cuda100           |      1.0 | conda           | linux-64, win-64
     pytorch/cuda75            |      1.0 | conda           | linux-64
     pytorch/cuda80            |      1.0 | conda           | linux-64, win-64
     pytorch/cuda90            |      1.0 | conda           | linux-64, win-64
     pytorch/cuda91            |      1.0 | conda           | linux-64, win-64
     pytorch/cuda92            |      1.0 | conda           | linux-64, win-64
     pytorch/magma-cuda100     |    2.5.2 | conda           | linux-64
     pytorch/magma-cuda101     |    2.5.2 | conda           | linux-64
     pytorch/magma-cuda102     |    2.5.2 | conda           | linux-64
     pytorch/magma-cuda110     |    2.5.2 | conda           | linux-64
     pytorch/magma-cuda75      |    2.2.0 | conda           | linux-64
     pytorch/magma-cuda80      |    2.3.0 | conda           | linux-64
     pytorch/magma-cuda90      |    2.5.0 | conda           | linux-64
     pytorch/magma-cuda91      |    2.3.0 | conda           | linux-64
     pytorch/magma-cuda92      |    2.5.2 | conda           | linux-64
     
     
                                          : dask-cuda library
Found 100 packages

找到了100个包,以 pytorch/magma-cuda92为例

anaconda show pytorch/magma-cuda92

Ubantu系统在anaconda中安装pytorch_第14张图片
找到pytorch/magma-cuda92正确的安装命令为:

 conda install --channel https://conda.anaconda.org/pytorch magma-cuda92

安装速度较慢

conda install cudnn=8.0.3

直接运行该命令,会自动安装cudnn的依赖性conda.
cudnn的版本为8.0.3,对应的cuda版本为10.2
下面网站可以查询cudnn和cuda版本间的对应关系
网站

为快速安装可以加上国内镜像
命令修改为:

conda install cudnn=8.0.3 -c https://pypi.tuna.tsinghua.edu.cn/simple/

可选的镜像有:
阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/ > 豆瓣(douban) http://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/

查看cuda版本:

nvcc --version

cat /usr/local/cuda/version.txt

查看cudnn版本

cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2

确认cudn版本和最前面生成安装pytorch命令的版本一致
执行命令:

conda install pytorch torchvision cudatoolkit=10.0 -c pytorch

版本号不一致可自己更改

至此,pytorch安装完成
查看pytorch版本号:
进入python交互环境

import torch
print(torch.__version__) 

参考:
使用anaconda创建虚拟环境安装不同深度学习框架
Ubuntu18.04安装Pytorch
Anaconda-- conda 创建、激活、退出、删除虚拟环境
CUDA 版本,显卡驱动,Ubuntu版本,GCC版本之间的对应关

你可能感兴趣的:(Ubantu,pytorch,cuda,cudnn,Ubantu)