さくらのレンタルサーバーで perlbrew を使って任意の Perl をインストールする

事前準備として

独自にローカルのディレクトリに CPAN モジュールをインストールして使っているような場合は、それらを使うのに必要だったパスを削除しておくこと。
初めてさくらのレンタルサーバーを使う場合は、特に必要ない。

capnminus をインストールする

まずは、cpanminus を保存するディレクトリを用意して、cpanminus をインストールする。

% mkdir -p ~/local/bin
% cd ~/local/bin
% wget wget --no-check-certificate https://github.com/miyagawa/cpanminus/raw/master/cpanm
% chmod u+x cpanm

これでインストールが完了。cpanminus は頻繁にアップデートされるので、cpanminus を更新する場合は下記のコマンドを実行する。

% ./cpanm --self-upgrade
--> Working on App::cpanminus
Fetching http://search.cpan.org/CPAN/authors/id/M/MI/MIYAGAWA/App-cpanminus-1.1005.tar.gz ... OK
Configuring App-cpanminus-1.1005 ... OK
Building and testing App-cpanminus-1.1005 ... OK
Successfully installed App-cpanminus-1.1005

local::lib をインストールする

ユーザーのローカルディレクトリに CPAN モジュールをインストールするために、local::lib をインストールする。
早速 cpanminus を使って local::lib をインストールする。

% ./cpanm local::lib

local::lib から出力される環境変数を予めシェルに登録するために、環境変数を出力する。

% perl -Mlocal::lib
Attempting to create directory /home/littlebuddha/perl5
export PERL_MB_OPT="--install_base /home/littlebuddha/perl5"
export PERL_MM_OPT="INSTALL_BASE=/home/littlebuddha/perl5"
export PERL5LIB="/home/littlebuddha/perl5/lib/perl5/i386-freebsd-64int:/home/littlebuddha/perl5/lib/perl5:$PERL5LIB"
export PATH="/home/littlebuddha/perl5/bin:$PATH"

ホームディレクトリはユーザーごとに異なるが、代替は $HOME 変数で代替できる。
私は zsh を使っているので、ホームディレクトリにある .zshrc に環境変数を記述する。

% vi ~/.zshrc
export PATH=$HOME/local/bin:$PATH
export PERL_MB_OPT="--install_base $HOME/perl5"
export PERL_MM_OPT="INSTALL_BASE=$HOME/perl5"
export PERL5LIB="$HOME/perl5/lib/perl5/i386-freebsd-64int:$HOME/perl5/lib/perl5:$PERL5LIB"
export PATH="$HOME/perl5/bin:$PATH"

環境変数を記述して設定を保存したら、下記のコマンドで変更を反映させる。

% source ~/.zshrc

最初に作成したディレクトリ「$HOME/local/bin」をシェルのサーチパスに追加した(「export PATH=$HOME/local/bin:$PATH」)ので、cpanminus をコマンド名のみで利用できるようになった。

% which cpanm
/home/littlebuddha/local/bin/cpanm

CPAN モジュールのインストール先を設定する

ローカルに CPAN モジュールをインストールするディレクトリを指定する。

% echo "install --install_base $HOME/perl5" > $HOME/perl5/.modulebuildrc
% less perl5/.modulebuildrc
install --install_base $HOME/perl5

これで新しくインストールされる CPAN モジュールは、Perl のバージョン毎に「$HOME/perl5」下にインストールされる。

perlbrew をインストールする

perlbrew インストール用ファイルを保存するディレクトリを用意する。

% mkdir ~/src

インストールを開始する。

% cd src/
 curl -LO https://github.com/gugod/App-perlbrew/raw/master/perlbrew
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 24014  100 24014    0     0  17125      0  0:00:01  0:00:01 --:--:-- 21595

% chmod u+x perlbrew

% ./perlbrew install
The perlbrew is installed as:

    /home/littlebuddha/perl5/perlbrew/bin/perlbrew

You may trash the downloaded /home/littlebuddha/src/perlbrew from now on.

Next, if this is the first time you install perlbrew, run:

    /home/littlebuddha/perl5/perlbrew/bin/perlbrew init

And follow the instruction on screen.

インストールが完了したら、「$HOME/src/perlbrew」は不要なので削除する。

% rm perlbrew

perlbrew の本体にシンボリックリンクを張って、使いやすくする。

% ln -s $HOME/perl5/perlbrew/bin/perlbrew $HOME/local/bin/perlbrew
% whereis perlbrew
perlbrew: /home/littlebuddha/local/bin/perlbrew

perlbrew で Perl をインストールする

まずは perlbrew の設定を初期化する。

% perlbrew init
Perlbrew environment initiated, required directories are created under

    /home/littlebuddha/perl5/perlbrew

Well-done! Congratulations! Please add the following line to the end
of your ~/.bashrc

    source /home/littlebuddha/perl5/perlbrew/etc/bashrc

After that, exit this shell, start a new one, and install some fresh
perls:

    perlbrew install perl-5.12.1
    perlbrew install perl-5.10.1

For further instructions, simply run:

    perlbrew

The default help messages will popup and tell you what to do!

Enjoy perlbrew at $HOME!!

初期化後、perlbrew の設定をシェルに登録する。

% echo "source $HOME/perl5/perlbrew/etc/bashrc" >> ~/.zshrc
% source ~/.zshrc

Perl をインストールする。今回は Perl 5.12.2 をインストールする。インストール(前のテスト)に失敗する場合は、「--force」オプションを使ってインストールすることができる。
今回は、thread のテストなどで失敗していたが、特に利用しない機能なので、「--force」を使ってインストールする。

% perlbrew --force install perl-5.12.2
Attempting to load conf from /home/littlebuddha/perl5/perlbrew/Conf.pm
Use the previously fetched perl-5.12.2.tar.gz
Installing perl-5.12.2 into /home/littlebuddha/perl5/perlbrew/perls/perl-5.12.2
This could take a while. You can run the following command on another shell to track the status:

  tail -f /home/littlebuddha/perl5/perlbrew/build.log

(cd /home/littlebuddha/perl5/perlbrew/build; tar xzf /home/littlebuddha/perl5/perlbrew/dists/perl-5.12.2.tar.gz;cd /home/littlebuddha/perl5/perlbrew/build/perl-5.12.2;rm -f config.sh Policy.sh;sh Configure -de '-Dprefix=/home/littlebuddha/perl5/perlbrew/perls/perl-5.12.2';make;make test;make install) >> '/home/littlebuddha/perl5/perlbrew/build.log' 2>&1
Installed perl-5.12.2 as perl-5.12.2 successfully. Run the following command to switch to it.

  perlbrew switch perl-5.12.2

インストール後の確認。

% perl -v

This is perl, v5.8.9 built for i386-freebsd-64int
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2008, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

perlbrew のコマンドで使う Perl のバージョンを切り替える。

% perlbrew switch perl-5.12.2
% perl -v

This is perl 5, version 12, subversion 2 (v5.12.2) built for i386-freebsd

Copyright 1987-2010, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
% perlbrew list
* perl-5.12.2
  /usr/bin/perl (5.8.9)
  /usr/local/bin/perl (5.8.9)

cpanminus で CPAN モジュールをインストールする

ここまで来て、cpanminus で Perl 5.12.2 の CPANモジュールをインストールすることが可能になる。

% cpanm WWW::Mechanize
--> Working on WWW::Mechanize
Fetching http://search.cpan.org/CPAN/authors/id/P/PE/PETDANCE/WWW-Mechanize-1.66.tar.gz ... OK
Configuring WWW-Mechanize-1.66 ... OK
==> Found dependencies: HTTP::Server::Simple, HTTP::Server::Simple::CGI, Test::Warn
--> Working on HTTP::Server::Simple
Fetching http://search.cpan.org/CPAN/authors/id/J/JE/JESSE/HTTP-Server-Simple-0.43.tar.gz ... OK
Configuring HTTP-Server-Simple-0.43 ... OK
Building and testing HTTP-Server-Simple-0.43 ... OK
Successfully installed HTTP-Server-Simple-0.43
--> Working on Test::Warn
Fetching http://search.cpan.org/CPAN/authors/id/C/CH/CHORNY/Test-Warn-0.22.tar.gz ... OK
Configuring Test-Warn-0.22 ... OK
==> Found dependencies: Sub::Uplevel, Tree::DAG_Node
--> Working on Sub::Uplevel
Fetching http://search.cpan.org/CPAN/authors/id/D/DA/DAGOLDEN/Sub-Uplevel-0.22.tar.gz ... OK
Configuring Sub-Uplevel-0.22 ... OK
Building and testing Sub-Uplevel-0.22 ... OK
Successfully installed Sub-Uplevel-0.22
--> Working on Tree::DAG_Node
Fetching http://search.cpan.org/CPAN/authors/id/C/CO/COGENT/Tree-DAG_Node-1.06.tar.gz ... OK
Configuring Tree-DAG_Node-1.06 ... OK
Building and testing Tree-DAG_Node-1.06 ... OK
Successfully installed Tree-DAG_Node-1.06
Building and testing Test-Warn-0.22 ... OK
Successfully installed Test-Warn-0.22
Building and testing WWW-Mechanize-1.66 ... OK
Successfully installed WWW-Mechanize-1.66