使用conda(Miniconda或Anaconda)的话,就不用自己手动安装Python了。
安装Pytorch在文章最后。
-
下载安装:Anaconda3(对应于Python3) linux_x86_x64,并根据教程安装
安装教程 https://www.dazhuanlan.com/xiaoranishere/topics/1483582
两种安装方式:
-
直接安装:
yay -Sy miniconda3
, 会自动选择适合你系统的版本- (Miniconda3-py39_4.9.2-Linux-x86_64.sh, 58.6M, 2020-12-22)。
- 我这里使用yay是默认安装到了
/opt/miniconda3
-
手动下载安装:
-
Miniconda清华源 https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/
选择
Miniconda3-py3X_最新版本号-Linux-x86_64.sh
-
安装:
sh Miniconda3*.sh
,同意协议,选择位置,默认安装到$HOME/miniconda3
,继续。
-
-
-
安装后配置
-
安装完之后,如果输入conda找不到命令,需要
source ~/.bashrc
,如果不行就终端运行如下命令添加内容到.bashrc然后重新source:echo "[ -f /opt/miniconda3/etc/profile.d/conda.sh ] && source /opt/miniconda3/etc/profile.d/conda.sh" >> ~/.bashrc
查看conda版本:
conda -V
-
配置conda源,国内源或公司内部源:
教程 https://mirror.tuna.tsinghua.edu.cn/help/anaconda/
编辑
~/.condarc
文件,添加如下内容:# 我的添加,注意顺序,越靠上优先级越高 channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/ - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/fastai/ - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/ - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/ - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ - defaults # conda官方的 show_channel_urls: true
然后:
-
运行
conda clean -i
清除索引缓存,保证用的是镜像站提供的索引。 -
运行
conda create -n myenv numpy
测试一下吧。
或者运行命令逐条添加():
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main * 这样是添加到文件的`channes:`下 * 最后运行的config, 将会被插入到最上面,优先级最高
conda config --set show_channel_urls yes
注:
# bash添加 官方channel 到 ~/.condarc: conda config --add channels bioconda conda config --add channels conda-forge # bash添加 中科大channel 到 ~/.condarc conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/ conda config --set show_channel_urls yes # 下面是清华官网提供的添加样式,手动放到~/.condarc里面 channels: - defaults show_channel_urls: true default_channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2 custom_channels: msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud # for MinGW64 pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
-
-
-
查看命令:
conda env list
# 当前已创建的所有环境,当前环境会有*标记which numpy
orwhich numpy
# 查看软件或包的安装位置- 一般软件安装到环境的bin下
- 包或库安装在conda/lib/pythonx.x/site-packages/下
conda list
# 查看已安装软件
-
创建一个项目环境
conda create -n NAME python=3.7
# 很多包都要求Python 3.7-n
表示–nameNAME
是你起的这个环境的名字python=3.7
指定这个环境的版本- 创建的环境默认在:
~/.conda/envs/python37
-
激活所需的虚拟环境
conda activate ysy
# 激活指定环境,之后,终端之前出现(ysy)
提示ysy
是环境的名字- 老版本的Anaconda使用的不是conda, 而是source, 但心版本之后conda更好,
- 执行速度快
- 不会和其他管理器冲突
conda activate base
# 激活默认环境, 然后终端之前出现(base)
提示- 注:base中安装的包并不能被其他环境使用,base只是一个默认环境
-
新建激活环境之后,重启vscode即可左下角选择该环境。
-
退出某个conda虚拟环境:
conda deactivate
-
安装需要的包
- 强烈推荐使用
pip
安装包,而不是conda
,因为:pip
包更全,conda
经常找不到包而使命令中断(比如tflearn)pip
会使用之前的下载缓存,而conda
被打断之后每次重新运行install都要重新下载conda list
能够看到pip
安装的包,也能用conda
命令管理,pip
命令也能管理之前用conda安装的包。
激活环境后,使用conda在此环境中安装包:
-
conda install numpy
# 安装某个包 -
conda install numpy=1.9
# 安装某个指定版本
的包 -
conda install pytorch torchvision torchaudio cudatoolkit=10.2
# 同时安装多个包 -
conda install numpy -n NAME
# 安装到指定环境 -
conda install numpy --prefix=/path/to/envName
# 指定安装路径- 路径下需要包含conda环境
-
conda install mingw libpython -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
# 指定channel安装-c
等价于--channel
-
也可以使用pip安装,但可能出现一些问题(conda 4.0.6以上已经开始测试解决了),但还是尽可能:
- 参考 (2018) https://www.anaconda.com/blog/using-pip-in-a-conda-environment
- 尽量在conda创建的独立环境中使用pip,而不要在外面以root方式使用
- 尽量使用conda安装,除非conda安装不了,再尝试pip
- 最好可以将pip/conda安装的包分别储存在requirements.txt文件中
- 强烈推荐使用
-
升级conda
conda update -n base conda
-
更新软件/包
conda update numpy
conda update numpy=1.9.3
# 更新到指定版本
-
卸载软件/包
conda remove numpy
-
重命名某个conda虚拟环境
可以通过clone and remove old来实现重命名 -
conda create -n newName --clone oldName
-
conda remove -n oldName --all
# 删除原来名称的全部环境
-
安装Pytorch
由于GPU版的Pytorch还需要同时安装cudatoolkit, 所以最好使用conda而不是pip安装。通常的安装原则,去pytorch官网确定版本(必须要指定版本,否则默认是cpu版本)
— https://pytorch.org/get-started/locally/— https://pytorch.org/get-started/previous-versions/
版本的选择需要看你的显卡型号 算力
— https://developer.nvidia.com/zh-cn/cuda-gpus提供两个版本的pytorch安装命令:
-
pytorch 1.8.0, cuda10.2
conda install pytorch==1.8.0 torchvision==0.9.0 torchaudio==0.8.0 cudatoolkit=10.2
-
pytorch LTS (1.8.2), cuda11.1, 仅支持Python <= 3.8
conda install pytorch torchvision torchaudio cudatoolkit=11.1 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch-lts/
这个命令虽然指定了清华源,但下载速度还是很慢,可以把比较大的软件包离线下载下来,然后安装本地包。
- 查看包的下载地址:使用conda下载的时候断网,就会打印出下载地址,然后下载器下载就行,linux下载的是个
.tar.bz2
压缩包 - 通过conda安装本地包:
conda install --use-local xxx.tar.bz2
,回车之后可能需要等一会儿才有相应,等着就好。
- 查看包的下载地址:使用conda下载的时候断网,就会打印出下载地址,然后下载器下载就行,linux下载的是个
-