[Linux] Miniconda安裝及其使用


集群環境下安裝conda進行軟件管理。MinicondaAnaconda的簡化版,對於一般需求而言就夠用了。因此,我這里安裝Minconda3進行軟件安裝管理。

安裝

Miniconda下載地址,版本根據所需選擇下載。

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
sh Miniconda3-latest-Linux-x86_64.sh
#后續根據提示選擇安裝路徑或添加到環境變量即可

也可手動添加到環境變量:

vi  ~/.bashrc
export PATH=/path/to/software/conda/conda3/bin/:$PATH

這里提示一下:有的人說最好不要將conda加入到環境變量中,因為會污染到非conda安裝的其他軟件,造成不可預知的錯誤Anaconda is a snake. ,這個問題確實要注意,尤其是之前已經配置好很多軟件的情況下。最好是創建一個特定的環境,每次用時激活使用。不加入環境變量怎么辦?那就只能設置別名來簡化輸入了。

alias conda='/path/to/software/conda/conda3/bin/conda'
alias actative='source /path/to/software/conda/conda3/bin/activate'
alias deactative='source /path/to/software/conda/conda3/bin/deactivate'

實際上,我在安裝的過程時碰到了問題,不選擇加入環境變量,第一次雖然可以成功激活環境,但退出終端后再次進入環境時,怎么也激活不了。這意味着我必要將conda加入環境變量,而且是在安裝的時候就加入,否則無法激活環境,就很尷尬了,我不知道別人是否也是如此。

cd miniconda/envs #進入特定環境目錄下激活該環境
source ../bin/activate test  #no repsonse

驗證下是否成功:

conda -h

報如下錯誤:

Fatal Python error: Py_Initialize: Unable to get the locale encoding
  File "/share/app/python-2.7.10/lib/python2.7/encodings/__init__.py", line 123
    raise CodecRegistryError,\
                            ^
SyntaxError: invalid syntax

Current thread 0x00007f160e638740 (most recent call first):

說明和系統自帶的python2有沖突,我環境變量中的python庫設置如下:

export PATH=/my/python/software/Python-2.7.15/bin:$PATH
export PYTHONPATH=/my/python/software/Python-2.7.15/lib/python2.7/site-packages:/share/app/python-2.7.10/lib/python2.7/site-packages:/share/app/python-2.7.10/lib/python2.7/:/mygroup/python/software/python/python2.7_lib

可以看到我目前使用的是我自己安裝的python2.7.15,而PYTHONPATH變量中(可能有些設置為PYTHONHOME)包括我的python庫,我小組的python庫以及系統自帶的python庫。根據報錯信息,我將系統自帶庫去掉,再source下環境,可以正常顯示了,我們可以順便看下這些參數的用法。

$ conda -h
usage: conda [-h] [-V] command ...

conda is a tool for managing and deploying applications, environments and packages.

Options:

positional arguments:
  command
    clean        Remove unused packages and caches.
    config       Modify configuration values in .condarc. This is modeled
                 after the git config command. Writes to the user .condarc
                 file (/home/pengjianxiang/.condarc) by default.
    create       Create a new conda environment from a list of specified
                 packages.
    help         Displays a list of available conda commands and their help
                 strings.
    info         Display information about current conda install.
    install      Installs a list of packages into a specified conda
                 environment.
    list         List linked packages in a conda environment.
    package      Low-level conda package utility. (EXPERIMENTAL)
    remove       Remove a list of packages from a specified conda environment.
    uninstall    Alias for conda remove. See conda remove --help.
    search       Search for packages and display associated information. The
                 input is a MatchSpec, a query language for conda packages.
                 See examples below.
    update       Updates conda packages to the latest compatible version. This
                 command accepts a list of package names and updates them to
                 the latest versions that are compatible with all other
                 packages in the environment. Conda attempts to install the
                 newest versions of the requested packages. To accomplish
                 this, it may update some packages that are already installed,
                 or install additional packages. To prevent existing packages
                 from updating, use the --no-update-deps option. This may
                 force conda to install older versions of the requested
                 packages, and it does not prevent additional dependency
                 packages from being installed. If you wish to skip dependency
                 checking altogether, use the '--force' option. This may
                 result in an environment with incompatible packages, so this
                 option must be used with great caution.
    upgrade      Alias for conda update. See conda update --help.

optional arguments:
  -h, --help     Show this help message and exit.
  -V, --version  Show the conda version number and exit.

conda commands available from other packages:
  env

解決上述報錯的另一個方法就是用unset PYTHONPATH,即不用環境中python,然后再使用conda也是正常的。

需要注意的是,conda絕大部分命令都是要求在聯網的情況下使用的,如果你們單位的集群禁止聯網,那這個工具幾乎沒用。不過一般再嚴格也會有單獨一個節點來聯網的,我們下個軟件也不會占用太多資源。

添加頻道

  • 頻道
#官方channel:
conda config --add channels bioconda
conda config --add channels conda-forge

#清華鏡像:
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/bioconda/
  • 查看頻道
#顯示安裝的頻道
 conda config --set show_channel_urls yes 
#查看已經添加的頻道
conda config --get channels
vim ~/.condarc

環境管理

  • 查看環境
# 查看已安裝的python環境
conda info -e  #conda info --envs
conda env list
#當前環境會通過一個星號 (*) 標識
  • 創建環境
#基於python3.6版本創建一個名字為test的python獨立環境
conda create --name test python=3.6 

#指定python2版本
conda create -n test2 python=2

#指定環境路徑
conda create --prefix=/path/to/py36 python=3.6 #注-p/--prefix和-n/--name參數不能同時用

#如果不指定python,安裝會默認為conda自帶的python版本,即如果安裝的是conda2,就是python2,如果是conda3,就是python3.
#最好是每個環境指定python,尤其是和自己使用的保持一致
  • 啟動或關閉環境
#激活環境
source activate(后接環境名,不加默認為base)
source activate test
#退出環境
source deactivate test
#PS:若未加入環境變量,需進入conda的bin目錄下執行
  • 刪除環境
conda env remove -n test
conda remove -n test --all
  • 重命名環境
即先克隆,再刪除
conda create -n python2 --clone py2
conda remove -n py2 --all

軟件安裝

或使用以下命令

conda search bwa
#一般要安裝特定版本時才搜索
  • 安裝相關命令
#安裝指定版本
conda install bwa=0.7.17(其他版本將會覆蓋)
#指定環境安裝
conda install bwa -n test  #加-c還可指定頻道
#指定軟件安裝位置
conda install bwa --prefix=/path/to/env/name  #安裝后可添加環境變量中或使用全路徑
#查看安裝位置
which bwa #一般而言,軟件會安裝到環境中的bin下;而包或庫會安裝在conda/lib/pythonx.x/site-packages
#已安裝軟件
conda list
#更新軟件
conda update bwa
#卸載
conda remove bwa

主要命令回顧

source activate
source deactivate
conda env list
conda create python=2 -n 
conda -h
conda --version
conda list # 查看已經安裝的包
conda update conda # 升級conda自身
conda update python #更新到最新版本的python
conda search  # 查詢包
conda install  # 安裝包
conda install -n test  # 將包安裝到指定環境
conda env remove -n #刪除環境
conda update --all # 更新所有包
conda update  # 升級包
conda remove  # 移除包
conda remove -n test # 從指定環境中移除包

溫馨提示

對於miniconda的使用,我的建議是如果你是全新的系統,本來就沒配置什么東西,那么它或許是個可供選擇的好工具。但如果已經配置了一系列軟件和環境,而你又必須在miniconda加入環境變量的前提下才能激活虛擬環境來使用,那么不用也罷,因為我們無法預知它帶來什么樣的災難。

補充
后來我安裝了Anaconda3,並安裝的過程選擇了初始化(加入環境變量?),需要全路徑調用conda命令,能成功激活虛擬環境,不知道選擇不初始化能否成功激活,反正我的環境又發生了變化,鬼知道我經歷了什么。Anaconda的命令和Miniconda有些不同,看來簡化版還是有其缺陷的。

Ref: https://www.jianshu.com/p/edaa744ea47d
https://www.jianshu.com/p/e1d6276ac0c3
https://www.jianshu.com/p/17288627b994
https://vip.biotrainee.com/d/494-conda


免責聲明!

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



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