28

What is the difference between the cpan and cpanm commands?

They both seem to install perl modules, so what is the difference?

CJ7
  • 22,579
  • 65
  • 193
  • 321

2 Answers2

30

cpan the CPAN shell has been shipped with Perl since about 1997. When you run it the first time it asks a bunch of questions and saves the answers in a config file. Then you can install a module by running:

cpan -i Module::Name

The shell provides other commands for searching CPAN and looking inside distribution files.

A project to create a newer, better and more featureful CPAN shell called CPANPLUS (cpanp from the command-line) was started by Jos Boumans, but it was never quite completed to the point where the original vision had been realised.

Meanwhile MIYAGAWA decided that cpanp was trying to do too much and what the world really needed was a simpler shell that did less and asked fewer questions (ideally none at all). He created App::cpanminus which provides the cpanm command and does exactly what he intended. You can use it to install a module (and all the module's dependencies) with a command like:

cpanm Module::Name

The main difference between the two is that if you have Perl you should already have the cpan command. Whereas you won't have cpanm unless/until you install it.

Grant McLean
  • 6,898
  • 1
  • 21
  • 37
  • So which one be used to install modules? Is `cpanm` better because it is newer? – CJ7 Jul 08 '16 at 02:41
  • 10
    Use which ever you prefer - either is fine. I would use `cpan -i App::cpanminus` to install cpanm and then use `cpanm`. I think it's better because it's simpler (certainly not just because it's newer). – Grant McLean Jul 08 '16 at 02:56
  • 1
    @CJ7, Neither actually installs anything. They execute the installer included in the distribution to install. As such, the differences are going to be mostly cosmetic. I don't see the point of going out of my way to install `cpanm` just to avoid pressing Enter once the first time `cpan` is run – ikegami Jul 08 '16 at 03:30
  • 4
    Either tool will download the required file(s) from a CPAN mirror, unpack the distribution and then kick off the make/make install process. You use these tools to install modules from CPAN. To say that "neither actually installs anything" is a silly nitpick that almost seems intended to create confusion. – Grant McLean Jul 08 '16 at 08:11
  • 3
    That's an interesting and informative history, but the OP asked **"What is the difference between cpan and cpanm"**. Your answer came at the end of your post and says *"The main difference between the two is that if you have Perl you should already have the cpan command"*, so they are pretty much *identical* except that you must install `cpanm`. That's not correct and I must -1 – Borodin Jul 08 '16 at 12:34
  • 1
    You can also drop the `-i`: `cpan Module::Name` – ThisSuitIsBlackNot Jul 08 '16 at 15:15
19

Most modern Perl users prefer cpanm for it's simplicity and mainly, brevity of output.

Using cpan can result in hundreds of lines of output as it shows you everything it's doing: downloading, checksum, running installer, parsing, loading, the list goes on. The same applies to every dependency.

cpanm on the other hand tells you what it is doing in a more terse and general way, that is: Fetching, Configuring, Building and testing, Done.

So the main difference in usage is that cpanm suppresses a lot of (typically) irrelevant information. Sometimes when a module fails, you might want more information. There's a -v|--verbose flag that essentially shows you everything cpan would.

Also, some modules that require user input may seem to 'hang' when you try to install them. That's because the output that's asking you to type something is suppressed. You can use the --interactive (or --verbose) flags to work around that.

For a comparison of the output, see slides 35-39 here or slides 37-41 here. (These are from the same presentation. I'm just duplicating the links in case one goes off line.)

Borodin
  • 126,100
  • 9
  • 70
  • 144
elcaro
  • 2,227
  • 13
  • 15
  • 6
    *"Most modern perl users prefer cpanm for it's simplicity and mainly, brevity of output."* That requires a citation and is an unnecessary part of the answer. You are clearly a `cpanm` fan without saying that, and cannot be trusted to give a factual account. *"a more terse and general way"* surely it cannot be both? – Borodin Jul 08 '16 at 12:40
  • "General" as defined by '_considering or including only the main features or elements of something_'. By only stating the main elements of module installation, the output from `cpanm` is much more terse than that of `cpan`. – elcaro Jul 08 '16 at 14:17