perlbrew - Installation of Perl on User Directries 2022
Tags: perl, perlbrew, biginners, webdev
Published: 2022-04-13 08:07:23

[perlbrew](https://perlbrew.pl/) is a command line tool to intall Perl on user directries. You can install any version of Perl on your user directries and use it.

This article is originally <a href="https://en.perlzemi.com/blog/20110220130464.html">perlbrew - Installation of Perl on User Directries | Perl ABC</a>

### Installation of perlbrew

Execute the following command to install perlbrew.

<pre>
curl -L http://install.perlbrew.pl | bash
</pre>

If you don't have curl command, use wget command or fetch command.

<pre>
# Linux
wget -O - http://install.perlbrew.pl | bash

# FreeBSD
fetch -o- http://install.perlbrew.pl | sh
</pre>

You can also install perlbrew from [CPAN](https://en.perlzemi.com/blog/20100504127089.html).

<pre>
sudo cpan App::perlbrew
perlbrew init
</pre>

#### bash

Add the perlbrew settings to "~/.bashrc".

<pre>
echo "source ~/perl5/perlbrew/etc/bashrc" >> ~/.bashrc
</pre>

Reload "~/.bashrc".

<pre>
source ~/.bashrc
</pre>

#### csh

Add the perlbrew settings to "~/.cshrc".

<pre>
echo "source ~/perl5/perlbrew/etc/cshrc" >> ~/.cshrc
</pre>

Reload "~/.cshrc".

<pre>
source ~/.cshrc
</pre>

### Installtion of Perl using perlbrew

You can check the available Perls using **available** command.

<pre>
# Show the available Perls
perlbrew available
</pre>

Let's install a Perl using **install** command. The latest Perl version for 2022 is v5.34.1.

<pre>
# Install Perl
perlbrew install perl-5.34.1
</pre>

If the installation of Perl fails, check the following commands are installled. These commands are needed to install Perl.

*  make 
* [gcc](https://en.linux.perlzemi.com/blog/20201216103956.html)
*  patch 

Show the list of installed Perls using **list** command.

<pre>
# Show the list of installed Perls
perlbrew list
</pre>

The installed Perls are displayed.

<pre>
  perl-5.34.1
</pre>

And switch to the installed Perl.

<pre>
perlbrew switch perl-5.34.1
</pre>

And show the installed Perls again. 

<pre>
# Show the installed Perls
perlbrew list
</pre>

"*" is added to the head of the current Perl.

<pre>
* perl-5.34.1
</pre>

And see the Perl version.

<pre>
perl -v
</pre>

The version of current Perl is displayed.

<pre>
This is perl 5, version 34, subversion 1 (v5.34.1) built for x86_64-linux
</pre>

### Installation of cpanm

[cpanm](https://en.perlzemi.com/blog/20101027127859.html) can be installed using **install-cpanm** command.

<pre>
perlbrew install-cpanm
</pre>

And install a module.

<pre>
cpanm JSON
</pre>

Modules are installed on the following directory in my case.

<pre>
/home/kimoto/perl5/perlbrew/perls/perl-5.34.1/lib/site_perl/5.34.1/
</pre>

### Returning back to System Perl

If you want to returning back to system Perl, use **off** command.

<pre>
perlbrew off
</pre>

<!--

<h3>How to use the version of Perl specified by perlbrew with crontab</h3>

One of the main points of perlbrew is that the version of Perl specified by perlbrew is not used by cron. This is because .bash_profile is not loaded when running from cron because the shell is not started. The setting by perlbrew is described in .bash_profile.

The simplest solution is to rewrite PATH in crontab to the value of the environment variable PATH that the user has.

First, as the user who installed perlbrew, display the value of the PATH environment variable.

<pre>
env | grep PATH
</pre>

You will see some environment variable that include PATH, so look for something called PATH. (This is an example when the user name is admins.)

<pre>
PATH =/home/admins/perl5/perlbrew/bin:/home/admins/perl5/perlbrew/perls/perl-5.16.2/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/admins/bin
</pre>

Edit crontab with the following command.
<pre>
crontab -e
</pre>

Then write the above PATH value to the top of crontab.

<pre>
PATH =/home/admins/perl5/perlbrew/bin:/home/admins/perl5/perlbrew/perls/perl-5.16.2/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/admins/bin

# cron settings continue ...
</pre>

Now cron will use the specified version of Perl.

<h4>First line description of Perl script</h4>

Write the first line of the Perl script using the env command as shown below.

<pre>
# !/usr/bin/env perl
</pre>

This will automatically detect perl from the set PATH value.

As you can see, this method solidifies the PATH, so crontab is not portable at all. Since the user name admins is directly embedded and the version number of perl is written as it is, it will not work when changing the version with perlbrew.

But I think this method is the best. The reason is that it is difficult to make it portable because there is a limitation that environment variable cannot be expanded in crontab. Portability increases the amount of description, requires additional scripts, and complicates management.

Also, no matter how hard you try, crontab usually has some differences depending on the environment, so even if you keep portability, there may not be much merit.

-->