views:

3190

answers:

13

I get this error:

Can't locate Foo.pm in @INC

Is there an easier way to install it than downloading, untarring, making, etc?

+3  A: 

sudo perl -MCPAN -e 'install Foo'

dreeves
+26  A: 

On Unix:

usually you start cpan in your shell:

# cpan

and type

install Chocolate::Belgian

or in short form:

cpan Chocolate::Belgian

On Windows:

If you're using ActivePerl on Windows, the PPM (Perl Package Manager) has much of the same functionality as CPAN.pm.

Example:

# ppm
ppm> search net-smtp
ppm> install Net-SMTP-Multipart

see How do I install Perl modules? in the CPAN FAQ

Many distributions ship a lot of perl modules as packages.

  • Gentoo: category dev-perl
  • Debian: apt-cache search 'perl$'

You should always prefer them as you benefit from automatic (security) updates and the ease of removal. This can be pretty tricky with the cpan tool itself.

For Gentoo there's a nice tool called g-cpan which builds/installs the module from CPAN and creates a Gentoo package (ebuild) for you.

bene
The short form is just "cpan Chocolate::Belgian" from the command line :)
brian d foy
IIRC the latest ActivePerl no longer has command-line ppm, it now opens a fancy GUI.
Kev
+2  A: 

If you're on Ubuntu and you want to install the pre-packaged perl module (for example, geo::ipfree) try this:

    $ apt-cache search perl geo::ipfree
    libgeo-ipfree-perl - A look up country of ip address Perl module

    $ sudo apt-get install libgeo-ipfree-perl
Otto
A: 

On Windows with the ActiveState distribution of Perl, use the ppm command.

PW
A: 

2 ways that I know of :

USING PPM :

With Windows (ActivePerl) I've used ppm

from the command line type ppm. At the ppm prompt ...

ppm> install foo

or

ppm> search foo

to get a list of foo modules available. Type help for all the commands

USING CPAN :

you can also use CPAN like this (*nix systems) :

perl -MCPAN -e 'shell'

gets you a prompt

cpan>

at the prompt ...

cpan> install foo  (again to install the foo module)

type h to get a list of commands for cpan

JWHEAT
Beat me by 20 secs :-)
Pat
You can also just say "$ cpan Foo::Bar" to install directly from the command line. No fancy one-liner or CPAN shell necessary. :)
brian d foy
A: 

The standard way which should work on Linux, Unix , Strawberry Perl (for windows), etc... is

sudo perl -MCPAN -e 'install Foo'

Or you can start a shell via

sudo perl -MCPAN -e 'shell'

then once the cpan shell has started you type

install Foo

or simply type help for a list of commands.

The first time you run any of these commands you might be required to configure CPAN by answering a few questions.

Most perl distributions provide a cpan shell command so you can start it with cpan instead of perl -MCPAN -e 'shell'

If you are using Active State perl on win32 the easiest way is to install modules using Perl Package Manager provided with the distribution

You do this you type:

ppm install Foo
Pat
You can also just say "$ cpan Foo::Bar" to install directly from the command line. No fancy one-liner or CPAN shell necessary. :)
brian d foy
+12  A: 

I note some folks suggesting one run cpan under sudo. That used to be necessary to install into the system directory, but modern versions of the CPAN shell allow you to configure it to use sudo just for installing. This is much safer, since it means that tests don't run as root.

If you have an old CPAN shell, simply install the new cpan ("install CPAN") and when you reload the shell, it should prompt you to configure these new directives.

Nowadays, when I'm on a system with an old CPAN, the first thing I do is update the shell and set it up to do this so I can do most of my cpan work as a normal user.

Also, I'd strongly suggest that Windows users investigate strawberry Perl. This is a version of Perl that comes packaged with a pre-configured CPAN shell as well as a compiler. It also includes some hard-to-compile Perl modules with their external C library dependencies, notably XML::Parser. This means that you can do the same thing as every other Perl user when it comes to installing modules, and things tend to "just work" a lot more often.

Dave Rolsky
+8  A: 
brian d foy
+4  A: 

Also see Yes, even you can use CPAN. It shows how you can use CPAN without having root or sudo access.

Corion
A: 

Lots of recommendation for CPAN.pm, which is great, but if you're using Perl 5.10 then you've also got access to CPANPLUS.pm which is like CPAN.pm but better.

And, of course, it's available on CPAN for people still using older versions of Perl. Why not try:

$ cpan CPANPLUS
davorg
Heh, that's cute. Use CPAN.pm to replace itself with CPANPLUS. :)
brian d foy
A: 

Otto made a good suggestion. This works for Debian too, as well as any other Debian derivative. The missing piece is what to do when apt-cache search doesn't find something.

$ sudo apt-get install dh-make-perl build-essential apt-file
$ sudo apt-file update

Then whenever you have a random module you wish to install:

$ cd ~/some/path
$ dh-make-perl --build --cpan Some::Random::Module
$ sudo dpkg -i libsome-random-module-perl-0.01-1_i386.deb

This will give you a deb package that you can install to get Some::Random::Module. One of the big benefits here is man pages and sample scripts in addition to the module itself will be placed in your distro's location of choice. If the distro ever comes out with an official package for a newer version of Some::Random::Module, it will automatically be installed when you apt-get upgrade.

mikegrb
A: 

On Fedora you can use

# yum install foo

as long as Fedora has an existing package for the module.

Bruce Alderman
+2  A: 

Try App::cpanminus:

# cpanm Chocolate::Belgian

It's great for just getting stuff installed. It provides none of the more complex functionality of CPAN or CPANPLUS, so it's easy to use, provided you know which module you want to install. If you haven't already got cpanminus, just type:

# cpan App::cpanminus

to install it.

Philip Potter