首页 > 技术文章 > [Linux] Miniconda安装及其使用

jessepeng 2019-10-16 14:04 原文

集群环境下安装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

推荐阅读