Install Perl on Ubuntu
| Is Perl already there | |
| Install from apt | |
| cpanm and apt modules | |
| perlbrew for newer versions |
Is Perl already there
Ubuntu ships Perl because parts of the system use it. Before installing anything, ask what you already have:
perl -v
This is perl 5, version 38, subversion 2 (v5.38.2) built for x86_64-linux-gnu-thread-multi
If you see a version, you can start the course right now. The minimum for every example is 5.26, and anything newer runs the modern syntax from lesson 49.
Also check where the binary lives. A version installed by hand in /usr/local/bin can shadow the system one, and then two terminals disagree about which Perl they run:
which perl
/usr/bin/perl
Install from apt
The perl package is enough to run scripts, but the documentation and the standard tooling live in separate packages. Refresh the list and install them together:
sudo apt update
sudo apt install perl perl-doc
perl-doc is what makes perldoc perlintro and perldoc -f open work offline. It is a large download, and it pays for itself the first time you need to look up a function signature without a browser.
cpanm and apt modules
Ubuntu packages many CPAN modules with a lib...-perl name. Installing them with apt keeps them updated by the system, which is convenient on a server:
sudo apt install libjson-pp-perl libtext-csv-perl
For anything not packaged, use cpanm. Install it once from apt, then it manages modules in the usual CPAN way:
sudo apt install cpanminus
cpanm HTTP::Tiny
Running cpanm with sudo installs into the system Perl, which mixes your modules with the distribution's. Prefer a local library for project work, as lesson 32 shows, and keep the system Perl for system scripts.
perlbrew for newer versions
The Perl in the Ubuntu release is frozen when the release ships. When you need a newer one, perlbrew builds it in your home folder and leaves the system copy untouched:
curl -L https://install.perlbrew.pl | bash
The installer prints one line to add to your shell profile. After a new shell, build and switch:
perlbrew install perl-5.40.0
perlbrew switch perl-5.40.0
Building takes a few minutes and needs a compiler and the development headers. If the build fails at the first step, install build-essential and try again.
One caution: scripts that start with #!/usr/bin/perl keep using the system Perl even when your shell prefers the perlbrew one. For a shebang that follows your choice, use #!/usr/bin/env perl.
Next: Install Perl on Rocky Linux
Article author: Arthur Isaev