plenv - Installation of Perl on user directories 2022
Tags: perl, plenv, beginners, cpanm
Published: 2022-04-14 08:29:17

[plenv](https://github.com/tokuhirom/plenv) 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/20180816153437.html">plenv - Installation of Perl on user directories | Perl ABC</a>

### Installation of plenv

Execute the following command to install plenv.

<pre>
git clone https://github.com/tokuhirom/plenv.git ~/.plenv
git clone https://github.com/tokuhirom/Perl-Build.git ~/.plenv/plugins/perl-build/
</pre>

Next, let's pass the path of plenv. Add the configuration file for bash provided by plenv to ".bash_profile" (for bash).

Execute the following command.

<pre>
echo 'export PATH="$HOME/.plenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(plenv init -)"' >> ~/.bash_profile
exec $SHELL -l
</pre>

### Installtion of Perl using plenv

You can check the available Perls using **install** command and **-l** option.

<pre>
plenv install -l
</pre>

If plenv is installed successfully, you will see a list of Perls you can install.

<pre>
Available versions: Available versions:
 5.35.10
 5.35.9
 5.35.8
 5.35.7
 5.35.6
 5.35.5
 5.35.4
 5.35.3
 5.35.2
 5.35.1
 5.35.0
 5.34.1
  ...
</pre>

Install Perl in your environment. Let's install Perl of 5.34.1. It will take some time, so wait(about 40 minutes).

<pre>
plenv install 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 **versions** command.

<pre>
# Show the list of installed Perls
plenv versions
</pre>

When Perl is available

The installed Perls are displayed.

<pre>
* system(set by /home/kimoto/.plenv/version)
  5.34.1
</pre>

And switch to the installed Perl using the **global** command.

<pre>
plenv global 5.34.1
</pre>

And show the installed Perls again. 

<pre>
# Show the list of installed Perls
plenv versions
</pre>

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

<pre>
  system(set by /home/kimoto/.plenv/version)
* 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>
plenv install-cpanm
</pre>

And install a module.

<pre>
cpanm JSON
</pre>

### Returning back to System Perl

If you want to returning back to system Perl, switch to **system**.

<pre>
plenv global system
</pre>

<!--

<h3>How to use Perl installed with plenv with crontab</h3>

One of the main points of plenv is that Perl installed with plenv 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 plenv 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 plenv, 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/kimoto2/.plenv/shims: /home/kimoto2/.plenv/bin:/home/kimoto2/.plenv/shims:/home/kimoto2/.plenv/bin:/usr/local/bin:/ bin:/usr/bin:/usr/local/sbin: /usr/sbin:/home/kimoto2/.local/bin:/home/kimoto2/bin:/home/kimoto2/.local/bin:/home/kimoto2 /bin:/home/kimoto2/.local/bin:/home/kimoto2/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/kimoto2/.plenv/shims: /home/kimoto2/.plenv/bin:/home/kimoto2/.plenv/shims:/home/kimoto2/.plenv/bin:/usr/local/bin:/ bin:/usr/bin:/usr/local/sbin: /usr/sbin:/home/kimoto2/.local/bin:/home/kimoto2/bin:/home/kimoto2/.local/bin:/home/kimoto2 /bin:/home/kimoto2/.local/bin:/home/kimoto2/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 is completely non-portable to crontab. The user name kimoto2 is directly embedded.

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.

-->