各个平台下 Perl 模块安装总结

作者:史提芬先森
原文:https://www.yuque.com/shenweiyan/cookbook/how-to-install-perl-modules

从 1994 年 10 月 17 日发布的 Perl 5.000 起,Perl 增加了模块的概念,用来提供面向对象编程的能力。这是 Perl 语言发展史上的一个里程碑。此后,广大自由软件爱好者开发了大量功能强大、构思精巧的 Perl 模块,极大地扩展了 Perl 语言的功能。

CPAN,Comprehensive Perl Archive Network(https://www.cpan.org/) 是 Perl 模块最大的集散地,包含了现今公布的几乎所有的 perl 模块。CPAN 从 1995 年 10 月 26 日开始创建,截止 2019 年 4 月该网站已经囊括了超过 13,750 位作者编写的 180,202 个 Perl 模块,其镜像分布在全球在 257 台服务器上。

Perl 作为生物信息数据预处理、文本处理和格式转换中的一把瑞士军刀,其强大和重要性不言而喻。今天,我们在这里主要介绍一下各种平台下 perl 模块的安装方法。以安装 Bio::SeqIO 模块为例。

一、Linux 下安装 Perl 模块

Linux/Unix下安装Perl模块有两种方法:手工安装和自动安装。
第一种方法是从 CPAN 上下载您需要的模块,手工编译、安装。第二种方法是使用 CPAN 模块自动完成下载、编译、安装的全过程。

1.1 手工安装

# 从 CPAN(https://metacpan.org/)下载了 BioPerl 模块 1.7.5 版的压缩文件 BioPerl-1.7.5.tar.gz
# 假设放在/usr/local/src/下。
$ cd /usr/local/src

# 解压缩这个文件,这时会新建一个 BioPerl-1.7.5 的目录。
$ tar xvzf BioPerl-1.7.5.tar.gz

# 换到解压后的目录
$ cd BioPerl-1.7.5

# 生成 makefile,如果要指定模块的安装路径,需要添加 PREFIX=/path/to/install 路径
$ perl Makefile.PL (PREFIX=/home/shenweiyan/perl_modules)

# 生成模块
$ make

# 测试模块(这步可有可无):
$ make test

# 如果测试结果报告"all test ok",您就可以放心地安装编译好的模块了。
# 安装模块前,先要确保您对 perl5 安装目录有可写权限(通常以 su 命令获得),执行:
$ make install

# 现在,试试 Bio::SeqIO 模块吧。如果下面的命令没有给出任何输出,那就没问题。
$ perl -MBio::SeqIO -e1

# 如果指定 PREFIX 安装路径,模块安装完成后,需要把模块安装的新路径添加到 PERL5LIB 环境变量
$ export PERL5LIB=$PERL5LIB:/home/shenweiyan/perl_modules/lib/site_perl  //或者把该行内容添加到 ~/.bashrc
bioperl-install.png
bioperl-install.png

上述步骤适合于 Linux/Unix 下绝大多数的 Perl 模块。可能还有少数模块的安装方法略有差别,所以最好先看看安装目录里的 README 或 INSTALL。

有的时候如果是 Build.PL 的需要以下安装步骤:(需要 Module::Build 模块支持)

$ perl Build.PL
$ ./Build
$ ./Build test
$ ./Build install

1.2 自动安装

Linux/Unix 下自动安装 Perl 模块主要有两种方法,一是利用 perl -MCPAN -e 'install 模块' 安装;二是直接使用 cpan 的命令执行安装。这两种方法都是通过与 CPAN 进行交互,然后执行对应模块的自动安装,本质上都是一样的。需要先安装 CPAN 模块。

root@ecs-steven 16:07:59 /tmp
$ perl -MCPAN -e shell
Can't locate CPAN.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .).
BEGIN failed--compilation aborted.

[root@ecs-steven ~]# perl -MCPAN -e shell

CPAN.pm requires configuration, but most of it can be done automatically.
If you answer 'no' below, you will enter an interactive dialog for each
configuration option instead.

Would you like to configure as much as possible automatically? [yes] ^C
[root@ecs-steven CPAN-2.26]# perl -MCPAN -e shell
Sorry, we have to rerun the configuration dialog for CPAN.pm due to
some missing parameters. Configuration will be written to
 <</root/.cpan/CPAN/MyConfig.pm>>


CPAN.pm requires configuration, but most of it can be done automatically.
If you answer 'no' below, you will enter an interactive dialog for each
configuration option instead.

# 输入yes会自动配置,输入no会让手动的配置一些参数。这里我选择了yes,配置好后的配置文件存放在 ~/.cpan/CPAN/MyConfig.pm
Would you like to configure as much as possible automatically? [yes]   #直接回车


Autoconfiguration complete.

commit: wrote '/root/.cpan/CPAN/MyConfig.pm'

You can re-run configuration any time with 'o conf init' in the CPAN shell
Terminal does not support AddHistory.

To fix that, maybe try>  install Term::ReadLine::Perl


cpan shell -- CPAN exploration and modules installation (v2.26)
Enter 'h' for help.

cpan[1]>

除了 cpan 以外,Perl 的模块安装还可以通过 cpanm 命令进行安装,而要获取 cpanm 命令我们需要先安装 Perl 的 App::cpanminus 模块:

[root@ecs-steven data]# cpan
Loading internal logger. Log::Log4perl recommended for better logging
Terminal does not support AddHistory.

To fix that, maybe try>  install Term::ReadLine::Perl


cpan shell -- CPAN exploration and modules installation (v2.26)
Enter 'h' for help.

cpan[14]> install App::cpanminus
Running install for module 'App::cpanminus'
Fetching with HTTP::Tiny:
http://www.cpan.org/authors/id/M/MI/MIYAGAWA/App-cpanminus-1.7044.tar.gz

  CPAN: checksum security checks disabled because Digest::SHA not installed.
  Please consider installing the Digest::SHA module.

Scanning cache /root/.cpan/build for sizes

......

App::cpanminus 模块安装完成后,cpanm 命令就会自动安装到对应 Perl 所在的目录(如果用的是 /bin/perl,cpanm 将默认安装到 /usr/local/bin/cpanm)。

1.2.1 使用 CPAN 模块自动安装方法一

  • 安装前需要先联上网,有无 root 权限均可。对于非 root 用户,Perl 提供了 'local::lib'、'sudo' or 'manual' 三种使用 CPAN 模块自动安装的方式,初次运行 CPAN 时需要用户自行进行选择设置,默认为 'local::lib'。
  • 执行 perl -MCPAN -e shell 命令,运行 CPAN,进入 cpan 交互模式。
  • 初次运行 CPAN 时需要做一些设置,如果我们的机器是直接与因特网相联,那么一路回车就行了。
  • 常用 cpan 命令:
# 获得帮助
cpan> help

# 列出 CPAN 上所有模块的列表
cpan>m

# 安装模块,自动完成 Bio::SeqIO 模块从下载到安装的全过程。
cpan> install Bio::SeqIO

# 查看当前 CPAN 使用的镜像站点
cpan[3]> o conf urllist

# 查看当前 CPAN 的配置信息
cpan[2]> o conf

# 退出
cpan> quit

1.2.2 使用CPAN模块自动安装方法二

cpan -i 模块名

例如:cpan -i CGI

1.2.3 修改CPAN mirror的默认源

安装 perl 下的很多模块文件时,比较快捷的方法是使用 cpan 工具。默认 cpan shell 使用的是 cpan.org 的源,在国内使用的话速度会非常的慢。如果更换为国内的如阿里或网易等公司的源的话,速度会明显提高。修改方法如下:

  • 执行 cpan 命令,或者通过 perl -MCPAN -e shell 命令进入 CPAN 交互模式查看当前源配置
cpan[1]> o conf
$CPAN::Config options from /root/.cpan/CPAN/MyConfig.pm:
    commit             [Commit changes to disk]
    defaults           [Reload defaults from disk]
    help               [Short help about 'o conf' usage]
    init               [Interactive setting of all options]

    applypatch         []
    auto_commit        [0]
    build_cache        [100]
    build_dir          [/root/.cpan/build]
    build_dir_reuse    [0]
    ......
cpan[2]> o conf urllist
    urllist
        0 [http://www.cpan.org/]
Type 'o conf' to view all configuration items
  • 增加源或移出源并提交
cpan[2]> o conf urllist
    urllist
        0 [http://www.cpan.org/]
Type 'o conf' to view all configuration items


cpan[3]> o conf urllist push https:////mirrors.aliyun.com/CPAN/
Please use 'o conf commit' to make the config permanent!


cpan[4]> o conf commit
commit: wrote '/root/.cpan/CPAN/MyConfig.pm'

cpan[5]> o conf urllist
    urllist
        0 [http://www.cpan.org/]
        1 [https://mirrors.aliyun.com/CPAN/]
Type 'o conf' to view all configuration items

移出一个源或者多个源可以使用 pop 函数,如下:

cpan[6]> o conf urllist pop https://mirrors.aliyun.com/CPAN/

cpan[3]> o conf urllist pop http://www.cpan.org/ https://mirrors.aliyun.com/CPAN/

也可以一次增加多个源:

cpan[4]> o conf urllist ftp://mirrors.sohu.com/CPAN/ http://mirrors.163.com/cpan/
  • 或者直接修改 ~/.cpan/CPAN/MyConfig.pm 文件的 urllist
$ grep urllist /home/steven/.cpan/CPAN/MyConfig.pm
  'urllist' => [q[http://www.cpan.org/], q[http://mirrors.aliyun.com/CPAN/]],

$ cpan
Loading internal logger. Log::Log4perl recommended for better logging
Terminal does not support AddHistory.

To fix that, maybe try>  install Term::ReadLine::Perl


cpan shell -- CPAN exploration and modules installation (v2.26)
Enter 'h' for help.

cpan[1]> o conf urllist
    urllist
        0 [http://www.cpan.org/]
        1 [http://mirrors.aliyun.com/CPAN/]
Type 'o conf' to view all configuration items
2.png
2.png
  • 如果对当前的当前所有的 cpan shell 的配置不满意,也可以通过命令重新初始化该文件,如下:
cpan> o conf init

1.2.4 使用 cpanm 命令安装

<br />参数名直接为模块名称即可。如:

$ sudo cpanm CGI::Session
$ sudo cpanm Template

注: 如果不是 root 权限,cpanm 也一样能用。它会将模块下载安装到用户的根目录(~)下。

为了加快 cpanm 下载速度, 可以指定使用镜像. 并只从镜像下载. 如下:

 $ sudo cpanm --mirror http://mirrors.163.com/cpan --mirror-only CGI::Session

二、windows 下 Perl 模块安装

在开始介绍 windows 下 Perl 模块安装前,我们先来了解一些关于 make、gmake、nmake、dmake 等编译工具。

Make 是一种自动化构建工具,用于从源码自动生成可执行程序和库(通常借助于 makefile 来推导出目标程序);Make 有多个版本,如 Unix/Linux下 的 GNU Make(之所以有 gmake,是因为在别的平台上 make 一般被占用,GNU make 只好叫 gmake 了)、Windows 下的 Microsoft nmake 等。dmake 则是一个适用于 Linux、Solaris、Win32 以及其他平台的跨平台的命令行编译工具,除此之外,跨平台编译工具中还有一个大名鼎鼎的 cmake。

关于 Make 的详细介绍,具体可参考:Wikipedia: Make (Software)

dmake 工具:https://metacpan.org/release/dmake,具体安装方法请自行谷歌(dmake 貌似是 cpan 环境配置好就有了在 "C:\Perl\site\bin" 下)。

nmake 是 Microsoft Visual Studio 中的附带命令,需要安装 VS;安装 VS 后,nmake.exe 出现在 C:\Program Files (x86)\Microsoft Visual Studio 9\VC\bin 目录下(bin 文件夹的位置根据每个人系统不同,找到自己的安装位置,bin 文件夹中包含 nmake 即可)。如果 VS 安装过程中,没有注册 VS 的环境变量,因此直接在 cmd 的 DOC 中输入 nmake 就会出现:"'nmake' 不是内部或者外部命令,也不是可运行的程序或批处理文件" 报错。

使用 nmake 的方法有两种:

  1. 打开 VS 自带的命令行 "Visual Studio 2008 Command Prompt",然后在该命令行中使用 nmake;
  2. 将路径 "C:\Program Files (x86)\Microsoft Visual Studio 9\VC\bin" 设置到环境变量中去,然后在 DOS 命令行中使用 nmake。

2.1 解压编译手动安装

windows 下手动 Perl 模块方法跟 Linux类似。都需要解压后执行:

perl makefile.pl 
nmake/dmake 
nmake/dmake test
nmake/dmake install

2.2 cpan 自动安装

安装前需要对 cpan 配置,cpan需要安装其他的模块 dmakeMinGw gcc compiler。完成后,进入 cpan 交互界面安装(参考上面的 linux cpan 自动安装)。

2.3 PPM 自动安装

如果使用 ActivePerl,可以使用 PPM(Perl Package Manager) 来安装,使用 PPM GUI 或 PPM Commandline。PPM commandline 实例如下:

  1. add correct repositories.
c:\perl\bin\ppm repo add http://theoryx5.uwinnipeg.ca/ppms/package.lst
c:\perl\bin\ppm repo add http://www.roth.net/perl/packages/

通过设置不同的 repositories 可以加速部分 Perl 模块的下载,我们也可以通过 ppm 命令查看可用的 repositories list:

# Use the command ppm repo list to list all installed PPM repositories in ActivePerl 5.10 through 5.14.
PS C:\> ppm repo list
┌────┬───────┬────────────────────────────────┐
│ id │ pkgs  │ name                           │
├────┼───────┼────────────────────────────────┤
│  1 │ 13334 │ ActiveState Package Repository │
│  2 │   294 │ Theoryx                        │
└────┴───────┴────────────────────────────────┘
 (2 enabled repositories)
PS C:\>

# The command ppm repo desc #, where "#" is the number of the repository (in the order it was added), will display information about that repository:
PS C:\> ppm repo desc 2
Id: 2
Name: Theoryx
URL: http://cpan.uwinnipeg.ca/PPMPackages/14xx/
Enabled: yes
Last-Status: 200 OK
Last-Access: 9 seconds ago
Refresh-In: 1 day
  1. install the packages
c:\perl\bin\ppm install Carp-Assert

c:\perl\bin\ppm install Log-Log4perl

c:\perl\bin\ppm install YAML-Syck

更多关于 ActivePerl 的文档与资料,可以参考:http://docs.activestate.com/

三、参考资料

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 156,423评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,339评论 1 289
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,241评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,503评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,824评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,262评论 1 207
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,615评论 2 309
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,337评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,989评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,300评论 2 240
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,829评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,193评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,753评论 3 230
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,970评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,708评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,295评论 2 267
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,207评论 2 258

推荐阅读更多精彩内容

  • 时间2018-05-04 一、安装环境 操作系统Windows10 二、 安装过程中需要使用的软件 1. MySQ...
    skybirth阅读 2,769评论 3 2
  • 2016-10-20 号更新。 源文件可以在 github 或 perl6.org上找到. General Rak...
    焉知非鱼阅读 938评论 0 0
  • Perl 哲学 Perl 是一种能“干实事”的语言。它灵活、宽容、可塑。在一名编程能者的手中,它可以 完成几乎所有...
    firefive阅读 1,306评论 1 11
  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 5,618评论 0 10
  • 今天是立夏,友人说:“下雨了!"我关掉音乐,仔细聆听,嘀嗒嘀嗒的雨声滴落在雨棚上。夜已静,而我却无眠。 那...
    448dc009f227阅读 239评论 2 0