Perl Scripts ·

How to Check Whether a Perl Module Is Installed

By Manu Mathew, Technical Lead Engineer · Published 15 January 2014 · Last updated 5 October 2026

Isometric illustration of a shelf of unlabelled boxes, one box found and highlighted in blue

A script fails with “Can’t locate Text/CSV.pm in @INC”, or you are about to deploy to a new server and want to know what is already there. Either way, the first question is whether a Perl module is installed, and which version.

Here is how to check if a Perl module is installed, from the terminal and from inside a script. I tested every example on Perl 5.40.

Quick answer

From the terminal, run perl -MModule::Name -e 1: no output means it is installed, and an error starting “Can’t locate” means it isn’t. Inside a script, require the module in an eval and check the result. Module::Load::Conditional and Module::CoreList, both core modules, report versions, file locations and whether a module ships with Perl.

How do I check from the terminal?

Load the module with -M and run an empty program; Perl prints nothing if the module loads.

perl -MList::Util -e 1 && echo "List::Util is installed"
# List::Util is installed

perl -MNot::A::Real::Module -e 1
# Can't locate Not/A/Real/Module.pm in @INC (you may need to install the Not::A::Real::Module module) ...

The exit status is 0 on success, so the same command works in shell scripts and deployment checks.

How do I find the installed version and file location?

Print the module’s VERSION and its entry in %INC, which records the file Perl loaded.

perl -MList::Util -le 'print List::Util->VERSION'
# 1.63

perl -MList::Util -le 'print $INC{"List/Util.pm"}'
# /usr/lib/x86_64-linux-gnu/perl-base/List/Util.pm

The location tells you whether a module came from the system packages, a local::lib folder or a project directory, which is often the real cause when two machines behave differently.

How do I check inside a Perl script?

Turn the module name into a file path and require it inside eval, which returns false instead of dying when the module is missing.

use strict;
use warnings;

sub module_available {
    my ($module) = @_;
    (my $file = "$module.pm") =~ s{::}{/}g;   # Time::Piece -> Time/Piece.pm
    return eval { require $file; 1 } ? 1 : 0;
}

for my $module (qw(Time::Piece Not::A::Real::Module)) {
    printf "%-22s %s\n", $module, module_available($module) ? 'available' : 'missing';
}
Time::Piece            available
Not::A::Real::Module   missing

The older version of this note used eval("use $mod"), a string eval. That works, but it runs whatever text is in $mod as Perl code, so it is unsafe if the name ever comes from outside the script. Requiring a file path avoids that.

Can I check without loading the module?

Yes. check_install from Module::Load::Conditional finds the module and its version without running it; can_load loads it only if a minimum version is met.

use strict;
use warnings;
use Module::Load::Conditional qw(check_install can_load);

my $info = check_install(module => 'List::Util');   # looks, but does not load
print "List::Util $info->{version} at $info->{file}\n" if $info;

if (can_load(modules => { 'List::Util' => '1.45' })) {   # loads it, with a minimum version
    print "List::Util 1.45 or newer is loaded\n";
}
List::Util 1.63 at /usr/lib/x86_64-linux-gnu/perl-base/List/Util.pm
List::Util 1.45 or newer is loaded

Is the module part of core Perl?

Ask Module::CoreList: the corelist command, or Module::CoreList->is_core in code, tells you whether a module ships with Perl and since when.

corelist List::Util
# List::Util was first released with perl v5.7.3

perl -MModule::CoreList -le 'print Module::CoreList->is_core("Time::Piece") ? "core" : "not core"'
# core

Core modules are a safe choice for scripts that must run on any server, but note that some distributions split core modules into separate packages, so a minimal server may still lack one.

Frequently asked questions

Why does Perl say a module is missing when I installed it?

Usually it was installed for a different Perl, or into a folder that isn’t in @INC. Run which perl and perl -V to see which Perl runs and where it looks. Install the module with that same Perl, and if you use local::lib, load it in the same shell.

How do I install a missing module?

Use your system’s packages where they exist, such as apt install libtext-csv-perl on Debian or Ubuntu, so updates arrive with the system. Otherwise use cpanm Module::Name, or cpanm -L local Module::Name to install into a project folder instead of the system Perl.

How do I list every installed module?

There is no single built-in command that covers every case. cpan -l lists the modules the CPAN client can find. On Debian or Ubuntu, dpkg -l 'lib*-perl' lists modules installed as system packages.

Can use or require check the version too?

Yes. use List::Util 1.45; stops at compile time if the installed version is older, with a message such as “List::Util version 1.45 required”. List::Util->VERSION(1.45) does the same check at run time.

Related notes: removing duplicate values from a Perl array; checking whether a Perl variable is a number; where Perl still belongs; hiring a remote Perl developer.

Need help with Perl code?

If you have Perl code that needs fixing, extending or upgrading, I’m happy to take a look. I work remotely from Kozhikode, Kerala, with teams across India and elsewhere; Perl development and maintenance explains how I help. If you are deciding how to bring someone in, Hiring a remote Perl developer is a short checklist.

You can reach me through the contact section or email me at manu@manu.co.in. A few lines about the system and what you need is a good start.

An earlier version of this note (2014) gave the terminal check and a string-eval helper. This version is rewritten with safer code and more ways to check.

Sources: perlrun: the -M switch; perlfunc: require; Module::Load::Conditional; Module::CoreList.

Back to the blog