2

Perl's cpan command is a powerful way to manage Perl modules. However, when maintaining modules system-wide under UNIX, Michal Ingeli notes that another possible option is yum install 'perl(PerlModuleName)'. If available, should yum be my first resort in this case?

For example, the command cpanm CGI installs the CGI module under my ~/perl5 directory, which may be best if the CGI module is only needed by scripts run under my account. But this won't provide the CGI module to scripts run by other accounts.

I can use cpanm -l <directory> to force the cpanm command to load modules to a specific directory (e.g., cpanm -l /usr/local CGI to install CGI to /usr/local/lib/perl5), or I can edit ~/cpan/CPAN/MyConfig.pm to change the default install location cpan uses.

But on nearly all systems, multiple Perl system library locations exist (/usr/local/share/perl5, /usr/share/perl5/vendor_perl, /usr/lib64/perl5, etc.), and choosing the correct one is somewhat arbitrary since these are not generated by the cpan command.

With this in mind, should I turn to yum (if available) before cpan for system-wide UNIX Perl module management? It's easy enough to test with a command like:

yum install 'perl(LWP::Simple)'

If yum failed in this instance, I would fall back to:

cpanm -l <directory> LWP::Simple

What do you recommend in this type of case, and why?

(Note that nxadm has answered a more general question about this.)


To summarize answers so far:

  • If at all possible, use the system package manager to update CPAN modules. E.g., for LWP::Simple:

    • yum install 'perl(LWP::Simple)', or

    • apt-get install liblwp-simple-perl

  • If the preceding fails, try to implement a separate Perl environment in which to use CPAN modules not present in the system-wide libraries. Consider local::lib or Perlbrew for this;

  • Only if the above options don't apply, use cpanm -l <directory> to load the module to a system-wide directory.

Community
  • 1
  • 1
CODE-REaD
  • 2,819
  • 3
  • 33
  • 60
  • Yes. On a package managed system if the package manager provides the software you need I would suggest using it before doing anything else. – Etan Reisner Mar 10 '16 at 17:51
  • 4
    You generally shouldn't mix package installs with CPAN installs. If you want to use the system perl, use your package manager; otherwise, it's best to install your own perl and use CPAN. – ThisSuitIsBlackNot Mar 10 '16 at 18:54
  • The same applies to `dpkg` on Debian-based systems (like Ubuntu), though the package naming scheme is different. One issue is that not all CPAN modules are going to be available in your OS's package repo (which is why you need `cpanm` as a fallback). – Keith Thompson Mar 10 '16 at 20:00
  • Leave your vendor Perl alone and check out [Perlbrew](http://perlbrew.pl/) instead. – Matt Jacob Mar 10 '16 at 20:31

2 Answers2

3

I can't speak from experience with RPM/yum systems, but I have done a lot of work with Perl applications on Debian systems and I do highly recommend going with the system packaged versions of CPAN modules if you can. I know a lot of people disagree and historically they may have had good reason but I've been doing it for a long time and find it works very well.

In the Debian world there are an enormous number of Perl modules in pre-packaged form and if you happen to need one that isn't packaged you can build your own package with dh-make-perl and put it in your local apt repository. Being able to run apt-get install your-application and have it pull in all the required dependencies is a real time saver when your code is moving through Dev -> Staging/UAT -> Production workflows. It also gives you confidence that the version of a particular module you're deploying to production is the same as the one you tested in UAT.

One thing you absolutely should not do is use cpanm or the cpan shell as root to install modules into the system directories. If you decide to install direct from CPAN, then use local::lib to install the modules in an application-specific lib directory.

[Edit] Some sample commands as requested:

On a Debian-based system, you would first install the dh-make-perl tool:

sudo apt-get-install dh-make-perl

Then to download a package from CPAN and build it into a .deb file you would run a command like this*:

dh-make-perl --build --cpan Algorithm::CouponCode

You could install the resulting .deb file with:

sudo dpkg -i libalgorithm-couponcode-perl_1.005-1_all.deb

Managing your own apt repository is a whole other topic. In my case I'd copy the .deb to an appropriate directory on the local apt server and run a script to update the index (I think our script uses dpkg-scanpackages).

Note in my opening paragraph above I recommend using systems packages "if you can". To be clear, I meant in the case where most of the modules you want are already packaged by Debain. The example above did not build packages for any dependencies. If your app involves installing modules which have long dependency chains that are not in Debian already, then using cpanm and local::lib will simplify the install. But then you shoulder the burden of repeating that as your code advances through staging to production servers. And you may need to use cpanfile or carton to make sure you're getting the same versions at each step.

* one gotcha: if you have previously set up local::lib so that cpan installs go into a private directory (e.g.: /home/user/perl5) then that will affect the pathnames used in the .deb produced by dh-make-perl. To avoid that, run this before dh-make-perl:

unset PERL5LIB PERL_LOCAL_LIB_ROOT PERL_MB_OPT PERL_MM_OPT
Grant McLean
  • 6,898
  • 1
  • 21
  • 37
  • Grant, to clarify, to install system-wide a CPAN module not found by `apt-get`, do you recommend I download and build my own and add it to a local repository rather than rely on `cpanm` to find and install it? Also, would you please give an example of the command(s) you use to accomplish this under Debian? – CODE-REaD Mar 11 '16 at 14:57
0

Your system's perl was put there for your system's use. The folks that maintain your distribution will update it when they see fit to another version that suits the needs of your system. Using your system's Package Manager to manage it is really your best idea.

Feel free to use it, but if you need a different version, for whatever reason, you are best rolling your own into a separate location. When maintaining your own perl install, use CPAN.

tjd
  • 4,064
  • 1
  • 24
  • 34
  • I don't entirely agree with *"put there for your system's use"*. Yes, the system does use Perl and Perl modules, but ultimately all those packages are made available for everyone to use. I do agree that you shouldn't mess with the files provided by the distribution packages. – Grant McLean Mar 11 '16 at 19:17
  • @GrantMcLean "made available for everyone to use" So long as we're willing to use the generally out of date version of perl that comes with the distro, sure! Updating it in place is dangerous. This leaves the option of a locally installed "production" perl. Perhaps I'm just a control freak... – tjd Mar 16 '16 at 15:09