How to Install Perl on Ubuntu 24.04, 22.04 or 20.04

Perl is a high-level, general-purpose programming language known for its powerful text processing capabilities and versatility across different domains, including system administration, web development, and network programming. Perl’s extensive library of modules and its support for various platforms make it a valuable tool for developers and administrators alike.

On Ubuntu 24.04, 22.04, or 20.04, Perl can be installed using two primary methods. The first method involves using Ubuntu’s default APT repository, which offers a straightforward and stable installation of Perl. The second method is to download the Perl source archive and manually configure, compile, and install the latest version. This approach allows you to access the most recent features and optimizations. This guide will walk you through both methods, ensuring that you have the right Perl setup for your needs.

Method 1: Install Perl via APT

Checking if Perl is Installed

Before installing Perl, checking if it has already been installed on your Ubuntu system is essential. To do this, open a terminal window and type the following command:

perl -v

The command will display its version number if Perl is installed on your system. If not, the terminal will display an error message.

Update Ubuntu Before Perl Installation

Before installing Perl, you must update your Ubuntu system’s package repository. This will ensure you download the latest version of Perl and any dependencies it may require. To do this, run the following command:

sudo apt update

This command will update the package repository on your Ubuntu system.

Install Perl via APT Command

Once you have updated the package repository on your Ubuntu system, you can install Perl using the following command:

sudo apt install perl

This command will download and install the latest version of Perl and any dependencies it requires.

Install Perl Additional Modules

Alternatively, when installing Perl on Ubuntu, you can install additional packages that provide more development tools and debugging options or use libraries to improve your Perl experience. Here are some of the most popular packages you may want to consider:

  • perl-doc: This package contains the official Perl documentation, which includes reference manuals, tutorials, and other helpful resources for learning and using Perl.
  • libperl-dev: This package provides development files and libraries that can be used to compile and link Perl modules and extensions.
  • libdbd-mysql-perl: This package provides a Perl interface to MySQL databases, making interacting with MySQL from your Perl scripts easy.
  • libdatetime-perl: This package provides a collection of modules that can be used to manipulate dates and times in Perl. It includes support for time zones, daylight saving time, and other common date/time operations.
  • libjson-perl: This package provides a Perl module for encoding and decoding JSON data, a popular data interchange format used in many web applications.
  • libxml-simple-perl: This package provides a simple API for parsing and manipulating XML data in Perl. It is often used in web applications and other systems that need to work with XML data.
  • libtest-simple-perl: This package provides a simple framework for writing and running Perl unit tests. It can be used to ensure that your Perl code is working correctly and to catch bugs before they become problems.

To install any of these packages, add their names to the apt-get install command when installing Perl. For instance, to install Perl along with the libdatetime-perl and libjson-perl packages, run the following command:

sudo apt install perl libdatetime-perl libjson-perl

This command will install Perl and the optional packages you specified. You can then use these packages in your Perl scripts to improve functionality and productivity.

Search For Perl Modules

In addition to the optional Perl packages we have mentioned, there are many other Perl packages available on Ubuntu that you may find helpful. To search for additional Perl packages, you can use the apt-cache command, which is used to search the package repository on Ubuntu.

To search for Perl packages using apt-cache, open a terminal window and type the following command:

apt-cache search perl

This command will display a list of Perl packages available in the Ubuntu package repository. You can scroll through the list to see the available packages or use the grep command to search for specific packages.

For example, if you want to search for packages related to MySQL, you can use the following command:

apt-cache search perl | grep mysql

This command will display a list of Perl packages with the word “mysql” in their name or description. You can use similar commands to search for other keywords or specific packages that you may be interested in.

Once you have found a package you want to install, you can use the apt-get command to install it. For example, to install the libdbd-mysql-perl package that we mentioned earlier, you can run the following command:

sudo apt install libdbd-mysql-perl

This command will download and install the package and any dependencies it requires. You can then use the package in your Perl scripts as needed.

Method 2: Install Perl via Perl Source Archive

The guide will demonstrate a secondary method for installing Perl: downloading the source archive, compiling the binary, and installing it. This method is best if you need a specific version of Perl or an updated version.

Install Prerequisite Packages to Build Perl

Preparing your Ubuntu system with the necessary development tools and libraries is crucial before initiating the Perl installation process. These components are pivotal in facilitating the smooth compilation of Perl from the source.

To install these prerequisite packages, execute the command below:

sudo apt install build-essential make wget

Download Perl Source Code Archive

With the dependencies in place, proceed to download Perl’s source code. The official Perl website is a reliable source for obtaining the most up-to-date code version. In this guide, we’ll use Perl version 5.38.0 for illustration purposes.

Run the following command to download the specified version:

wget https://www.cpan.org/src/5.0/perl-5.38.0.tar.gz

Note: The version used here serves as an example. Always refer to the official website to download the latest version of Perl source code.

Extract Perl Source Code From the Archive

Having successfully downloaded the source code, the subsequent step involves extracting the tarball. Execute the command below to unpack the source code:

tar -xzf perl-v5.38.0.tar.gz

Then, navigate into the directory housing the extracted source code:

cd perl-5.38.0

Setup Perl Build Configuration

Before compiling the source code, it’s imperative to set up the build configuration. This preparatory step tailors the Perl compilation process to your Ubuntu system’s specific needs and characteristics.

Run the command below to configure the build:

./Configure -des -Dprefix=/usr/local

Note: Ensure to use ‘-Dprefix’ exactly as shown, not ‘-dprefix’. This command initializes the build with default settings and designates /usr/local as the installation directory for Perl.

Compile and Install Perl Binary

With the configuration in place, you are set to compile the Perl source code. The duration of this step is contingent on your system’s capabilities. Initiate the compilation process with the command below:

make

Upon successful compilation, proceed to install Perl using the following command:

sudo make install

Validate Perl Installation

Verifying that Perl has been successfully installed from the source is prudent to wrap up the installation process. Run the command below to check the installed Perl version:

perl -v

This command will unveil the Perl version number, affirming the successful installation.

Installing Perl from the source accords unparalleled control over the configuration and compilation parameters. This approach is particularly advantageous if your project demands a specific Perl version or requires the activation (or deactivation) of specific features not in the pre-compiled packages available in the Ubuntu repositories.

Create a Perl Test Application

You can create a simple “Hello World” script to test Perl on your Ubuntu system. Open a terminal window and type the following command:

nano hello.pl

This command will open the Nano text editor, where you can enter the following code:

#!/usr/bin/perl
print "Hello, world!\n";

This code will print “Hello, world!” to the terminal.

Save the file by pressing Ctrl+O, then exit the editor by pressing Ctrl+X. Make the script executable by running the following command:

chmod +x hello.pl

This command will make the script executable.

Now, run the script by typing the following command:

./hello.pl

This command will execute the “Hello World” script and print the message “Hello, world!” to the terminal.

Conclusion

With Perl installed on your Ubuntu system, whether through the default APT repository or by compiling from source, you can leverage its powerful capabilities for a wide range of programming tasks. The APT repository offers convenience and stability, while compiling from source provides the latest features and greater control over the installation process. Regularly updating your Perl installation will ensure optimal performance and access to the latest improvements, making your development and scripting work on Ubuntu efficient and effective.

Leave a Comment