1. 程式人生 > 其它 >Ubuntu安裝anaconda和一些常用的conda命令

Ubuntu安裝anaconda和一些常用的conda命令

time stamp:2022-03-28

author: Weber

實驗環境

  • vmware ubuntu虛擬機器

    $uname -a

    Linux weber-virtual-machine 5.11.0-40-generic #44~20.04.2-Ubuntu SMP Tue Oct 26 18:07:44 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
    

實驗步驟

1. 下載安裝指令碼

$wget -P /tmp https://repo.anaconda.com/archive/Anaconda3-2021.11-Linux-x86_64.sh

如果下載速度過慢,可能是ubuntu沒有換源,下面是給Ubuntu換源的步驟:

  1. 將原先的源進行備份

    sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
    
  2. vim原始檔並新增國內的源

    sudo vim/etc/apt/sources.list
    
    #配置內容(國內源):
    #阿里源
    deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
    
    
    #清華源
    # 預設註釋了原始碼映象以提高 apt update 速度,如有需要可自行取消註釋
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
    
    # 預釋出軟體源,不建議啟用
    # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
    
    #中科大源
    deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
    deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
    deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
    deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
    deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
    deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
    deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
    deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
    deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
    deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
    
    #網易163源
    deb http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse
    deb http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse
    deb http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse
    deb http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse
    deb http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse
    
  3. 更新

    #更新源
    sudo apt-get update
    #更新軟體
    sudo apt-get upgrade
    

2.驗證指令碼資料完整性

$sha256sum /tmp/Anaconda3-2021.11-Linux-x86_64.sh

輸出類似於以下:

$fedf9e340039557f7b5e8a8a86affa9d299f5e9820144bd7b92ae9f7ee08ac60 /tmp/Anaconda3-2021.11-Linux-x86_64.sh

確保上面的命令打印出來的雜湊值和https://docs.anaconda.com/anaconda/install/hashes/lin-3-64/ 頁面對應版本的 Anaconda 雜湊值一樣。

fedf9e340039557f7b5e8a8a86affa9d299f5e9820144bd7b92ae9f7ee08ac60

3.執行指令碼安裝

$bash /tmp/Anaconda3-2021.11-Linux-x86_64.sh

輸出:

Welcome to Anaconda3 2020.02

In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>> 

ENTER繼續。往下滑動閱讀協議,使用ENTER按鍵。一旦你看完協議,你將會被詢問是否接受協議條款:

Do you approve the license terms? [yes|no]

輸入yes接受協議,並且你會被提示選擇安裝路徑:

Anaconda3 will now be installed into this location:
/root/anaconda3

    - Press ENTER to confirm the location
    - Press CTRL-C to abort the installation
    - Or specify a different location below

預設的位置應該對大部分使用者都可直接使用。按Enter確認位置。

安裝過程將會花費一些時間,並且一旦完成,指令碼將會問你是否想要執行conda init。輸入yes

Installation finished.
Do you wish the installer to initialize Anaconda3
by running conda init? [yes|no]

這將會將命令列工具conda新增到系統的PATH環境變數中。

想要啟用 Anaconda,你可以關閉並且重新開啟你的 shell 或者在當前 shell 會話中輸入下面的命令,來重新載入PATH環境變數:

source ~/.bashrc

想要驗證安裝過程,在你的終端輸入conda

3.conda相關命令

建立環境

conda create --name your_env_name

輸入y確認建立。

創建制定python版本的環境

conda create --name your_env_name python=2.7
conda create --name your_env_name python=3
conda create --name your_env_name python=3.5

建立包含某些包的環境

conda create --name your_env_name numpy scipy

建立指定python版本下包含某些包的環境

conda create --name your_env_name python=3.5 numpy scipy

列舉當前所有環境

conda info --envs
conda env list

進入某個環境

activate your_env_name

退出當前環境

deactivate 

複製某個環境

conda create --name new_env_name --clone old_env_name 

刪除某個環境

conda remove --name your_env_name --all