How to Install Perl Modules on Mac OS X in 4 Easy Steps
Today at work, I learned how to install Perl modules using CPAN. It’s a lot easier than I thought.
You see, for the past couple of years, I’ve been a bit frustrated because OS X does not come with a whole lot of Perl modules pre-installed, and for all I googled, I couldn’t find an “idiot’s” guide for moderately-savvy-but-not-expert users like myself to install modules and dependencies on demand.
The only instructions I could find point to Fink, which basically installs modules in a path that isn’t included in the Perl @INC variable (which I still haven’t figured out how to update), meaning you have to manually specify the full path to the modules in every script — which is not a lot of fun if you’re developing on OS X and deploying on Red Hat, for instance.
Moreover, Fink doesn’t seem to make every module available, and it’s not very easy to determine which Fink package you need to install if you need a particular module.
So, with a script that called on several apparently unavailable modules, and a deadline looming, I finally decided to suck it up and figure out how to use CPAN to install them:
1) Make sure you have the Apple Developer Tools installed.
These are on one of your install discs, or available as a huge but free download from the Apple Developer Connection [free registration required]. I thought I had them, but apparently when we upgraded that computer to Tiger, they went missing.
If you don’t have this stuff installed, your installation will fail with errors about unavailable commands.
2) Configure CPAN.
$ sudo perl -MCPAN -e shell
perl> o conf init
This will prompt you for some settings. You can accept the defaults for almost everything (just hit “return”). The two things you must fill in are the path to make (which should be /usr/bin/make) and your choice of CPAN mirrors (which you actually choose don’t really matter, but it won’t let you finish until you select at least one). If you use a proxy or a very restrictive firewall, you may have to configure those settings as well.
If you skip Step 2, you may get errors about make being unavailable.
3) Upgrade CPAN
$ sudo perl -MCPAN -e 'install Bundle::CPAN'
Don’t forget the sudo, or it’ll fail with permissions errors, probably when doing something relatively unimportant like installing man files.
This will spend a long time downloading, testing, and compiling various files and dependencies. Bear with it. It will prompt you a few times about dependencies. You probably want to enter “yes”. I agreed to everything it asked me, and everything turned out fine. YMMV of course. If everything installs properly, it’ll give you an “OK” at the end.
4) Install your modules. For each module….
$ sudo perl -MCPAN -e 'install Bundle::Name'
or
$ sudo perl -MCPAN -e 'install Module::Name'
This will install the module and its dependencies. Nice, eh? Again, don’t forget the sudo.
The first time you run this after upgrading CPAN, it may prompt you to configure again (see Step 2). If you accept its offer to try to configure itself automatically, it may just run through everything without a problem.
There are a couple of potential pitfalls with specific modules (such as the LWP::UserAgent / HEAD issue), but most have workarounds, and I haven’t run into anything that wasn’t easily recoverable.
And that’s it!
Did you find this useful? Is there anything I missed?
COMMENT: by Ken, January 15th, 2008
thank you!
similarly, i bet it would be also helpful for some people to see how to install perl packages with FC8 using yum…say you want to install the Perl Frontier::Client package (and its dependencies)…
$ su root
# yum -y install perl-Frontier-RPC
…all you do is append a ‘perl-’ to the package name and substitute the ‘::’ for a ‘-’ and you should be all set…
COMMENT: by Alex, March 14th, 2008
This gave me the courage to get to grips with CPAN
Very helpful, thanks!
COMMENT: by jpd, March 14th, 2008
Thanks. On Leopard there were some diffs but nothing too bad…The only thing I would say for newbies is that you don’t type sudo perl -MCPAN -e ‘install Module::Name’ – Module & Name are different depending what module you are loading. If you want the LWP module for web manipulation use sudo perl -MCPAN -e ‘install Bundle::lwp’
Hope that helps
COMMENT: by Noemi Millman, March 16th, 2008
Thanks, jpd. Would you mind sharing the differences you encountered on Leopard?
COMMENT: by Albert, March 18th, 2008
Excellent information. I have just got it working on Leopard. Cheers, Albert
COMMENT: by Michele, July 7th, 2008
Very helpful. I installed on Leopard without too much trouble. Only problem I ran into is that some of the standard config’d locations is not where my programs were located, and didn’t really feel like doing a system-wide grep. Google helped me locate them, and the install went great after that.
COMMENT: by Phoenix2Life, September 26th, 2008
Extremely well written and perfectly working CPAN guide I have come across. It has since long I always used to download RPMs/TAR.GZs and used to install/configure Perl on my linux boxes. This tutorial has helped me to setup nice automated way which I have been looking for. Thanks.
COMMENT: by nobighair, January 14th, 2009
Thanks for these instructions, certainly got me up and running.
I had a list of modules to install. Plus there were a couple I needed to force install. So I found it easier to split it up:
> sudo perl -MCPAN -eshell
To get the CPAN shell. Then in the shell:
> install XML::Writer
or
> force install XML::Writer
Cheers
COMMENT: by Guizard Sébastien, May 5th, 2009
hello,
I had stop the process at the step 2 when you have to enter the adress of the miror cpan(for searching this adress).
Now, when I restart the command, it don’t ask me for the Cpan miror and the command make was not created. What can I do ? It’s my first macbook, I’ve bought it 3 day ago. I don’t know what can I do ! i’m thinking to re instal Mac OS X. I don’t it’s good idea ! if you can help me I will be very glad ! ! !
PS : I’m sory if my english is not very good, I’m learning right now in USA ^^
COMMENT: by Noemi Millman, May 6th, 2009
Guizard, have you tried simply running through Step 2 (above) again?
COMMENT: by Mac, June 20th, 2009
Now how do you get back to a regular shell to run scripts?
COMMENT: by Noemi Millman, June 23rd, 2009
Mac: try typing “exit” or “quit”
COMMENT: by Nick, July 30th, 2009
thank you so much! i needed this
COMMENT: by Simon, November 17th, 2009
Thanks so much! This is exactly what I was looking for.
COMMENT: by nod, January 4th, 2010
Thanks muchly! This was very helpful and I’m so glad I came across it. New to Mac and there is a lot to get used to.
COMMENT: by Christian, June 8th, 2010
I am on 10.6.3
I followed these instructions and everything went well until I tried to install a module. Maybe I just misread the post, but instead of ‘install Module::Name’ I had to use ‘install Name’
COMMENT: by Noemi Millman, June 8th, 2010
Christian, you don’t use the word “Module” — it varies depending on what specifically you’re trying to install. See JPD’s notes above.
COMMENT: by Christian, June 8th, 2010
Right, but if I wanted to install the DateTime module, I would use ‘install DateTime’ and not ‘install Module::DateTime’. That was just unclear to me from the post.
COMMENT: by JM, August 31st, 2010
Thanks for your tutorial. It was very helpful, and enabled me to just installed seven PERL mods, as well as the Expat C library from Sourceforge.
Like Christian though, I only had to use “sudo cpan ModuleName” at the Terminal prompt to install most of them. I think there was only one where I had to prefix the command with “Bundle::”.
This is on a 27″ iMac running Snow Leopard 10.6.4.
I also installed them on a ten-year-old G4 mini-tower running Tiger.
YMMV
Thanks again!