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 shellInside the shell, install any module by name:
cpan[1]> install LWP::Simple
cpan[2]> install DBIOne-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 CPANMethod 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::cpanminusIf curl is unavailable:
wget -O - https://cpanmin.us | perl - --sudo App::cpanminusOn systems where you already have a working CPAN client:
cpan App::cpanminusInstalling Modules with cpanm
cpanm LWP::Simple
cpanm DBI
cpanm MooseInstalling a Specific Version
Version pinning is essential for reproducible builds. To install an exact release:
cpanm GAAS/libwww-perl-6.67.tar.gzOr use the @version syntax:
cpanm LWP::Simple@6.67Installing 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.gzStep 2: Extract the Archive
tar -xzf libwww-perl-6.67.tar.gz
cd libwww-perl-6.67Step 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 installFor Build.PL (Module::Build):
perl Build.PL
./Build
./Build test
sudo ./Build installWhat each step does:
perl Makefile.PL— Interrogates your Perl installation and system libraries to generate a platform-specificMakefile.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::libOr 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 ~/.bashrcThis 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 MojoliciousImportant 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-perlRHEL/CentOS/Fedora:
sudo dnf install perl-LWP-Simple perl-DBI perl-MooseWhen to prefer system packages vs. CPAN:
| Criterion | System Package | CPAN / cpanm |
|---|---|---|
| — | — | — |
| Version currency | Often outdated | Always latest |
| Dependency management | OS-managed | CPAN-managed |
| Build toolchain required | No | Yes (for XS modules) |
| Suitable for production servers | Yes, for stability | Yes, with pinning |
| Works without internet | Yes (local mirror) | Requires mirror setup |
| Integrates with system updates | Yes | No |
| Supports local::lib | No | Yes |
Comparison of Perl Module Installation Methods
| Method | Root Required | Speed | Offline Support | Version Pinning | Best Use Case |
|---|---|---|---|---|---|
| — | — | — | — | — | — |
| CPAN shell | Yes (default) | Slow | No | Limited | Legacy systems, initial setup |
| `cpanm` | Optional | Fast | With local mirror | Yes | All modern workflows |
| Manual tarball | Optional | Manual | Yes | Yes | Air-gapped, patched builds |
| `local::lib` | No | Fast (with cpanm) | No | Yes | Shared hosting, non-root |
| System packages | Yes | Fast | Yes (cached) | No | Stability-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-essentialOn 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::httpsThis 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::ModuleNever 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::SimpleModule 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:
- Use a
cpanfileto declare all dependencies with minimum version constraints. - Use
cpanm --installdeps .in your deployment script. - Pin critical module versions to prevent unexpected upgrades.
- Run
make testorcpanm --test-onlyin your CI pipeline before deploying. - Use
local::liborperlbrewto 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
cpanmwith a system-wide install. - You are on shared hosting or lack sudo — Use
local::libwithcpanm. - 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
cpanfileandcpanm --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
perlbrewto manage Perl installations, thencpanmwithin 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::libfor 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.
