anaconda安裝及用conda命令行搭建pytorch環境與使用


在Ubuntu系統安裝anaconda:

1.下載后安裝:sudo sh Anaconda3xxxxx-Linux-x86_64.sh

Do you accept the license terms? [yes|no]
yes
Anaconda3 will now be installed into this location:
/xxx/anaconda3
-Press ENTER to confirm the location
-Press CTRL-C to abort the installation
-Or specify a different location below
/usr/local/anaconda3 (自定義anaconda地址)
Do you wish the installer to initialize Anaconda3
in your xxx/.bashrc ? [yes|no]
yes

2.初始化:重啟終端后如果conda命令無法使用,則手動更新bashrc:
sudo gedit ~/.bashrc
在文件末尾添加 export PATH="/usr/local/anaconda3/bin:$PATH"(自己的anaconda地址)后保存
再運行source ~/.bashrc
重啟終端
如果終端語句前沒有(base),運行conda init即可

3.打開anaconda圖形化界面
anaconda-navigator
———————————————————————————————————————————————————————————————

環境搭建

在base下新建一個環境xxx,可在envs文件夾中找到

conda create -n xxx python=3.9

在base下激活環境

conda activate xxx

在xxx中退出環境至base

conda deactivate

通過conda安裝pytorch下載地址
根據需要選擇安裝的版本
顯卡查看lspci | grep -i vga
使用nvidia GPU可以:lspci | grep -i nvidia
Nvidia自帶一個命令行工具可以查看顯存的使用情況以及驅動和cuda版本:nvidia-smi

conda install pytorch torchvision torchaudio -c pytorch

其中-c 是選擇從哪個channe下載,-從pytorch是從pytorch的官網下載
安裝前可先添加channel,如清華的鏡像源,然后去掉-c pytorch,就會從已添加的channel中下載

安裝cudnn
在anaconda環境中使用conda命令安裝cuda、cudnn、tensorflow(-gpu)、pytorch
conda虛擬環境中安裝cuda和cudnn,再也不用頭疼版本號的問題了
cuda和cudnn版本對應:
cuDNN Archive

清華源用這個

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 --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
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/pkgs/free/
conda config --set show_channel_urls yes

中科大源:

conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
conda config --set show_channel_urls yes

然后:
conda install pytorch torchvision torchaudio
即可

delete channel:
conda config --remove-key channels

注意:經測試如果安裝后無法使用cuda,可能是channel下載的pytorch是cpu版本的問題,可以刪除所有的channel重新安裝pytorch

conda常用命令

復制某一個環境
conda create --name yyy --clone xxx

刪除某個環境
conda remove -n xxx --all

刪除某一個包
conda uninstall xxx

查看當前環境中安裝了哪些包

conda list

顯示有哪些環境
conda info -e

終止當前步驟
ctrl+c

在pycharm中調用conda的環境

在選擇解釋器界面選擇conda environment
選擇exist environment,將目標環境文件夾下(anacona/envs/xxx/)的python.exe添加進去

調用完conda環境后,此時pycharm下方的terminal就等同於conda進入該環境后的conda命令行界面
可以使用conda命令,效果是一樣的

學習自bilibili人工智能新手環境搭建指南anaconda+pytorch+pycharm


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM