Fast Perl module installation with cpm
Tags: perl, cpan, conferences, dependencies
Published: 2021-09-28 14:21:00

---
title: Fast Perl module installation with cpm
published: true
date: 2021-09-28 14:00:00 UTC
tags: Perl,CPAN,conferences,dependencies
canonical_url: https://phoenixtrap.com/2021/09/28/fast-perl-module-installation-with-cpm/
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ilsx3wsln9w6pd42eix.jpeg
---

One of Perl’s cardinal strengths is the depth and variety of add-on modules to extend its capabilities, collected for [the past 26 years](https://groups.google.com/g/comp.lang.perl.announce/c/1qlLHdviZlY/m/hU90ubJHrf4J?pli=1) and counting on [CPAN, the Comprehensive Perl Archive Network](https://www.cpan.org/). Starting with [version 5.004](https://perldoc.perl.org/perl5004delta) in 1997, Perl has come packaged with a module (also [called CPAN](https://perldoc.perl.org/CPAN)) and associated command-line client for downloading and installing from this service. Some developers favor alternative tools such as [CPANPLUS](https://metacpan.org/pod/CPANPLUS) and its `cpanp` command or [cpanminus](https://metacpan.org/pod/App::cpanminus) and its `cpanm`, or tools built on the latter such as [Carton](https://metacpan.org/pod/Carton) and [Carmel](https://metacpan.org/pod/Carmel).

My favorite of these over the past several years has been Shoichi Kaji’s [`cpm`](https://metacpan.org/pod/App::cpm), mainly because it’s blazingly fast. As an example, the documentation [cites](https://metacpan.org/pod/App::cpm#HOW-FAST?) an installation of [Plack](https://plackperl.org/), the Perl web application toolkit, as taking three times as long using `cpanm` versus `cpm`. Both use the same [Menlo](https://metacpan.org/pod/Menlo::Legacy) core code but `cpm` achieves its speed by breaking down dependencies into individual streams, installing modules in parallel, and synchronizing the necessary worker processes.

[Shoichi’s presentation](https://youtu.be/CdpBV8BgPI4) from [The Perl Conference](https://perlconference.us/) 2016 provides a great summary:

{% youtube CdpBV8BgPI4 %}

It’s very important to note that **`cpm` is not a drop-in replacement** for the `cpan` or `cpanm` command-line tools. Firstly, it uses the subcommand `install`, e.g., `cpm install Module::Name`. Also, by default, it installs modules into a subdirectory named `local/` as if you specified `cpanm --local-lib-contained local`. You might want this if you’re setting up a Perl project with its non-core dependencies in a separate location addressed by the [local::lib](https://metacpan.org/pod/local::lib) module; otherwise, you should use `cpm install --global` to install into a directory in Perl’s [`@INC` array](https://perldoc.perl.org/variables/@INC). I tend to do the latter when developing, declaring my project’s dependencies in a `cpanfile`.

Speaking of `cpanfile`s, like `cpanm --installdeps` `cpm` will use a `cpanfile` to drive project dependency installation. In fact, it defaults to looking for one if you don’t specify individual modules on the command line and supports the version-controlled `cpanfile.snapshot` file [introduced by Carton](https://metacpan.org/pod/Carton#DESCRIPTION) for tracking exact dependencies used by your project. This is great for repeatedly building [Docker](https://www.docker.com/) containers and `cpm` makes that process even faster.

Although speed is its most important feature, `cpm` has a couple more tricks up its sleeve like installing from a [Git](https://git-scm.com/) repository or self-hosted “DarkPAN.” Check out its [included tutorial](https://metacpan.org/pod/App::cpm::Tutorial).