Perl beginner

How to Install Perl Modules on Windows (CPAN, cpanm, Strawberry Perl, ActiveState, local::lib)

Question

how to install perl modules in windows

How to Install Perl Modules in Windows

Difficulty: Beginner (with some intermediate sections)

Tags: Perl, Windows, CPAN, cpanm, Strawberry Perl, ActiveState, local::lib, XS, toolchain

Overview

In Perl, “modules” are reusable libraries distributed primarily through CPAN (the Comprehensive Perl Archive Network). On Windows, installing modules is usually straightforward, but the exact workflow depends on which Perl distribution you installed and whether the module is “pure Perl” (no compiled code) or includes XS (compiled C/C++ bindings).

This guide covers the main approaches on Windows: cpanm (recommended), the built-in CPAN client, and Windows distribution-specific package managers (notably ActiveState). It also covers best practices (project isolation, version pinning), common pitfalls (compilers, PATH, permissions), and practical real-world workflows.

Step 1: Choose a Perl Distribution (this matters on Windows)

Your Perl distribution determines whether you already have build tools and which module installer is easiest.

  • Strawberry Perl (recommended for most Windows users): ships with a MinGW toolchain (gcc, make, etc.). This makes installing XS modules from CPAN much more likely to succeed.
  • ActiveState Perl: historically offered curated, prebuilt packages and tooling. This can be convenient in locked-down corporate environments, but CPAN installs may differ from “standard” community workflows.
  • MSYS2/MinGW-based environments: sometimes used by developers who already rely on MSYS2. Viable, but more moving parts.

If you want the most “CPAN-native” experience on Windows—especially for modules that need compilation—Strawberry Perl is the usual best default.

Step 2: Confirm Perl Works and Is on PATH

Open Command Prompt or PowerShell and check:

perl -v
perl -e "print $^X, \"\n\""

If Windows says “perl is not recognized…”, you need to add Perl’s bin directory to your PATH (the installer usually offers a checkbox like “Add Perl to PATH”).

Recommended Installer: cpanm (App::cpanminus)

cpanm (“cpanminus”) is the most common recommendation because it’s simpler and more automation-friendly than the interactive CPAN shell.

Install cpanm

Many setups already include it; if not, install it using CPAN:

cpan App::cpanminus

After that, you should have the cpanm command:

cpanm --version

Install a module with cpanm

Example: install Text::CSV:

cpanm Text::CSV

Verify it installed:

perl -MText::CSV -e "print $Text::CSV::VERSION, \"\n\""

Common cpanm options (Windows-friendly)

  • cpanm --notest Module::Name — skips tests (use sparingly; useful when a module’s tests fail on your machine but you accept the risk).
  • cpanm -v Module::Name — verbose logs for troubleshooting.
  • cpanm --mirror ... --mirror-only Module::Name — use a specific CPAN mirror (helpful behind proxies or with slow networks).
  • cpanm --installdeps . — install dependencies for a project in the current directory (commonly used with cpanfile).

Alternative: the CPAN Shell (built in)

Perl includes the CPAN module, which can configure itself and install modules. It’s powerful but more interactive.

Start it:

cpan

First run usually asks configuration questions (download mirrors, build tools, etc.). Then you can do:

cpan> install Text::CSV

The CPAN shell is fine for manual installs, but for scripts/CI, cpanm is typically easier.

Install Modules Without Admin Rights (Best Practice on Windows): local::lib

On Windows, installing to the system-wide Perl directories can fail if you don’t have Administrator privileges. A common best practice is to install modules into a per-user directory using local::lib.

One-time setup

cpanm local::lib

Then initialize it (choose a directory; a common one is C:\Users\YOURNAME\perl5):

perl -Mlocal::lib

That command prints environment variable settings you should add to your PowerShell profile or system environment. After you set them, installs go into your user area, not the global Perl.

Project-isolated installs (very useful)

For a single project, you can keep a local module directory inside the project folder:

cpanm --local-lib-contained .\local Text::CSV

Then run scripts with that local library enabled:

perl -I .\local\lib\perl5 your_script.pl

Understanding XS Modules on Windows (Compilers and “make”)

Some CPAN modules include XS code that must be compiled. On Windows, compilation is the #1 source of installation pain. Typical symptoms include errors about missing gcc, make, nmake, headers, or linkers.

  • Strawberry Perl: usually includes a working gcc/make toolchain, so XS builds often work out of the box.
  • Other Perls: may require you to install a compatible C toolchain or rely on prebuilt binaries.

If a module fails to compile, check the build output carefully. Sometimes the fix is installing a missing dependency (e.g., a system library), but on Windows it’s often easier to choose a distribution that provides build tools.

ActiveState Perl: Package-Managed Installs

If you are using ActiveState Perl, you may have a vendor-provided workflow (often centered around curated packages). This can reduce compilation issues by preferring prebuilt artifacts. In corporate environments, this approach can be more predictable than building from source.

The tradeoff is that your available modules/versions may be constrained by what your platform provides, and instructions from general CPAN guides might not map 1:1.

Real-World Workflows

1) One-off scripts (automation, admin tasks)

You find a script that needs DBI and Text::CSV. A pragmatic approach:

cpanm DBI Text::CSV

If you don’t have admin rights, use local::lib or --local-lib-contained.

2) Team projects (pin dependencies)

For a production app, you usually want repeatable installs. Common patterns include:

  • A cpanfile listing dependencies, installed via cpanm --installdeps .
  • A vendored local lib directory (e.g., local/) built by CI and shipped with the app, or rebuilt as part of deploy.

This reduces “works on my machine” problems caused by different module versions.

3) Packaging for distribution

If you need to ship a Perl tool to Windows machines that won’t install CPAN modules at runtime, consider packaging approaches (for example, bundling modules and an embedded Perl). Packaging is a deeper topic, but the key idea is: production deployment often avoids installing from CPAN on the target machine.

Common Pitfalls (and fixes)

  • Installing to protected directories: If installs fail with permission errors, use local::lib or run the shell as Administrator (the former is usually safer).
  • PATH confusion: Multiple Perl installations (Strawberry + ActiveState + Git for Windows’ perl, etc.) can cause you to install modules into one Perl but run another. Always check perl -e "print $^X" and where perl.
  • XS compilation errors: Use Strawberry Perl for an easier toolchain. If you must use another Perl, ensure a compatible compiler setup.
  • Corporate proxy / SSL interception: CPAN downloads may fail. Configure proxy environment variables and/or select a mirror.
  • Spaces and long paths: Some older build scripts can choke on deep paths. Installing Perl in a simple path (and keeping project paths short) helps.
  • Anti-virus locking build directories: Builds unpack to temp directories; real-time scanning can cause mysterious failures. If you see intermittent “file in use” issues, try excluding the build/temp directories.

Best Practices Checklist

  • Prefer Strawberry Perl on Windows for broad CPAN compatibility.
  • Use cpanm for simple, scriptable installs.
  • Install per-user with local::lib instead of system-wide when possible.
  • Pin dependencies for production (use a cpanfile and install deps consistently).
  • Verify the Perl you’re using before/after install: where perl, perl -e "print $^X".
  • Keep logs for failed installs: rerun with cpanm -v.

Runnable Perl Code Examples

Example 1: Verify Perl and the CPAN client are available (no extra modules required)

use strict;
use warnings;

print "Perl OK\n";

my $has_cpan = eval {
  require CPAN;
  1;
};

print $has_cpan ? "CPAN OK\n" : "CPAN missing\n";

Expected output:

Perl OK
CPAN OK

Example 2: Use an installed module (DateTime) after installing with cpanm

Install first:

cpanm DateTime

Then run:

use strict;
use warnings;
use DateTime;

my $dt = DateTime->new(
  year      => 2026,
  month     => 1,
  day       => 1,
  hour      => 12,
  minute    => 34,
  second    => 56,
  time_zone => 'UTC',
);

print $dt->ymd . " " . $dt->hms . " " . $dt->time_zone_short_name . "\n";

Expected output:

2026-01-01 12:34:56 UTC

Example 3: Install and use Text::CSV (common real-world parsing module)

Install first:

cpanm Text::CSV

Then run:

use strict;
use warnings;
use Text::CSV;

my $csv = Text::CSV->new({ binary => 1, auto_diag => 1 });

my $input = "name,age\nAlice,30\nBob,41\n";
open my $fh, "<", \$input or die "open: $!";

# Read header
my $header = $csv->getline($fh);

while (my $row = $csv->getline($fh)) {
  my ($name, $age) = @$row;
  print "$name is $age\n";
}

Expected output:

Alice is 30
Bob is 41

Summary

To install Perl modules on Windows reliably: install a Windows-friendly Perl distribution (often Strawberry Perl), use cpanm for day-to-day module installs, and prefer per-user or per-project installs via local::lib to avoid permission problems. When modules fail, the most common causes are multiple Perls on PATH, missing compiler toolchains for XS modules, and network/proxy issues.

Verified Code

Executed in a sandbox to capture real output. • 158ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Perl OK
CPAN OK
STDERR
(empty)

Was this helpful?

Related Questions