15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started
21.10.2024

Perl Module Installation: A Complete Technical Guide

Perl modules are self-contained, reusable packages of Perl code stored in files with the .pm extension, designed to extend the core language with pre-built functionality for tasks ranging from HTTP requests and database access to XML parsing and cryptography. Installing them correctly — whether via CPAN, cpanm, or manual build — is a foundational skill for any Perl developer or systems administrator.

This guide covers every installation method in depth, including non-root environments, dependency resolution, version pinning, and post-install verification — the details that most tutorials skip entirely.

What Are Perl Modules and Why They Matter

A Perl module is a namespace-scoped package that exports functions, variables, or object-oriented interfaces into your script. Modules live in the @INC search path and are loaded at compile time with use or at runtime with require. The distinction matters: use Module is evaluated before your script runs, meaning a missing module causes an immediate fatal error rather than a runtime surprise.

The Comprehensive Perl Archive Network (CPAN) hosts over 200,000 module distributions authored by thousands of contributors. Every production Perl environment — whether running on a bare-metal server, a VPS, or a shared environment — depends on a reliable module installation workflow.

Method 1: Installing Perl Modules via the CPAN Shell

The built-in CPAN client ships with every standard Perl installation. It handles dependency resolution, module fetching, building, testing, and installation automatically.

First-Time CPAN Configuration

On a fresh system, the first invocation of the CPAN shell triggers an interactive configuration wizard. To bypass it and accept sensible defaults automatically:

perl -MCPAN -e 'CPAN::Shell->install("CPAN")'

Or launch the shell directly:

perl -MCPAN -e shell

Inside the shell, install any module by name:

cpan[1]> install LWP::Simple
cpan[2]> install DBI

One-Line Non-Interactive Installation

For scripted deployments or CI pipelines, skip the shell entirely:

perl -MCPAN -e 'install("LWP::Simple")'

Critical edge case: If CPAN prompts for configuration during a non-interactive run (common in Docker containers or minimal OS images), force auto-configuration first:

perl -MCPAN -e 'my $c = CPAN::HandleConfig->load; CPAN::Shell->install("LWP::Simple")'

Or set the environment variable before running:

PERL_MM_USE_DEFAULT=1 perl -MCPAN -e 'install("LWP::Simple")'

Updating the CPAN Client Itself

An outdated CPAN client is a frequent source of TLS handshake failures and broken dependency graphs. Update it before installing anything else on a legacy system:

cpan CPAN

Method 2: cpanm (CPAN Minus) — The Preferred Production Tool

cpanm is a zero-configuration, dependency-aware CPAN client that is significantly faster than the full CPAN shell. It produces cleaner output, handles most dependency chains silently, and integrates cleanly with local::lib and perlbrew. For any serious deployment workflow, cpanm is the correct default choice.

Installing cpanm

curl -L https://cpanmin.us | perl - --sudo App::cpanminus

If curl is unavailable:

wget -O - https://cpanmin.us | perl - --sudo App::cpanminus

On systems where you already have a working CPAN client:

cpan App::cpanminus

Installing Modules with cpanm

cpanm LWP::Simple
cpanm DBI
cpanm Moose

Installing a Specific Version

Version pinning is essential for reproducible builds. To install an exact release:

cpanm GAAS/libwww-perl-6.67.tar.gz

Or use the @version syntax:

cpanm LWP::Simple@6.67

Installing from a cpanfile

For project-level dependency management, define your requirements in a cpanfile:

requires 'LWP::Simple', '>= 6.00';
requires 'DBI', '>= 1.643';
requires 'Moose';

Then install all declared dependencies in one command:

cpanm --installdeps .

This is the Perl equivalent of pip install -r requirements.txt or npm install, and it belongs in every production deployment script.

Method 3: Manual Installation from Source Tarball

Manual installation is necessary when working in air-gapped environments, when a module is not yet on CPAN, or when you need to apply patches before building.

Step 1: Download the Tarball

Fetch the distribution archive from search.cpan.org or metacpan.org, or transfer it manually to your server:

wget https://cpan.metacpan.org/authors/id/G/GA/GAAS/libwww-perl-6.67.tar.gz

Step 2: Extract the Archive

tar -xzf libwww-perl-6.67.tar.gz
cd libwww-perl-6.67

Step 3: Build Using Makefile.PL or Build.PL

Most CPAN distributions use one of two build systems. Check which one is present before proceeding.

For Makefile.PL (ExtUtils::MakeMaker):

perl Makefile.PL
make
make test
sudo make install

For Build.PL (Module::Build):

perl Build.PL
./Build
./Build test
sudo ./Build install

What each step does:

  • perl Makefile.PL — Interrogates your Perl installation and system libraries to generate a platform-specific Makefile.
  • make — Compiles any XS (C extension) code and stages the pure-Perl files.
  • make test — Runs the distribution's test suite against the built files. Never skip this step on production systems; a failing test suite is a hard signal that something is wrong with your environment or the module itself.
  • sudo make install — Copies the built files into the system Perl library path.

Pitfall: If make test reports failures, do not proceed to make install. Investigate the test output in the t/ directory. Many failures are caused by missing optional dependencies or environment-specific configuration, not actual bugs.

Method 4: local::lib for Non-Root and Shared Environments

On shared hosting, managed servers, or any environment where you lack sudo access, local::lib redirects all module installations to a directory inside your home folder. This is the correct architectural approach — not a workaround.

Setting Up local::lib

If you have any CPAN access at all:

cpanm --local-lib=~/perl5 local::lib

Or via the CPAN shell:

perl -MCPAN -e 'install("local::lib")'

Activating the Environment

Add the following to your ~/.bashrc or ~/.bash_profile:

eval "$(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib=$HOME/perl5)"

Reload your shell:

source ~/.bashrc

This sets PERL5LIB, PERL_LOCAL_LIB_ROOT, PERL_MB_OPT, PERL_MM_OPT, and PATH so that both perl and cpanm automatically use your local library directory.

Installing Modules into local::lib

After activation, all subsequent cpanm calls install to ~/perl5 without any additional flags:

cpanm LWP::Simple
cpanm Mojolicious

Important nuance: If you are deploying to a VPS with cPanel, cPanel manages its own Perl environment separately from the system Perl. Modules installed to the system path may not be visible to cPanel's Perl interpreter. Always verify which Perl binary is in use with which perl and perl -V before installing.

Method 5: System Package Manager

On Debian/Ubuntu and RHEL/CentOS systems, many popular CPAN modules are packaged as native OS packages. This approach integrates with system updates and avoids build toolchain requirements.

Debian/Ubuntu:

sudo apt-get install libwww-perl libdbi-perl libmoose-perl

RHEL/CentOS/Fedora:

sudo dnf install perl-LWP-Simple perl-DBI perl-Moose

When to prefer system packages vs. CPAN:

CriterionSystem PackageCPAN / cpanm
Version currencyOften outdatedAlways latest
Dependency managementOS-managedCPAN-managed
Build toolchain requiredNoYes (for XS modules)
Suitable for production serversYes, for stabilityYes, with pinning
Works without internetYes (local mirror)Requires mirror setup
Integrates with system updatesYesNo
Supports local::libNoYes

Comparison of Perl Module Installation Methods

MethodRoot RequiredSpeedOffline SupportVersion PinningBest Use Case
CPAN shellYes (default)SlowNoLimitedLegacy systems, initial setup
`cpanm`OptionalFastWith local mirrorYesAll modern workflows
Manual tarballOptionalManualYesYesAir-gapped, patched builds
`local::lib`NoFast (with cpanm)NoYesShared hosting, non-root
System packagesYesFastYes (cached)NoStability-focused servers

Verifying Module Installation

After installation, always confirm the module is loadable and check its version:

perl -MLWPSimple -e 'print $LWP::Simple::VERSION, "n"'

For modules that do not export a $VERSION scalar directly, use:

perl -e 'use LWP::Simple; print "OKn"'

To find where a module was installed on disk:

perl -MLWP::Simple -e 'print $INC{"LWP/Simple.pm"}, "n"'

To inspect the full @INC search path your Perl uses:

perl -e 'print join("n", @INC), "n"'

Troubleshooting Common Installation Failures

Missing C Compiler or Build Tools

XS modules (those with C extensions) require a C compiler and make. On Debian/Ubuntu:

sudo apt-get install build-essential

On RHEL/CentOS:

sudo dnf groupinstall "Development Tools"

Failed TLS/SSL Connections to CPAN Mirrors

If cpanm or the CPAN shell fails with SSL errors, install the required modules first:

sudo cpan Mozilla::CA IO::Socket::SSL LWP::Protocol::https

This is especially relevant when deploying Perl applications that also handle HTTPS traffic — the same SSL infrastructure that secures your SSL Certificates on the web layer must be reflected in your Perl environment.

Dependency Conflicts

When a module requires a version of a dependency that conflicts with what is already installed, use cpanm's --notest flag cautiously, or isolate the environment with local::lib or perlbrew:

cpanm --notest Problematic::Module

Never use --notest in production without understanding why tests are failing. It suppresses the only automated safety check in the build pipeline.

Permission Denied During Installation

If make install fails with permission errors and sudo is not available, redirect to a local library:

cpanm --local-lib=~/perl5 LWP::Simple

Module Not Found After Installation

If perl -MModule::Name -e 1 reports "Can't locate," the module was installed to a path not in @INC. Check:

perl -V | grep -A 20 '@INC'

Then either reinstall to the correct path or set PERL5LIB:

export PERL5LIB="$HOME/perl5/lib/perl5:$PERL5LIB"

Deploying Perl Applications on Server Infrastructure

For production Perl applications — particularly those using frameworks like Mojolicious, Catalyst, or Dancer2 — module management must be treated as infrastructure, not an afterthought.

Recommended production workflow:

  1. Use a cpanfile to declare all dependencies with minimum version constraints.
  2. Use cpanm --installdeps . in your deployment script.
  3. Pin critical module versions to prevent unexpected upgrades.
  4. Run make test or cpanm --test-only in your CI pipeline before deploying.
  5. Use local::lib or perlbrew to isolate application environments from the system Perl.

Perl applications that handle web requests, process email, or interact with databases are common workloads on VPS Hosting and Dedicated Servers. On dedicated infrastructure, you have full control over the Perl version, build toolchain, and module paths — eliminating the constraints that make local::lib necessary on shared environments.

If your Perl application sends transactional email, the Email::Sender or MIME::Lite modules integrate directly with SMTP infrastructure. Pairing this with a reliable Email Hosting service ensures your application mail does not get flagged as spam due to misconfigured relay settings.

Technical Decision Checklist

Use this matrix to select the correct installation approach for your situation:

  • You have root access and need the latest module version — Use cpanm with a system-wide install.
  • You are on shared hosting or lack sudo — Use local::lib with cpanm.
  • You are in an air-gapped or offline environment — Download tarballs manually and build from source.
  • You need reproducible builds across multiple servers — Use a cpanfile and cpanm --installdeps . in your deployment pipeline.
  • You need OS-level stability and automatic security updates — Use system packages (apt, dnf) for well-packaged modules.
  • You are running multiple Perl versions on one machine — Use perlbrew to manage Perl installations, then cpanm within each.
  • A module fails its test suite — Investigate before installing. Check the module's issue tracker on MetaCPAN.
  • You need to isolate a Perl app's dependencies from the system — Combine perlbrew + local::lib for full isolation.

FAQ

What is the difference between use and require when loading a Perl module?

use Module is processed at compile time and will abort the script immediately if the module is not found. require Module is evaluated at runtime, allowing conditional loading. For most modules, use is the correct choice because it catches missing dependencies before execution begins.

Why does cpanm fail with "SSL verification failed" on a fresh server?

The cpanm client uses LWP::Protocol::https for HTTPS connections to CPAN mirrors. On a minimal OS install, the required SSL libraries (IO::Socket::SSL, Mozilla::CA) may be absent. Install them via your system package manager first: sudo apt-get install libssl-dev followed by cpan IO::Socket::SSL.

Can I install Perl modules without internet access?

Yes. Download the .tar.gz distribution from MetaCPAN on a connected machine, transfer it to the target server, and build manually using perl Makefile.PL && make && make test && sudo make install. You can also set up a local CPAN mirror using CPAN::Mini for team-wide offline access.

How do I check which version of a module is currently installed?

Run perl -MModule::Name -e 'print Module::Name->VERSION, "n"'. For example: perl -MLWP::Simple -e 'print $LWP::Simple::VERSION, "n"'. Alternatively, use cpanm --info Module::Name to see both the installed version and the latest available on CPAN.

What should I do if a module installs successfully but my script still cannot find it?

The module was installed to a directory not in your Perl's @INC. Run perl -V to see the full include path, then compare it against the output of perl -MModule::Name -e 'print $INC{"Module/Name.pm"}'. Either reinstall to the correct prefix, or export PERL5LIB to point Perl at the correct directory before running your script.

15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started