Perl5 is a full-featured programming language. It is used for all types of production projects, including commercial business systems around the world. Perlbrew is an installation and version switching tool for Perl5. You can install, delete, and switch between Perl versions.
Perlbrew installs Perl in your home directory, and each version of Perl you install is independent of other versions. This allows you to test your code against multiple Perl versions without interfering with any other versions or effects. This includes the default Perl package for your operating system, which is usually older than the latest stable version.
In this tutorial, you will install Perlbrew and use it to install and test the version of Perl5.
To start this tutorial, you need a CentOS 7 server, including a non-root user and firewall that can use the sudo command, we recommend that you use Tencent Cloud free Developer Lab for experimentation, or Click here to purchase a server. Regarding firewall configuration, you can refer to Tencent Cloud's Security Group for configuration.
Before installing Perlbrew, you need some prerequisite packages. Perlbrew needs gcc
and bzip2
to compile and install Perl. By default, these packages will not be installed on CentOS 7.
Two packages related to gcc are needed to build Perl, libgcc
and gcc
. To see if you already own these packages, execute the following command:
rpm -qa | grep gcc
If it is installed, you will see the following output:
libgcc-4.8.5-11.el7.x86_64
gcc-4.8.5-11.el7.x86_64
CentOS 7 only includes the libgcc
package by default, so you can install gcc
with the following command:
sudo yum install gcc
You also need the bzip2-libs
and bzip2
packages. Check if they are also installed:
rpm -qa | grep bzip2
If they are all installed, you will see the following:
bzip2-libs-1.0.6-13.el7.x86_64
bzip2-1.0.6-13.el7.x86_64
By default, CentOS 7 only includes the bzip2-libs
package. Add the bzip2
package:
sudo yum install bzip2
Finally, install the patch
utility.
sudo yum install patch
When you are ready, you can install Perlbrew. Download the installation script to your server:
curl -L https://install.perlbrew.pl -o install.perlbrew.pl
To review the contents of the script before running it, open it in a text editor to view its contents:
vi install.perlbrew.pl
Once you are satisfied with the contents of the script, pass the script to bash to run the installation script:
cat install.perlbrew.pl | bash
This will create a new directory structure in /home/sammy/perl5
where Perlbrew will store its supporting files and Perl version. You will see the following output in the installation script:
## Download the latest perlbrew
## Installing perlbrew
perlbrew is installed:~/perl5/perlbrew/bin/perlbrew
perlbrew root(~/perl5/perlbrew) is initialized.
Append the following piece of code to the end of your ~/.bash_profile and start a
newshell, perlbrew should be up and fully functional from there:
source ~/perl5/perlbrew/etc/bashrc
Simply run `perlbrew`for usage details.
Happy brewing!
## Installing patchperl
## Done.
Next, use the perlbrew
tool to create some initial configuration files and directories in /home/sammy/perl5/perlbrew
:
~ /perl5/perlbrew/bin/perlbrew self-install
You will see the following output:
You are already running the installed perlbrew:/home/sammy/perl5/perlbrew/bin/perlbrew
Perlbrew is now installed, but you need to modify the shell configuration file to make it easier to use.
.bash_profile
to include PerlbrewBefore installing the Perl version with Perlbrew, you should edit the .bash_profile
file to automatically set some important environment variables. Perlbrew does this by including the required code in another profile in .bash_profile
that you can include.
Open the file ~/.bash_profile
in the editor:
vi ~/.bash_profile
Add the following line at the bottom of the file to include Perlbrew settings:
source ~/perl5/perlbrew/etc/bashrc
Save the file and exit the editor.
Then log out and log in again to ensure that your .bash_profile
file loads Perlbrew's settings. It now adds /home/sammy/perl5/perlbrew/bin
to the front of the environment variable PATH
and sets some other environment variables needed by Perlbrew.
Verify that these environment variables have been set by running the env command, and use grep to filter the resulting text PERL:
env | grep PERL
You should see something similar to the following:
PERLBREW_BASHRC_VERSION=0.78
PERLBREW_ROOT=/home/sammy/perl5/perlbrew
PERLBREW_HOME=/home/sammy/.perlbrew
These environment variables tell Perlbrew where the directory is located. Entering which perlbrew
should recognize the full path of the perlbrew
command:
which perlbrew
You should see the following in the terminal:
~ /perl5/perlbrew/bin/perlbrew
Now that Perlbrew is installed and configured, let's start using it.
Let's use Perlbrew to install a stable version of Perl5. Use the perlbrew command to view the Perl version available for installation:
perlbrew available
You will see something similar to the following partial list:
perl-5.25.11
perl-5.24.1
perl-5.22.3
perl-5.20.3
perl-5.18.4...
Odd-numbered versions are under development, such as perl-5.25
, they are under development, and it is not stable enough. Generally, unless your code requires an older version, please don't use an older version such as 5.10.1
.
According to the output, perl-5.24.1
is the latest stable version because it has the largest even major number. You can choose any Perl version shown in the list, but for this tutorial, we will install perl-5.24.1
.
Use perlbrew install
to install it:
perlbrew install perl-5.24.1
Perl installation may take a long time to compile and install, usually about 20 minutes, so don't interrupt the compilation process. If you want to view the progress of the build, you can open a separate terminal session and use tail -f \~/perl5/perlbrew/build.perl-5.24.1.log
to monitor the build log.
After the build is complete, you will see the following output from Perlbrew:
perl-5.24.1 is successfully installed.
After the build is complete, the last line of the build log file will be:
##### Brew Finished #####
You can repeat this process for each version of Perl you want to install. This tutorial only demonstrates one version installed, but you can repeat this step as needed.
Next, let's see how to use Perlbrew to handle multiple versions of Perl.
At this point, there are two versions of Perl on your system: the version provided with the operating system, and the version of Perl you just installed in Perlbrew's ~/perl5
directory.
To use the new Perl installation, run the following command:
perlbrew use perl-5.24.1
This command updates the PERLBREW_PERL
environment variable to point to the Perl version of the current login session.
If you want to use the Perl version every time you log in, run the following command:
perlbrew switch perl-5.24.1
This command sets the PERLBREW_PERL environment variable to point to the specified Perl version every time you log in.
Verify that you are using Perl 5.24.1:
perl -V
You should see the following output:
... %ENV:
PERLBREW_BASHRC_VERSION="0.78"
PERLBREW_HOME="/home/sammy/.perlbrew"
PERLBREW_MANPATH="/home/sammy/perl5/perlbrew/perls/perl-5.24.1/man"
PERLBREW_PATH="/home/sammy/perl5/perlbrew/bin:/home/sammy/perl5/perlbrew/perls/perl-5.24.1/bin"
PERLBREW_PERL="perl-5.24.1"
PERLBREW_ROOT="/home/sammy/perl5/perlbrew"
PERLBREW_VERSION="0.78"
@ INC:/home/sammy/perl5/perlbrew/perls/perl-5.24.1/lib/site_perl/5.24.1/x86_64-linux
/home/sammy/perl5/perlbrew/perls/perl-5.24.1/lib/site_perl/5.24.1/home/sammy/perl5/perlbrew/perls/perl-5.24.1/lib/5.24.1/x86_64-linux
/home/sammy/perl5/perlbrew/perls/perl-5.24.1/lib/5.24.1.
Perlbrew only installs the core Perl code. To see which modules form the core of a specific Perl version, execute the following command:
corelist -v 5.24.1
After using the perlbrew use
or perlbrew switch
command, you can use the perlbrew off
command to return to using the vendor version of Perl. If you have used perlbrew switch
to set a new default Perl, you can use perlbrew switch-off
to delete the default setting.
The perlbrew
command issued without a flag will generate a simple list of useful commands. The command perlbrew help
generates more detailed help information. Let's install some additional Perl modules.
Perl provides a large number of common code modules that extend the core language. These modules are stored in the Comprehensive Perl Archive Network (CPAN). You can test whether your Perl is available through CPAN.
App ::cpanminus is a Perl module that allows you to browse the CPAN repository and download modules. Let's install this module and use it to test your Perl installation. Make sure you are using the new Perl to install:
perlbrew use perl-5.24.1
Install the cpanminus module using the following command:
curl -L https://cpanmin.us | perl - App::cpanminus
You will see the following output:
- - > Working on App::cpanminus
Fetching http://www.cpan.org/authors/id/M/MI/MIYAGAWA/App-cpanminus-1.7043.tar.gz ... OK
Configuring App-cpanminus-1.7043... OK
Building and testing App-cpanminus-1.7043... OK
Successfully installed App-cpanminus-1.70431 distribution installed
Verify that the cpanminus module now exists:
perlbrew list-modules
You should see App::cpanimus
in the output:
App::cpanminus
Perl
You can now use the command line cpanm
to install other modules and their dependencies. Let's use it to install the Email::Simple
module. If you are writing some Perl code to send emails, you need to use this module.
cpanm Email::Simple
After the installation is complete, check the module list again:
perlbrew list-modules
You will see the following output:
App::cpanminus
Email::Date::Format
Email::Simple
Perl
The Email::Simple
module requires the Email:Date::Format
module. The cpanm program automatically installs the dependencies for you.
In this tutorial, you installed Perlbrew and used it to install Perl in your home directory. You also learned how to use Perlbrew to install and manage multiple versions of Perl, and how to use the cpanm utility to install other modules from the CPAN repository. You can use the same process to install different versions of Perl5 so that you can install the version required by the application. Have you learnt that? For more Linux tutorials, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn, thank you for reading.
Reference: "How to Install Perlbrew and Manage Multiple Versions of Perl 5 on CentOS 7"
Recommended Posts