How to install cpan on RHEL 8 / CentOS 8

Perl is a well-known programming language with long development history. The same long history provides the countless modules written in it, and distributed in various channels throughout the World Wide Web. As with most programming languages, if you implement something that you find useful, and maybe write it in a generic way, there are high chances others would find it useful too.

If you share your work with others, they don’t need to re-implement the wheel, they can spend time on other tools, thus making the community as a whole richer, and we have found the spirit of open source with a short walk. But as many people write perl modules, and perl runs on just about anything, it can be hard to find the module you are looking for, let alone build it in your environment. And that’s where cpan comes into play. CPAN itself is a large perl module repository, the cpan tool is a client that is able to fetch and compile needed modules to the local system.

In this tutorial we’ll install cpan on a RHEL 8 / CentOS 8, run the initial configuration, and install a module with this tool’s help.

In this tutorial you will learn:

  • How to install cpan
  • How to run initial configuration
  • How to install a perl module with cpan

Successful cpan perl module install with cpan on RHEL 8

Successful cpan perl module install with cpan on RHEL 8

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System RHEL 8 / CentOS 8
Software cpan 2.18
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to install cpan on RHEL 8 / CentOS 8 step by step instructions

The rpm package of cpan is present in the software sources after enabling subsription management repositories.

While it’s name is a bit tricky at first, after a few encounters with perl packages we’ll get used to the naming convention, and it can come quite handy to query the rpm database for perl-* pattern when we search for something written in perl. But that’s the realm of rpm packaging.

  1. We can install package cpan with dnf:
    # dnf install perl-CPAN
  2. Before we can start using our new tool, we need to run the initial configuration. At version 2.18 it is pretty automatic if we choose to.
    # cpan
    [...] requires configuration, but most of it can be done automatically.
    If you answer 'no' below, you will enter an interactive dialog for each
    configuration option instead.
    Would you like to configure as much as possible automatically? [yes] yes

    In the output we can see that the autoconfiguration creates needed default directories and configuration files. We can always re-run this step if needed with 'o conf init' provided to the CPAN shell (which is what we’ll use in the next step).

  3. When the configuration is complete, we arrive at to the CPAN shell, where we can directly enter commands to cpan. To test our tool we will install the Config::Tiny perl module, which can be familiar to anyone as “missing” who ever worked with perl-based Nagios plugins.
    cpan[1]> install "Config::Tiny"
    Fetching with HTTP::Tiny:
    Reading '/root/.local/share/.cpan/sources/authors/01mailrc.txt.gz'
    Fetching with HTTP::Tiny:
    Reading '/root/.local/share/.cpan/sources/modules/02packages.details.txt.gz'
    Running make install
    Manifying 1 pod document
    Installing /usr/local/share/perl5/Config/
    Installing /usr/local/share/man/man3/Config::Tiny.3pm
    Appending installation info to /usr/lib64/perl5/perllocal.pod
      /usr/bin/make install  -- OK
    cpan[2]> exit

    After some work, cpan completes the module installation, and with that we verified our tool is working as intended. We can leave the CPAN shell with exit, or enter another command, maybe installing more needed modules.