How to Install Perl on Rocky Linux

This guide shows you how to install Perl on Rocky Linux using the DNF repositories or by compiling the latest stable release from source. You will set up Perl, confirm it runs, and work with CPAN modules so you can automate tasks, parse logs, or build reliable scripts.

Rocky Linux server and minimal installations typically do not include Perl, so you will install it yourself. DNF is the recommended choice for most systems because it provides security updates, while source compilation is best when you need a newer version or custom build options.

Choose Your Perl Installation Method

Rocky Linux offers two primary paths for installing Perl. DNF provides stable, tested packages with automatic security updates, while source compilation lets you build the newest Perl release with custom options.

MethodChannelVersionUpdatesBest For
DNF Package ManagerRocky Linux Repos5.26.3 (Rocky 8), 5.32.1 (Rocky 9), 5.40.2 (Rocky 10)Automatic via dnf upgradeMost users who want tested packages with minimal maintenance
Source CompilationPerl.org DownloadsLatest stable (5.42.0)Manual recompilation requiredDevelopers needing the newest Perl features or custom builds

For most users, the DNF method is recommended because it provides automatic security updates and requires minimal maintenance. Only compile from source if you specifically need features unavailable in the repository version or require custom compilation flags.

These installation steps apply to Rocky Linux 8, 9, and 10. The DNF Perl versions we verified are 5.26.3 on Rocky 8, 5.32.1 on Rocky 9, and 5.40.2 on Rocky 10.

Method 1: Install Perl via DNF

Check if Perl is Already Installed

Before installing Perl, check if it is already present on your Rocky Linux system:

perl -v

If Perl is not installed, the shell reports an error like this:

bash: perl: command not found

This confirms you need to proceed with installation.

Update Package Information

Refresh the package metadata and apply any pending updates:

sudo dnf upgrade --refresh

This command synchronizes your local package database with the Rocky repositories and applies available updates.

Install Perl and Module Build Dependencies

Install the Perl interpreter, cpanm for CPAN modules, and headers needed for compiling XS-based modules:

sudo dnf install perl perl-App-cpanminus perl-devel

The perl-App-cpanminus package provides cpanm, a lightweight CPAN client, while perl-devel adds headers and libraries required by modules that compile C code.

Verify the Perl Installation

Check the installed Perl version:

perl -v

On Rocky Linux 10, the output looks like this:

This is perl 5, version 40, subversion 2 (v5.40.2) built for x86_64-linux-thread-multi
(with 16 registered patches, see perl -V for more detail)

Copyright 1987-2025, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at https://www.perl.org/, the Perl Home Page.

Confirm cpanm is available:

cpanm --version | head -n 2

Example output on Rocky Linux 10:

cpanm (App::cpanminus) version 1.7047 (/usr/bin/cpanm)
perl version 5.040002 (/usr/bin/perl)

Manage Perl Modules with cpanm

cpanm (short for CPAN Minus) is a lightweight package manager that simplifies installing and upgrading Perl modules from CPAN without interactive setup.

Install Modules with cpanm

Use this syntax to install a module system-wide:

sudo cpanm Module::Name

Replace Module::Name with the module you need. The sudo prefix is required for system-wide installation.

Some CPAN modules require C compilation. If module installation fails with compiler errors, install build tools: sudo dnf install perl-devel gcc make.

cpanm from the Rocky repositories uses /usr/bin/perl. If you compiled Perl to /usr/local, use /usr/local/bin/cpan when you need modules for the source build so they install into the same tree.

Search for Modules on CPAN

To find modules for specific tasks, visit MetaCPAN, which provides a searchable index of modules with documentation and dependency details.

Upgrade Installed Modules

To upgrade an installed module to the latest release, run the same cpanm install command:

sudo cpanm Module::Name

List Installed Perl Modules

Use ExtUtils::Installed to list the modules installed on your system:

perl -MExtUtils::Installed -E 'say for ExtUtils::Installed->new->modules'

On a fresh installation, the output is minimal:

Perl

Find Module Installation Paths

To locate where a specific module is installed, use perldoc -l:

perldoc -l Carp
/usr/share/perl5/vendor_perl/Carp.pm

This is useful when troubleshooting module conflicts or confirming which copy Perl is loading.

Remove CPAN Modules

cpanm includes an experimental uninstall feature:

sudo cpanm -U Module::Name

The -U (or --uninstall) flag removes the specified module. This feature is experimental and may not remove every related file.

Method 2: Install Perl via Source Compilation

Compiling Perl from source gives you the latest stable release (currently 5.42.0 from perl.org) with full control over build options. This method is ideal when you need a newer version than the Rocky repositories provide or a custom build.

Install Build Dependencies

Install the compiler toolchain and build utilities required for a source build:

sudo dnf groupinstall "Development Tools"

This group includes gcc, make, and related utilities.

Create a Build Directory

Use a build directory under your home folder:

mkdir -p ~/src/perl-build
cd ~/src/perl-build

Download the Perl Source Code

Fetch the latest stable Perl tarball URL from perl.org and download it:

PERL_TARBALL_URL=$(curl -fsSL https://www.perl.org/get.html | grep -oE 'https://www\.cpan\.org/src/5\.0/perl-[0-9]+\.[0-9]+\.[0-9]+\.tar\.gz' | head -n 1)
curl -fLO --progress-bar "$PERL_TARBALL_URL"

If you prefer manual downloads, visit the Perl download page, copy the .tar.gz URL, then run curl -fLO --progress-bar <URL>.

Extract and Configure the Source

Extract the source archive and configure the build:

tar -xzf perl-*.tar.gz
cd perl-*/
./Configure -des -Dprefix=/usr/local

The -des flag accepts the default answers, while -Dprefix=/usr/local keeps the source build separate from system packages.

When configuration finishes, you should see output like this:

Updating makefile...
Now you must run 'make'.

Compile and (Optional) Test Perl

Compile the source code using all available CPU cores:

make -j$(nproc)

At the end of the build, you will see a message like this:

Everything is up to date. Type 'make test' to run test suite.

Optional but recommended: run the test suite before installing. It can take several minutes depending on your system:

make test

Install Perl

Install the compiled binaries into /usr/local:

sudo make install

If you skip make test, you may see a warning like this during installation:

WARNING: You've never run 'make test' or some tests failed! (Installing anyway.)
WARNING: You've never run 'make test'!!!  (Installing anyway.)

Verify the Source Installation

Confirm the source-compiled Perl version:

/usr/local/bin/perl -v

The output should show the version you compiled:

This is perl 5, version 42, subversion 0 (v5.42.0) built for x86_64-linux

Copyright 1987-2025, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

The source-compiled Perl installs to /usr/local/bin/perl. If /usr/local/bin appears before /usr/bin in your PATH, it will take precedence over the system Perl. Use the full path when you want to be explicit.

Source installs include a CPAN client at /usr/local/bin/cpan for managing modules with the source build.

Verify Perl with a Hello World Script

Create a Test Script

Create a simple Perl script in your working directory:

cat > hello.pl <<'EOF'
#!/usr/bin/env perl
use strict;
use warnings;

print "Hello, World!\n";
EOF

The shebang #!/usr/bin/env perl uses the first Perl in your PATH, which works for both DNF and source installs.

Run the Script

Make the file executable and run it:

chmod +x hello.pl
./hello.pl

You should see:

Hello, World!

Check Syntax (Optional)

Use a quick syntax check before running larger scripts:

perl -c hello.pl
hello.pl syntax OK

Troubleshooting Common Issues

Shebang Points to the Wrong Perl

If a script fails with a cannot execute: required file not found error, the shebang likely points to a Perl path that does not exist. Check the Perl path on your system:

command -v perl

DNF installs Perl to /usr/bin/perl, while source builds install to /usr/local/bin/perl. Update the first line of your script or use #!/usr/bin/env perl to let your PATH decide.

CPAN Module Compilation Failures

When module installation fails with missing headers or compiler errors, install the build prerequisites:

sudo dnf install perl-devel gcc make

The perl-devel package provides headers and libraries required for XS modules.

Missing Build Tools for Source Installation

If Configure reports that no C compiler is available, install the Development Tools group and re-run the configuration step:

sudo dnf groupinstall "Development Tools"

Update Perl on Rocky Linux

Update DNF-Installed Perl

Perl installed via DNF receives updates through the normal system update process. To update only Perl:

sudo dnf upgrade --refresh perl

If Perl is already at the latest version, DNF reports this:

Rocky Linux 10 - BaseOS                         4.7 kB/s | 4.3 kB     00:00
Rocky Linux 10 - AppStream                      4.7 kB/s | 4.3 kB     00:00
Rocky Linux 10 - Extras                         3.4 kB/s | 3.1 kB     00:00
Dependencies resolved.
Nothing to do.
Complete!

For routine maintenance, update all system packages at once (this includes Perl):

sudo dnf upgrade --refresh

Update Source-Compiled Perl

Option 1: Update Manually

Source-compiled Perl does not receive automatic updates. Check your current version:

/usr/local/bin/perl -e 'print $^V'
v5.42.0

Check the latest available version from perl.org:

curl -fsSL https://www.perl.org/get.html | grep -oE 'perl-[0-9]+\.[0-9]+\.[0-9]+' | head -n 1
perl-5.42.0

If a newer version is available, download and rebuild using the same steps from the source installation section:

mkdir -p ~/src/perl-build
cd ~/src/perl-build
PERL_TARBALL_URL=$(curl -fsSL https://www.perl.org/get.html | grep -oE 'https://www\.cpan\.org/src/5\.0/perl-[0-9]+\.[0-9]+\.[0-9]+\.tar\.gz' | head -n 1)
curl -fLO --progress-bar "$PERL_TARBALL_URL"
tar -xzf perl-*.tar.gz
cd perl-*/
./Configure -des -Dprefix=/usr/local
make -j$(nproc)
sudo make install

Optional: run make test after compilation and before installation if you want to validate the build.

Verify the updated version:

/usr/local/bin/perl -e 'print $^V'

If you want to clean up the build directory, double-check the path before removing it. Example: rm -rf ~/src/perl-build/perl-* ~/src/perl-build/perl-*.tar.gz.

Option 2: Use an Update Script

For convenience, you can create a reusable script that checks the latest version, downloads it, and installs it.

Create the script file:

cat > ~/update-perl.sh <<'EOF'
#!/usr/bin/env bash
set -euo pipefail

INSTALL_DIR="/usr/local"
PERL_BIN="$INSTALL_DIR/bin/perl"

REQUIRED_CMDS=(curl tar make gcc)
for cmd in "${REQUIRED_CMDS[@]}"; do
    if ! command -v "$cmd" > /dev/null 2>&1; then
        echo "Error: $cmd is required but not installed."
        exit 1
    fi
done

if [[ $EUID -ne 0 ]]; then
    if ! command -v sudo > /dev/null 2>&1; then
        echo "Error: sudo is required to install into $INSTALL_DIR."
        exit 1
    fi
    SUDO="sudo"
else
    SUDO=""
fi

if [[ -x "$PERL_BIN" ]]; then
    CURRENT="$("$PERL_BIN" -e 'print $^V')"
else
    CURRENT="not installed"
fi

LATEST_URL=$(curl -fsSL https://www.perl.org/get.html | grep -oE 'https://www\.cpan\.org/src/5\.0/perl-[0-9]+\.[0-9]+\.[0-9]+\.tar\.gz' | head -n 1)
if [[ -z "$LATEST_URL" ]]; then
    echo "Error: Could not detect the latest Perl version."
    exit 1
fi

LATEST_TARBALL="${LATEST_URL##*/}"
LATEST_VERSION="v${LATEST_TARBALL#perl-}"
LATEST_VERSION="${LATEST_VERSION%.tar.gz}"

echo "Current installed version: $CURRENT"
echo "Latest available version: $LATEST_VERSION"

if [[ "$CURRENT" == "$LATEST_VERSION" ]]; then
    echo "Perl is already up to date."
    exit 0
fi

read -rp "Proceed with update? [y/N] " confirm
if [[ "$confirm" != "y" ]]; then
    echo "Update cancelled."
    exit 0
fi

WORKDIR=$(mktemp -d -p "$HOME" perl-build-XXXXXXXX)
trap 'rm -rf "$WORKDIR"' EXIT

cd "$WORKDIR"
echo "Downloading $LATEST_TARBALL..."
curl -fLO --progress-bar "$LATEST_URL"

tar -xzf "$LATEST_TARBALL"
cd perl-*/

./Configure -des -Dprefix="$INSTALL_DIR"
make -j"$(nproc)"
$SUDO make install

echo "Update complete."
"$PERL_BIN" -e 'print $^V'
echo
EOF

Make the script executable:

chmod +x ~/update-perl.sh

Run the script whenever you want to check for and apply updates:

~/update-perl.sh

Remove Perl from Rocky Linux

Remove DNF-Installed Perl

If you installed Perl via DNF and need to remove it along with cpanm and development headers:

sudo dnf remove perl perl-App-cpanminus perl-devel

Removing Perl may affect other packages that depend on it. DNF will list dependent packages before removal, so review the list carefully.

Remove Source-Compiled Perl

For Perl compiled from source (installed to /usr/local), remove the installation manually:

The following commands permanently delete the source-compiled Perl installation, including all modules installed to /usr/local/lib/perl5. Back up any custom data you want to keep.

sudo rm -rf /usr/local/bin/perl*
sudo rm -rf /usr/local/lib/perl5/
sudo rm -rf /usr/local/share/man/man1/perl*
sudo rm -rf /usr/local/share/man/man3/

Remove the source build directory if you no longer need it:

Double-check the path before removing build artifacts. Example: rm -rf ~/src/perl-build/perl-* ~/src/perl-build/perl-*.tar.gz.

Remove CPAN User Data

cpanm stores build logs and downloaded source archives in your home directory. To remove this data:

These commands permanently delete your per-user CPAN caches. Back up anything you want to keep before removing them.

rm -rf ~/.cpanm/
rm -rf ~/.cpan/

Conclusion

You now have Perl installed via DNF or from source, along with the tooling to manage modules and validate scripts. Use perl -c to check syntax, perldoc for built-in documentation, and explore MetaCPAN when you need additional modules. For development workflows, pair Perl with Git on Rocky Linux to track your scripts.

Leave a Comment