Where does CPAN install modules?

CPAN doesn't actually install files. It runs the install script embedded in each distribution, which then performs the actual install.

For distributions using ExtUtils::MakeMaker, the defaults are documented here: https://metacpan.org/pod/ExtUtils::MakeMaker#make-install (and the default value of INSTALLDIRS is site). For Module::Build, see https://metacpan.org/pod/Module::Build#INSTALL-PATHS.

When the documentation talks about $Config{foo} or %Config, it means the %Config variable provided by the Config module. The value of $Config{foo} can also be inspected by running perl -V:foo.

(If you think this seems unnecessarily complicated, you're right.)

The short version is that perl has multiple "system directories", one of which is for "site specific" modules and thus used as the default installation target. You are right that this is a single directory (per perl install), which doesn't mesh well with a multi-user system: It is shared across all users, and you need root permissions to install modules (and doing so might upgrade/override modules from system packages, which is a bad idea).

What people do instead is to configure ExtUtils::MakeMaker, Module::Build, etc to install into a user's home directory. This can be done with environment variables. Then they tell perl to add this directory to @INC, so modules can actually be found and loaded. This is done with another environment variable, PERL5LIB. (PERL5LIB doesn't affect installation, it's purely used for loading.)

All of the above is automated and encapsulated in local::lib. (local::lib can also be used to e.g. create a per-project module subdirectory.)

The CPAN documentation also says:

As of CPAN 1.9463, if you do not have permission to write the default perl library directories, CPAN's configuration process will ask you whether you want to bootstrap local::lib, which makes keeping a personal perl library directory easy.


You can sidestep the whole issue by installing a private perl in your home directory (in which case the "system" directory is just another subdirectory under your $HOME and thus isn't shared with anyone and can be written to by you). This is very easy with e.g. perlbrew.


Another note: You've just found a bug in the documentation for PERL5LIB. "and the current directory" is outdated: . has been removed from the default list of module locations for security reasons.


First of all, CPAN doesn't install modules. It's a repository.

cpan doesn't install modules either. cpan downloads distributions from CPAN and runs the installer provided within, be it Makefile.PL or Build.PL. (Same goes for cpanm and cpanp.)

These installation scripts mostly use ExtUtils::MakeMaker or Module::Build to install the distribution (though other installers exist).


Perl specifies three sets of installation locations.

  • perl, for modules included with Perl itself.
  • vendor, for modules installed by the provider of your perl binary.
  • site, for modules installed using cpan.

Each of these sets provides installation locations for a number of files types.

                        Installation location
                        --------------------------------------------------------
Type of file            perl             vendor                 site
----------------------  ---------------  ---------------------  -------------------
Build-specific modules  installarchlib   installvendorarch      installsitearch
Modules                 installprivlib   installvendorlib       installsitelib
Binary programs         installbin       installvendorbin       installsitebin
Other programs          installscript    installvendorscript    installsitescript
man pages for scripts   installman1dir   installvendorman1dir   installsiteman1dir
man pages for modules   installman3dir   installvendorman3dir   installsiteman3dir
html docs for scripts   installhtml1dir  installvendorhtml1dir  installsitehtml1dir
html docs for modules   installhtml3dir  installvendorhtml3dir  installsitehtml3dir

You can obtain the path for any of these locations using the following:

perl -V:{var}        # Substitute `{var}` for the var name.

You can obtain all the paths for these locations using the following:

perl -V:'install.*'

Those are the defaults use by the installers[1]. However, the two most commonly used installers allow the user doing to installation to override any and all of these. If a module is installed in a non-standard location,

  • PERL5LIB can be used to let perl know where to find the modules.
  • PATH can be used to let the system know where to find bundled programs.
  • MANPATH can be used to let man know where to find the man pages.

Tags:

Perl

Cpan