CPAN on OS X

CPAN, the Comprehensive Perl Archive Network is a free community provided library of Perl programs.

Installing CPAN

Here are instructions for upgrading 64-bit CPAN, tested April 2014 on 10.9.2 Mavericks,
July 2015 on 10.10.4 Yosemite,
May 2016 on 10.11.5 El Capitan,
July 2016 on 10.12.6 Sierra.
and March 2018 on 10.13.3 High Sierra.

Prerequisites

Interaction with MacPorts on Sierra and El Capitan

If you install modules from Macports, it will install Perl 5.26.2 (as of 06/09/18), newer than the system-provided 5.18. Before installing CPAN modules, you need to change your startup environment to

    export PERL5LIB=/Users/thvv/bin:/opt/local/lib/perl5/5.26
    export PERL_LOCAL_LIB_ROOT="/opt/local/lib/perl5/5.26"
    export PERL_MB_OPT="--install_base "/opt/local/lib/perl5/5.26""
    export PERL_MM_OPT="INSTALL_BASE=/opt/local/lib/perl5/5.26"

Then replace /usr/bin/perl with a link to /opt/local/bin/perl so that Perl files that begin with #/usr/bin/perl will find the correct Perl. To do this, you need to disable System Integrity Protection (SIP):

When Apple releases an update to the OS, it sometimes includes an updated version of Perl... usually still way older than the version needed to build various ports. After the restart from installing the OS update, various Perl programs will not work, because /usr/local/bin/perl has changed versions to one that doesn't match your PERL5LIB. Repeat the steps just above to reset your link to the MacPorts version of Perl if you see messages like

  /Users/tomvanvleck/bin/expandfile: Perl lib version (5.26.2) doesn't match executable
  '/usr/bin/perl' version (5.18.2) at /opt/local/lib/perl5/5.26/darwin-thread-multi-2level/Config.pm line 62.
  Compilation failed in require at /opt/local/lib/perl5/5.26/darwin-thread-multi-2level/DynaLoader.pm line 22.
  BEGIN failed--compilation aborted at /opt/local/lib/perl5/5.26/darwin-thread-multi-2level/DynaLoader.pm line 22.

Install Process

After checking my environment, I did sudo -H cpan. CPAN started and asked if I wanted it to set as many default options as possible. I said yes, and it appeared to work. Then CPAN asked if it should choose the best mirror sites for me. Say NO and set them yourself.. Executing r worked, and showed many modules out of date.

I was then able to install modules from the command line. I did install CPAN and reload cpan to upgrade CPAN to the latest version, and this worked.

Next I tried install LWP and this failed with the message "Can't locate object method 'requirements_for_module'". This was a symptom of not setting PERL5LIB. I fixed that and things worked... see below.

To install DBD::mysql, first start the MySQL server. If you have already set the root password for MySQL, do o conf makepl_arg "--testuser=root --testpass=xxxxxxxx", before doing install DBD::mysql and o conf makepl_arg "" afterward.

Installing on High Sierra and Sierra

Macports installed Perl 5.24, newer than the system-provided 5.18. Later installs have upgraded it to 5.26.2. To use a later Perl, you need to relink /usr/bin/perl to the new Perl version, because of Perl files beginning #!/usr/bin/perl. In Sierra and later, you have to temporarily disable System Integrity Protection (SIP) to change /usr/bin. See above.

(Gotcha) Perl 5.22 and later emit complaints about some regular expressions that 5.18 accepted happily. I had to change /{{..../ to /\{\{..../ to put a backslash in front of open braces. This is backward compatible.

Installing on El Capitan

(Another gotcha) CPAN on El Capitan failed installing DBD::mysql with the message

  # Failed test 'dbh insert id 0 == max(id) 2 in dbd_mysql_t31'
  # at t/31insertid.t line 58.
  # got: 0
  # expected: 2
  # Looks like you failed 1 test of 18.

I had to do force install DBD::mysql and that seemed to work.

Installing on Yosemite

Installing OSX 10.10.5 broke my Perl setup. I had set PERL5LIB to $HOME/bin:/opt/local/lib/perl5/site_perl/5.16.3, but 10.10.5 installed Perl 5.18 as the system Perl. Access to MySQL from Perl started failing, saying that the 5.16 library was incompatible with the 5.18 Perl loader. I set PERL5LIB to $HOME/bin:/Library/Perl/5.18/, trying to avoid using the MacPorts Perl, and made a symlink to the new system Perl in my /bin directory with ln -s /usr/local/bin/perl. Then I did sudo -H /usr/bin/cpan to install a bunch of stuff into the system Perl lib, such as DBI. CPAN still failed installing DBD::mysql, even after setting the CPAN config using o conf makepl_arg "--testuser=aaa --testpass=bbb". I searched the Web, which suggested sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib and this allowed me to install DBD::mysql and run Perl MySQL database accesses. (This problem did not occur on later releases.)

Installing on OSX Snow Leopard and Mavericks

When I executed install DBD::mysql on Snow Leopard, the install failed, saying it could not find libmysqlclient.18.dylib. (This problem did not occur on Mountain Lion, but it did on Mavericks.) I Googled and found http://probably.co.uk/problems-installing-dbdmysql-on-os-x-snow-leopard.html . It suggested

 sudo install_name_tool -id /usr/local/mysql/lib/libmysqlclient.18.dylib\
   /usr/local/mysql/lib/libmysqlclient.18.dylib

I did this and re-tried install DBD::mysql, and it succeeded. This problem did not occur on Yosemite and El Capitan.

Home | FAQ © 2010-2018, Tom Van Vleck updated 2018-08-14 13:56