cpanm - Installation of Perl Modules on User Directories 2022
Tags: perl, cpan, cpanm, beginners
Published: 2022-04-08 09:05:24

[cpanm](https://metacpan.org/dist/App-cpanminus/view/bin/cpanm) is a command line tool to install [CPAN](https://en.perlzemi.com/blog/20100504127089.html) modules on user directories. 

This article is originally <a href="https://en.perlzemi.com/blog/20101027127859.html">cpanm - Installation of Perl Modules on User Directories | Perl ABC</a>

<pre>
# Installation of CPAN modules
cpanm MoudleName
</pre>

### Installation of cpanm

Explains how to install cpanm.

#### Strawberry Perl

[Strawberry Perl](https://en.perlzemi.com/blog/20180808153368.html) has cpanm by default. Strawberry Perl is one of Perl distributions on Windows.

#### perlbrew

If you install [perlbrew](https://en.perlzemi.com/blog/20110220130464.html), you can install cpanm in the following command.

<pre>
perlbrew install-cpanm
</pre>

#### plenv

If you install [plenv](https://en.perlzemi.com/blog/20180816153437.html), you can install cpanm in the following command.

<pre>
plenv install-cpanm
</pre>

#### Download cpanm itself

You can download cpanm itself.

<pre>
curl -L -O http://xrl.us/cpanm > cpanm
chmod 755 cpanm
</pre>

You can execute cpanm as the following.

<pre>
./cpanm ModuleName
</pre>

#### Package Managers

Install cpanm using package managers.

**[Ubuntu](https://en.ubuntu.perlzemi.com/), [Debian](https://en.wikipedia.org/wiki/Debian)**

<pre>
apt install cpanminus
</pre>

**[Red Hat Linux](https://en.wikipedia.org/wiki/Red_Hat_Linux), [Cent OS](https://en.centos.perlzemi.com/)**

<pre>
yum install perl-App-cpanminus
</pre>

### Install CPAN Modules

Install CPAN modules using cpanm.

<pre>
# Install CPAN modules.
cpanm Mojolicious
</pre>

#### Version Specific

You can install modules with the version.

<pre>
cpanm Mojolicious@9.23
</pre>

#### Specifying the installation directory

You can install CPAN modules on the specified directory using **-L** option.

<pre>
cpanm -L extlib DBIx::Custom
</pre>

[DBIx::Custom](https://dbix-custom.perlzemi.com/) will be installed "extlib" in the current directory.

### cpanfile - Definition of Modules

You can defined modules you want to install and the versions using **cpanfile**.

#### Writing cpanfile

**cpanfile** is a file to define the modules and the versions.

Create the file "cpanfile".

<pre>
# Create a "cpanfile" file
touch cpanfile
</pre>

Define the moudles and the versions.

<pre>
requires 'DBI', '== 1.619';
requires 'DBD::SQLite', '== 1.36_04';
requires 'DBIx::Custom', '== 0.25';
</pre>**cpanfile formats:**

<pre>
requires MODULE_NAME VERSION
</pre>

You can use the following syntax for the version:

<table>
  <tr>
    <td>
      <b> Descriptions </b>
    </td>
    <td>
      <b> Formats </b>
    </td>
    <td>
      <b> Examples </b>
    </td>
  </tr>

  <tr>
    <td>
      Specified version
    </td>
    <td>
      == Version
    </td>
    <td>
      == 0.03
    </td>
  </tr>

  <tr>
    <td>
      Specified version or higher
    </td>
    <td>
      >= Version
    </td>
    <td>
      >= 0.03
    </td>
  </tr>

  <tr>
    <td>
      Below the specified version
    </td>
    <td>
      <= Version
    </td>
    <td>
      <= 0.03
    </td>
  </tr>

  <tr>
    <td>
      Greater than the specified version
    </td>
    <td>
      > Version
    </td>
    <td>
      > 0.03
    </td>
  </tr>

  <tr>
    <td>
      Less than the specified version
    </td>
    <td>
      < Version
    </td>
    <td>
      < 0.03
    </td>
  </tr>
</table>


#### Install Modules using cpanfile

You can install CPAN modules from the definition of cpanfile using the "**--installdeps**" option.

<pre>
# Install CPAN modules from the definition of cpanfile
perl cpanm --installdeps .
</pre>

You can also install modules on the specified directory using the "**-L**" option.

<pre>
perl cpanm -L extlib - installdeps.
</pre>

### Showing Help

Show the cpanm help.

<pre>
cpanm -h
</pre>