1. 环境版本
Ubuntu 20.04
Python 3.8 (基于anaconda3)
nvidia-driver 460.73
cuda-version 11.2
pytorch 1.8.1
2. 安装过程
1. 安装nvidia驱动
sudo apt install nvidia-driver-460
安装完后重启系统,shell中输入nvidia-smi 出现如下页面即安装成功
+-----------------------------------------------------------------------------+ | NVIDIA-SMI 460.73.01 Driver Version: 460.73.01 CUDA Version: 11.2 | |-------------------------------+----------------------+----------------------+ | GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. | | | | MIG M. | |===============================+======================+======================| | 0 GeForce MX150 Off | 00000000:01:00.0 Off | N/A | | N/A 52C P0 N/A / N/A | 278MiB / 2002MiB | 7% Default | | | | N/A | +-------------------------------+----------------------+----------------------+ +-----------------------------------------------------------------------------+ | Processes: | | GPU GI CI PID Type Process name GPU Memory | | ID ID Usage | |=============================================================================| | 0 N/A N/A 927 G /usr/lib/xorg/Xorg 45MiB | | 0 N/A N/A 1694 G /usr/lib/xorg/Xorg 109MiB | | 0 N/A N/A 1878 G /usr/bin/gnome-shell 37MiB | | 0 N/A N/A 2549 G .../files/bin/baidu-qimpanel 15MiB | | 0 N/A N/A 3359 G ...AAAAAAAAA= --shared-files 62MiB | +-----------------------------------------------------------------------------+
2. 安装anaconda环境
官网地址:https://www.anaconda.com/products/individual#Downloads
选择对应的版本下载即可
下载下来的是一个sh文件,比如 Anaconda3-2020.11-Linux-x86_64.sh
由于没有执行权限,所以我们需要先给他赋予执行权限,然后安装
sudo chmod 774 Anaconda3-2020.11-Linux-x86_64.sh bash Anaconda3-2020.11-Linux-x86_64.sh
详细安装过程可以参考 Ubuntu安装Anaconda3
3. 安装 CUDA&cudnn
cuda安装官网地址: https://developer.nvidia.com/cuda-toolkit-archive
选择需要的版本安装即可,这里我选择 CUDA Toolkit 11.2.2 (March 2021), Versioned Online Documentation这一个
点进去有详细的安装方法
先选择自己的平台,这里我是ubuntu20.04
然后下边会有安装命令,按照命令执行即可:
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600 wget https://developer.download.nvidia.com/compute/cuda/11.2.0/local_installers/cuda-repo-ubuntu2004-11-2-local_11.2.0-460.27.04-1_amd64.deb sudo dpkg -i cuda-repo-ubuntu2004-11-2-local_11.2.0-460.27.04-1_amd64.deb sudo apt-key add /var/cuda-repo-ubuntu2004-11-2-local/7fa2af80.pub sudo apt-get update sudo apt-get -y install cuda
执行完后 /usr/local 目录下就会出现cuda的文件
然后将cuda添加到环境变量(追加到 /etc/profile 或 家目录的 .bashrc 中均可):
export PATH=/usr/local/cuda/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH export CUDA_HOME=/usr/local/cuda export PATH=${CUDA_HOME}/lib64:$PATH
shell中执行 source /etc/profile 或 source ~/.bashrc 更新环境变量,shell中输入 nvcc --version 可以看到版本号即成功( 如果不成功,可以尝试重启电脑)
4. 安装 pytorch
pytorch 的清华镜像站地址:https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/linux-64/
选择要安装的版本,这里采用本地安装方式:
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/linux-64/pytorch-1.8.1-py3.8_cuda11.1_cudnn8.0.5_0.tar.bz2 conda install --offline pytorch-1.8.1-py3.8_cuda11.1_cudnn8.0.5_0.tar.bz2
安装完毕后在python中测试
看到可以打印正常版本号,说明安装成功。