## 41.3. Compiling Perl from Scratch

If you don't have Perl already or you'd like to install the latest version, you have a few options. The first is to get a precompiled version for your platform. This is an option of last resort, since you lose the opportunity to configure Perl for your system. Most Unix systems will compile the Perl source code cleanly.

To compile Perl, you will need to fetch the latest Perl source for the Comprehensive Perl Archive Network (CPAN) (Section 41.11). You can find the gzipped tar archive of the source code at http://www.cpan.org/src/stable.tar.gz. The archive is several megabytes, so those on a slow modem link need to plan accordingly. Unpack the archive with the following command:

>     $ gzip -dc stable.tar.gz | tar xvf -

You should now have a new subdirectory called perl-X.Y.Z (whatever the current version of Perl is). Change into this directory and you will be be ready to configure the build process for perl.

Like many Unix utilities, compiling Perl requires configuring a Makefile and then executing make. The Perl source comes with a robust Configure shell script that will prompt you to confirm information it finds about your system. Often, all the defaults are fine so you can tell the Configure not to prompt you for confirmation by passing the `-de` flag. If all goes well with the configuration stage, you'll want to start compiling the source with make. These steps can be effectively combined into to following idiom:

>     $ ./Configure -de && make test

Recall that the double ampersand is a kind of flow control operator in the shell that allows the make to happen only if the Configure succeeds. The Perl source comes with a test suite that attempts to verify that the build went according to plan. Since the test suite needs perl to be built, this command is similiar to typing:

>     $ ./Configure -de && make && make test

The configuration stage may report missing libraries (like those needed to make NDBM files or read shadowed password files). Generally, these messages are harmless. If an important dependency is missing, the Configure script will halt. You will need to read the error message to figure out what's missing from your system that Perl requires. Generally, Perl will configure and compile without much intervention from you.

If the `make test` command succeeds, you are ready to install your new Perl. Typically, installation requires administrative privileges since you'll be writing files in /usr/local (the default installation root). One way to do this is to use the su command like this:

>     $ su -c 'make install'

This will prompt you for root's password. During the installation process, you will be asked if you want Perl installed as /usr/bin/perl. On a system that didn't have Perl to begin with, you can safely answer yes to this question. On a system that already had Perl, you might wish to answer no here. The new Perl interpreter will still be installed in /usr/local/bin/perl. You should now have the latest version of Perl on your system. Use /path/to/newly_installed/perl -v to verify this.

-- JJ

Copyright © 2003 O'Reilly & Associates. All rights reserved.