anaconda 使用的一些體驗與困惑

anaconda 使用的一些體驗與困惑

1. channels 使用

需要注意的是做生信分析的童鞋使用 conda 環境時一定要特別注意 conda channels 的設置,濫用 channels 很有可能會導致你的軟件升降級(甚至環境)錯亂。推薦設置如下(~/.condarc):

<code>channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda
  - 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/pro
  - defaults
show_channels_urls: true/<code>
  • mirrors.tuna.tsinghua.edu.cn/anaconda 是清華大學提供了一個 conda 的鏡像;
  • mirrors.ustc.edu.cn/anaconda 是中科大 conda 鏡像,有需要的也可以使用;
  • 生信軟件 channel:mirrors.tu na.tsing hua.edu.cn/ana conda/cloud/bioc onda;

從 2019.04 起清華大學和中科大宣佈停止 Anaconda 鏡像服務,但是出於教育科研機構使用的前提,在 2019-05-15 清華大學又宣佈重新恢復了 Anaconda 鏡像!

因此原來使用國內鏡像的 conda 可以根據自身需求決定是否需要變更新的 channels:

<code>channels:
  - conda-forge
  - bioconda
  - main
  - free
  - r
  - pro
  - defaults
show_channels_urls: true/<code>

2. 軟件安裝使用

conda 環境下的軟件儘量使用 conda、pip 命令去安裝。但同時也產生了一個問題,即 conda 中安裝了 R,有些使用了 install.packages(),install_github,biocLite 等方式安裝的 R 包,在環境遷移的時候,這些包如何遷移?

3. 環境激活

conda 4.6.x 切換環境使用的是:

<code>$ source activate bio-base/<code>

conda 4.7.x 後切換環境變成了:

<code># To activate this environment, use:
> conda activate bio-base

# To deactivate an active environment, use:
> conda deactivate/<code>

問題是,conda-4.7.x 使用推薦的命令切換環境,還要你 init 初始化一下 conda,不想 init 的話可以用回 4.6.x 的方式切換環境:

<code>$ conda --versionconda 4.7.5$ conda activate bio-baseCommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.To initialize your shell, run    $ conda init <shell>Currently supported shells are:  - bash  - fish  - tcsh  - xonsh  - zsh  - powershellSee 'conda init --help' for more information and options.IMPORTANT: You may need to close and restart your shell after running 'conda init'.$ source activate bio-base(bio-base) shenweiyan@ecs-steven 10:49:59 /home/shenweiyan$ which python/Bioinfo/Pipeline/SoftWare/Anaconda3/envs/bio-base/bin/python$ source deactivate bio-baseDeprecationWarning: 'source deactivate' is deprecated. Use 'conda deactivate'.shenweiyan@ecs-steven 11:03:40 /home/shenweiyan$ source activate bio-base(bio-base) shenweiyan@ecs-steven 11:03:50 /home/shenweiyan$ conda deactivate(bio-base) shenweiyan@ecs-steven  11:03:57 /home/shenweiyan$/<shell>/<code> 

4. 環境導出與恢復

使用 conda 命令安裝的包,都可以使用下面的命令導出依賴包/環境並批量恢復:

<code># To create a requirements.txt file
# Gives you list of packages used for the environment
conda list 
# Save all the info about packages to your folder
conda list -e > requirements.txt 

# To export environment file
activate <environment-name>
conda env export > <environment-name>.yml
# For other person to use the environment
conda env create -f <environment-name>.yml
# Install via `conda` directly.
# This will fail to install all dependencies. If one fails, all dependencies will fail to install.
conda install --yes --file requirements.txt
# To go around issue above, one can iterate over all lines in the requirements.txt file.
while read requirement; do conda install --yes $requirement; done /<environment-name>/<environment-name>/<environment-name>/<code>

5. R 與 R 包安裝

R Essentials 軟件包包含 IRKernel 和 80 多種最流行的數據科學 R 軟件包,包括 dplyr,shiny,ggplot2,tidyr,caret 和 nnet 等。

5.1 biocLite

Bioconductor 鏡像使用幫助:

  • Bioconductor 鏡像源配置文件之一是 .Rprofile (linux 下位於 ~/.Rprofile )。在文末添加如下語句:
<code>#清華大學開源鏡像
options(BioC_mirror="https://mirrors.tuna.tsinghua.edu.cn/bioconductor")/<code>
  • 也可以在安裝過程中指定鏡像:
<code>source("http://bioconductor.org/biocLite.R")
#指定一個離你最近的國內鏡像
options(BioC_mirror="http://mirrors.ustc.edu.cn/bioc/")
biocLite("包名")/<code>

biocLite 包批量安裝:

<code>> source("http://bioconductor.org/biocLite.R")
> options(BioC_mirror="http://mirrors.tuna.tsinghua.edu.cn/bioconductor")
> data = read.table("r-biocLite.txt",  header=T, check.names=F, quote="")
> head(data)
  biocLite_Packages_Name
1                RSQLite
2               KEGGREST
3                    png
4                   Rcpp
5                 digest
6          AnnotationDbi
> soft = as.vector(data[,1])
> biocLite(soft)/<code>

5.2 install.packages

install.packages()所有 R 包:

<code>> data = read.table("r-packages.txt",  header=T, check.names=F, quote="")
> soft = as.vector(data[,1])
> install.packages(soft)/<code>

6. 特別注意的軟件

6.1 gcc

對於使用

conda install --yes --file requirements.txt 重裝某一個環境的所有軟件時,如果軟件中包含了 gcc,安裝了 R 後,在使用 R 時會可能會引發錯誤:

<code>/path/to/SoftWare/Anaconda/v2/lib/R/bin/exec/R: /path/to/SoftWare/Anaconda/v2/lib/R/bin/exec/../../lib/../../libgomp.so.1: version `GOMP_4.0' not found (required by /path/to/SoftWare/Anaconda/v2/lib/R/bin/exec/../../lib/libR.so)/<code>

6.2 glibc

glibc 是 GNU 發佈的 libc 庫,即 c 運行庫。glibc 是 linux 系統中最底層的 api,幾乎其它任何運行庫都會依賴於 glibc。glibc 除了封裝 linux 操作系統所提供的系統服務外,它本身也提供了許多其它一些必要功能服務的實現。由於 glibc 囊括了幾乎所有的 UNIX 通行的標準,可以想見其內容包羅萬象。而就像其他的 UNIX 系統一樣,其內含的檔案群分散於系統的樹狀目錄結構中,像一個支架一般撐起整個作業系統。在 GNU/Linux 系統中,其 C 函式庫發展史點出了 GNU/Linux 演進的幾個重要里程碑,用 glibc 作為系統的 C 函式庫,是 GNU/Linux 演進的一個重要里程碑。

有一些軟件對於 glibc 的版本會有要求,如 cnvnator-0.3.3 要求 glibc >= 2.14。雖然在 anaconda 中有很多 channel 都提供了 glibc,但千萬注意一定要注意不要輕易去安裝,否則有很大的概率會導致整個環境掛掉,甚至會導致當前環境中的 conda、python 出現動態庫混亂錯誤,恢復起來相當麻煩!

我在《一次"膽戰心驚"的真實集群運維經歷》記錄了 gblic 的一些集群吐血經歷,感興趣的可以瞭解一下。

7. 什麼時候使用 Anaconda

對於 Anaconda(conda) 軟件安裝以及依賴解決的原理,我對這個黑盒子表示一頭霧水。真實的情況是,如果在一個環境中安裝了幾百個軟件(包),再去新裝軟件,這時候 Anaconda 常常會卡在 Collecting package metadata 和 Solving environment 過程中,甚至一個晚上都沒法解決環境的依賴。

anaconda 使用的一些體驗與困惑

conda 官方說他們在 conda-4.7 中花了很多的精力去提升了 conda 的速度(參考官方文章:《How We Made Conda Faster in 4.7》),但從 4.6 升級到 4.7 過程很容易導致環境崩潰,修復起來極其麻煩(反正我折騰了一個晚上都沒能把我的 base 給恢復回來,吐血的經歷)!

anaconda 使用的一些體驗與困惑

anaconda 使用的一些體驗與困惑

對於新手而言,Anaconda 的確是非常簡單易用,如果對於多用戶的服務器端,或者是提供公共使用的軟件庫是否需要採用 Anaconda,個人覺得的確需要慎重考慮一下,最起碼需要考慮:

  • 是否需要根據流程劃分環境?如果每個流程都需要使用 Python+R+Perl,每個環境都安裝這三個軟件(包),如何避免空間浪費?
  • 需要考慮如何備份和恢復環境。萬一某個環境崩潰,有什麼快速替代的方案而不影響業務。

或許還有更多的問題,這裡沒有列出來,如果大家有什麼更好的想法或者建議,也歡迎留言交流。

anaconda 使用的一些體驗與困惑


分享到:


相關文章: