Install Perl on FreeBSD

Perl in the base system

FreeBSD includes Perl in the base system, because the ports tree and several system scripts are written in it. There is nothing to install to start the course:

perl -v

This is perl 5, version 36, subversion 3 (v5.36.3) built for amd64-freebsd-thread-multi

The base Perl belongs to the release, and the project keeps it stable rather than current. That makes it a dependable target — and it means the version will not move until you upgrade FreeBSD itself.

A newer Perl from packages

The ports collection carries a current Perl as perl5. It installs beside the base one, with a versioned name, so nothing that ships with the system breaks:

sudo pkg install perl5

perl5.40 -v

This is perl 5, version 40, subversion 0 (v5.40.0) built for amd64-freebsd-thread-multi

To make the new one the default for your shell, put its folder first in PATH or create a symlink in /usr/local/bin. Check with which -a perl that you know every copy the shell can see.

Modules and cpanm

FreeBSD packages CPAN modules with a p5- prefix. Install the ones this course uses from packages, and they arrive compiled and tracked by the package manager:

sudo pkg install p5-JSON-PP p5-Text-CSV

For modules that are not packaged, install cpanm. The package is called p5-App-cpanminus:

sudo pkg install p5-App-cpanminus

cpanm HTTP::Tiny

A package install puts modules in the system Perl, which is fine on a personal machine. On a server, keep project modules in a local library so a package upgrade cannot change the code your application loads.

perlbrew in a home folder

For a Perl that is neither the base one nor the packaged one, perlbrew builds any release under your home folder. It needs the compiler and the GNU toolchain:

sudo pkg install gmake gcc

curl -L https://install.perlbrew.pl | bash

perlbrew install perl-5.40.0

The installer prints a line for your shell profile. After a new login, perlbrew switch perl-5.40.0 makes that build the default for everything the shell starts.

Inside a jail, perlbrew works the same way, but the build tools must be installed in the jail. For a throwaway environment, the Perl in Docker page is faster than a fresh jail — the image already contains a complete Perl and cpanm.

Next: Perl in Docker

Article author: Arthur Isaev