Perl modules often need the CPAN client or your distribution’s package manager to install correctly. Understanding how to install perl modules in linux is a key skill for any developer working with Perl scripts. Whether you are setting up a new server or maintaining existing code, knowing the right method saves time and prevents errors.
This guide walks you through every major approach. You will learn to use the CPAN shell, the cpanm tool, your Linux package manager, and manual installation. Each method has its own strengths, and we cover when to use each one.
How To Install Perl Modules In Linux
Before you start, make sure you have Perl installed. Most Linux distributions come with Perl pre-installed. You can check with perl -v in your terminal. If Perl is missing, install it using your package manager.
You also need a C compiler and development tools. Many Perl modules compile C code during installation. On Debian-based systems, install build-essential. On Red Hat-based systems, install gcc and make.
Method 1: Using The CPAN Shell
The CPAN shell is the most traditional way to install Perl modules. It connects directly to the Comprehensive Perl Archive Network. Here is how to use it.
- Open your terminal.
- Type
cpanand press Enter. The first time you run it, it asks configuration questions. Accept the defaults unless you have specific needs. - Once inside the CPAN shell, type
install Module::Name. ReplaceModule::Namewith the actual module name, likeinstall DBI. - Wait for the installation to finish. The shell downloads, compiles, and installs the module automatically.
- Exit the CPAN shell by typing
quit.
This method works well for most modules. However, it can be slow because it resolves dependencies one by one. If you have a slow internet connection, consider using cpanm instead.
Method 2: Using Cpanminus (Cpanm)
cpanm is a lighter, faster alternative to the CPAN shell. It is designed for simplicity and speed. First, you need to install cpanm itself.
Install cpanm using your package manager. On Debian/Ubuntu, run sudo apt install cpanminus. On CentOS/RHEL, enable EPEL and run sudo yum install cpanminus. On Fedora, use sudo dnf install cpanminus.
Once installed, use cpanm like this:
sudo cpanm Module::Name
For example, to install the JSON module, run sudo cpanm JSON. cpanm automatically resolves and installs all dependencies. It is much faster than the traditional CPAN shell.
You can also install modules locally without sudo. Use the -l flag to specify a local library path:
cpanm -l ~/perl5 Module::Name
This installs the module into your home directory. You then need to set the PERL5LIB environment variable to include that path.
Method 3: Using Your Linux Package Manager
Many Perl modules are available as pre-built packages in your distribution’s repositories. This is often the easiest method because it handles dependencies automatically and integrates with system updates.
On Debian-based systems (Ubuntu, Debian, Mint), use apt:
sudo apt install libmodule-name-perl
Note the naming convention: the module name is converted to lowercase, colons become dashes, and a “lib” prefix and “-perl” suffix are added. For example, DBI becomes libdbi-perl. JSON::XS becomes libjson-xs-perl.
On Red Hat-based systems (CentOS, RHEL, Fedora), use yum or dnf:
sudo yum install perl-Module-Name
Here, colons become dashes, and a “perl-” prefix is added. For example, DBI becomes perl-DBI. JSON::XS becomes perl-JSON-XS.
On openSUSE, use zypper:
sudo zypper install perl-Module-Name
This method is ideal for system-wide installations. However, the packages may not always be the latest version. For bleeding-edge modules, use CPAN or cpanm.
Method 4: Manual Installation From Source
Sometimes you need to install a module manually. This happens when the module is not in repositories or when you need a specific version. The process involves downloading the source tarball and running the standard Perl module build commands.
- Download the module tarball from CPAN or the module’s website. Look for files ending in
.tar.gz. - Extract the tarball:
tar -xzf Module-Name-1.0.tar.gz. - Change into the extracted directory:
cd Module-Name-1.0. - Run the standard build commands:
perl Makefile.PL make make test sudo make install - If the module uses
Build.PLinstead, use:perl Build.PL ./Build ./Build test sudo ./Build install
Manual installation gives you full control. But it requires you to handle dependencies manually. Always run make test before installing to ensure the module works correctly.
Method 5: Using Local::Lib For User Installations
If you do not have root access, or if you want to keep modules separate from the system, use local::lib. This tool creates a local Perl library directory in your home folder.
First, install local::lib using cpanm or your package manager:
cpanm local::lib
Then, set up the environment in your shell configuration file (like .bashrc):
eval "$(perl -I ~/perl5/lib/perl5 -Mlocal::lib)"
Now, any module you install with cpanm will go into ~/perl5. This method is perfect for shared hosting environments or when you want to avoid polluting the system Perl.
Common Issues And Troubleshooting
Installing Perl modules sometimes fails. Here are common problems and solutions.
- Missing dependencies: The error message usually tells you which module is missing. Install that module first. Use cpanm’s
--installdepsflag to auto-install dependencies:cpanm --installdeps .. - Permission denied: You need root access for system-wide installations. Use
sudoor install locally withlocal::lib. - Compiler errors: Install the necessary development tools. On Debian:
sudo apt install build-essential. On Red Hat:sudo yum groupinstall "Development Tools". - SSL certificate errors: Update your system’s CA certificates. On Debian:
sudo apt install ca-certificates. On Red Hat:sudo yum install ca-certificates. - Network timeouts: CPAN servers may be slow. Use a mirror closer to you. In the CPAN shell, set the mirror with
o conf urllist push http://your-mirror.
Best Practices For Managing Perl Modules
Keep your modules organized. Here are some tips.
- Use a version control system like Git for your Perl projects. Include a
cpanfilethat lists required modules. Tools likeCartoncan then install them automatically. - Regularly update modules. Use
cpanm --self-upgradeto update cpanm itself. Then runcpanm --updateto update all installed modules. - Test module installations in a staging environment before deploying to production.
- Consider using Docker containers to isolate Perl environments. This avoids conflicts between projects.
Choosing The Right Method
Each installation method has its place. Use this quick guide to decide.
- Package manager: Best for system-wide modules that need to stay stable. Use this for production servers.
- cpanm: Best for development environments. Fast and easy to use. Ideal for one-off installations.
- CPAN shell: Use when cpanm is not available. Good for interactive module management.
- Manual installation: Only when the module is not available through other methods. Requires more effort.
- local::lib: Best for shared hosting or when you lack root access. Keeps your modules separate.
Advanced: Using Carton For Project Dependencies
Carton is a dependency manager for Perl. It works like Bundler for Ruby or npm for Node.js. You define your modules in a cpanfile, and Carton installs them into a local directory.
Install Carton with cpanm:
cpanm Carton
Create a cpanfile in your project root:
requires 'DBI';
requires 'JSON';
requires 'Mojolicious';
Then run carton install. Carton installs the modules into a local/ directory. Use carton exec to run your script with the correct library path:
carton exec perl your_script.pl
This method ensures reproducible builds across different machines.
Security Considerations
Always install modules from trusted sources. CPAN is generally safe, but malicious modules have appeared in the past. Verify module signatures if possible. Use the --verify flag with cpanm to check signatures.
Avoid installing modules as root unless necessary. Use local installations for development. For production, use your distribution’s packages, which are vetted by the maintainers.
Keep your Perl installation updated. Security patches are regularly released for Perl itself and for popular modules.
Frequently Asked Questions
What Is The Easiest Way To Install Perl Modules In Linux?
The easiest way is using your distribution’s package manager. For example, on Ubuntu, use sudo apt install libmodule-name-perl. This handles dependencies automatically and integrates with system updates.
How Do I Install Perl Modules Without Root Access?
Use local::lib to create a local Perl library in your home directory. Install modules with cpanm -l ~/perl5 Module::Name. Set the PERL5LIB environment variable to include the local path.
Why Does My Perl Module Installation Fail With A Compiler Error?
This usually means you are missing development tools. Install build-essential on Debian-based systems or gcc and make on Red Hat-based systems. Some modules also require specific libraries like libssl-dev.
Can I Install Perl Modules Using Pip?
No, pip is for Python packages. Perl modules are installed using CPAN, cpanm, or your package manager. Do not confuse the two ecosystems.
How Do I Update All Installed Perl Modules?
Use cpanm with the --update flag: cpanm --update. This updates all modules installed via cpanm. For CPAN shell modules, run cpan and then upgrade.
Conclusion
Mastering how to install perl modules in linux is essential for any Perl developer. You now have five reliable methods at your disposal. Start with your package manager for simplicity, switch to cpanm for speed, and use manual installation only when needed.
Remember to always test your installations and keep your environment organized. With these tools, you can handle any Perl module requirement that comes your way. Happy coding!