1. 安裝miniconda
- 下載安裝包
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh
- 執行程序
bash Miniconda3-latest-Linux-x86_64.sh
- 重啟終端或者source .bashrc后安裝完畢;執行conda,不報錯說明安裝成功
2. 更換為清華源
PS:后添加的會排在channels列表的最上面,因此越后添加的優先級越高。
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/pytorch/
conda config --set show_channel_urls yes
conda config --set auto_activate_base false
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
或者修改.condarc文件:
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- defaults
show_channel_urls: true
auto_activate_base: false
PS: 這里的順序最好一致,排在越上面的表示優先級越高,因此在安裝pytorch的時候,會優先在第一個源倉庫中去尋找。而gpu版本的pytorch也只有第一個倉庫中含有。
conda恢復默認源的方法:
config --remove-key channels
conda config --set show_channel_urls yes
conda config --set auto_activate_base false
或移除清華源:
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
或者修改.condarc文件:
channels:
- defaults
show_channel_urls: true
auto_activate_base: false
3. 新建python3.8環境
conda create -n torch python=3.8
並激活該環境,進入該環境后才能將pytorch安裝在此環境中:
conda activate torch
4. 安裝pytorch
安裝cuda和cudnn按照這篇文章。
安裝pytorch前需要查看本機的cuda版本:
nvcc -V
由圖可知cuda版本為11.0.221
查看cudnn版本
whereis cudnn
找到相應的目錄cat /usr/local/cudnn/include/cudnn_version.h | grep CUDNN_MAJOR -A 2
可知cudnn版本為8.1.1
如果未安裝cuda和cudnn,也可以使用conda來安裝,前提是conda源中含有相應的版本。
- 利用conda安裝cuda和cudnn(根據顯卡型號安裝相應驅動)
conda install cudatoolkit=11.0.221
- 安裝cudnn(如果沒有安裝cudnn,我們可以用conda安裝,否則這一步不需要)
通過下面的網址可以知道cudnn與cuda版本對應關系:
https://developer.nvidia.com/rdp/cudnn-archive#a-collapse742-10
從 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/linux-64/ 查看有無cuda版本對應的pytorch以及所需要的cudnn版本。
因此cudnn的版本最好為8.0.5
(但是我們這里選擇了cudnn=8.1.1,原因是cudnn 8.1.1也支持cuda11.0,選擇支持自己cuda版本的最新版本,向下兼容,見下圖)
4.1 使用默認源安裝pytorch
對應cuda版本11.0.221,我們找到了1.7.1版本的pytorch與之對應。
conda install pytorch==1.7.1 torchvision==0.8.2 torchaudio==0.7.2 cudatoolkit=11.0 -c pytorch
4.2 使用清華源安裝pytorch
conda install pytorch==1.7.1 torchvision==0.8.2 torchaudio==0.7.2 cudatoolkit=11.0
4.3 指定channel安裝pytorch
conda install pytorch==1.7.1 torchvision==0.8.2 torchaudio==0.7.2 cudatoolkit=11.0 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
4.4 本地安裝pytorch
這里用pytorch 1.7.1 cuda版本舉例。
4.4.1 下載所需的安裝包
從清華開源鏡像中找到所需要的安裝包:
使用wget下載對應的安裝包
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/linux-64/pytorch-1.7.1-py3.8_cuda11.0.221_cudnn8.0.5_0.tar.bz2
或者本地用迅雷下載資源(速度較快)[SCP指令部分]
然后通過scp指令上傳到服務器:
scp /Users/username/Downloads/filename username@servername:/path
補充:多文件拷貝
scp /Users/username/filedir/filename1 filename2 filename3 username@servername:/path
PS:
scp使用密鑰驗證並上傳文件到服務器:
scp -i 證書的絕對路徑/證書.perm 路徑/需要上傳的文件 username@servername:/path
scp下載單文件指令:
scp username@servername:/path/filename /localdir(本地目錄)
補充:
1. 多文件拷貝
scp username@servername:/path/\{filename1,filename2,filename3,filepre.* \} /localdir
2. scp默認連接的遠端主機22端口,如果ssh不是使用標准的22端口(以222為例)則使用-P(P大寫)指定:
scp -P 222 username@servername:/path/filename /localdir(本地目錄)
scp下載目錄文件指令:
scp -r username@servername:/var/www/remote_dir/(遠程目錄) /local_dir(本地目錄)
scp上傳目錄文件指令:
scp -r local_dir username@servername:remote_dir
4.4.2 conda本地安裝
下載完成后開始本地安裝。
conda install --use-local pytorch-1.7.1-py3.8_cuda11.0.221_cudnn8.0.5_0.tar.bz2
測試時候會報錯,原因是沒有安裝相關依賴:
提示說明缺少動態鏈接庫文件,文件在mkl庫中,需要另外安裝。
安裝所需要的依賴后即可:
conda install mkl
5. 測試pytorch是否安裝成功
終端執行python,並輸入以下命令:
import torch
print(torch.__version__, torch.cuda.is_available())
如果結果如下,則說明cuda版本的pytorch安裝成功。
6. 安裝pytorch_lightning
前提:安裝的pytorch至少應該是1.7版本。
conda install pytorch-lightning