If I have both /usr/bin/perl and /usr/local/bin/perl available on a system, which one should I use?


It depends: on some systems, e.g. BSD packaging, /usr/local/bin may be the directory where packages install executables. In that case, it would be newer (and perhaps better), but /usr/bin/perl would be more stable.

You can use perl -V to see which has a newer version number. Sometimes the contents of /usr/local/bin (on multi-user systems) isn't really newer.

The usual compromise is to let the user decide, by using env in the shebang/hashbang line (but again, the path to env can vary by system). The shebang line refers to the first line of a script, which may look like one of the following:

#!/usr/bin/perl -w
#!/usr/bin/env perl

The first two specify the directory from which to run Perl. As a bonus, this form allows passing an option, e.g. -w (used to turn on warnings). The last line uses the env program to pick the first occurrence of perl by searching the directories listed in the user's PATH environment variable.

It is also possible to run a script like this:

perl myscript

which (like env) tells the shell to pick the first occurrence of perl from the directories listed in PATH.

An explicit

./perl myscript

of course overrides the PATH. But scripts with shebang lines are the way most people use Perl scripts (Perl one-liners are a different matter).

Further reading:

  • If I run perl from my home directory, which perl will be executed? – CJ7 Feb 15 '16 at 0:44
  • 1
    @CJ7 Run type perl or command -v perl to find out – muru Feb 15 '16 at 1:11
  • 1
    @CJ7 as Thomas has already said, the PATH environment variable. – muru Feb 15 '16 at 1:24
  • 2
    This is the first time I've heard of the shebang referred to as a "hash-tag line"... – Bob Feb 15 '16 at 3:20
  • 1
    hashtag is a neologism when used in the sense @Bob is familiar. It's derived from one sense of the use of hashmark (with which some commenters also are apparently unfamiliar). For examining the PATH, I use a script except when explaining how PATH works. ymmv. – Thomas Dickey Feb 15 '16 at 18:16

Who is it for? If it is just you, as long as both your versions of perl are new enough it does not matter. Is it for a work environment? If so check your deployment policies as they may have the answer. If it is for distribution to the general public, I would recommend varying based upon package type, specifically use /usr/bin/perl for debs (per the debian guidelines) and rpms (as Red Hat specifically disallows rpms to install to /usr/local), but /usr/bin/env perl for tar and shar packages as it has the best chance of working on random unix computers.

Not the answer you're looking for? Browse other questions tagged or ask your own question.