1. 安装Anaconda
- Anaconda下载(Linux)
- 安装
bash Anaconda3-2019.10-Linux-x86_64.sh
2. 创建虚拟环境
不同的代码需要不同版本的Python或Pytorch,使用虚拟环境是有必要的。
- 创建虚拟环境:
conda create -n your_env_name python=X.X
conda create -n py3.6-torch1.3 python=3.6
- 激活虚拟环境:
conda activate your_env_name
conda activate py3.6-torch1.3
- 退出虚拟环境:
conda deactivate
- 删除虚拟环境:
conda remove -n your_env_name --all
3. 安装Pytorch
3.1 确定CUDA版本
查看服务器系统CUDA版本的方法:
cat /usr/local/cuda/version.txt
nvcc -V
只有安装了nvidia-cuda-toolkit才可以使用nvcc -V
。
cat /usr/local/cuda/version.txt
# CUDA Version 10.0.130
系统的CUDA版本必须和Pytorch的CUDA版本对应,比如pytorch0.4最高支持cuda9.2,那么在CUDA10.0以上的系统中不能安装pytorch0.4。
3.2 添加清华镜像源
使用conda默认下载源会出现问题:第三方源太大,下载慢,易中断下载。
- 参照清华大学开源软件镜像站,添加步骤如下:
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/pytorch/
conda config --set show_channel_urls yes
conda config --set show_channel_urls yes
:从channel中安装包时显示channel的url,即显示包的安装来源。
- 查看channel信息:
conda config --show channels
:
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- defaults
- 移除镜像源可以输入:
conda config --remove channels http_ulr(需要删除的channel)
。
3.3 Pytorch安装
根据自己系统的环境,在pytorch官网寻找合适的命令:
conda install pytorch torchvision cudatoolkit=10.0
如需指定版本:conda install pytorch==1.2.0 torchvision==0.4.0 cudatoolkit=9.2 -c pytorch
。
注意:-c pytorch
参数指定了pytorch的安装来源,即为自带的pytorch仓库,因此,将-c pytorch
去掉,安装时会选择清华镜像源作为安装来源。