Mac OS X(Leopard)へPEARをインストールする手順

手元のMacBook Pro(OS X; Leopard)にPEARをインストールした際の手順メモです。

追記 (2009/03/09 01:46)

portでphp5のインストール時のオプションでpearをインストールできるみたい。気付くのが遅かった。orz

ちなみにこんだけオプションがあります。

$ port variants php5
php5 has the variants:
        darwin_6: Platform variant, do not select manually
        darwin_7: Platform variant, do not select manually
        macosx: Platform variant, do not select manually
        no_web: Don't include any web server support
        apache: Add Apache 1 web server module
        apache2: Add Apache 2.2 web server module (default)
        fastcgi: Add FastCGI web server binary
        gmp: Add GNU MP functions
        dbase: Add dBase file format support
        imap: enable operation with IMAP protocol
        pspell: Add pspell spell-checking functions
        tidy: add Tidy support
        mssql: add support for MS-SQL server
        snmp: use Apple snmp
        macports_snmp: use MacPorts snmp
        mysql3: build MySQL 3 support
        mysql4: build MySQL 4 support
        mysql5: build MySQL 5 support
        oracle: Add Oracle oci8 database functions with the Oracle Instant Client
        postgresql82: provide postgresql82 support
        postgresql83: provide postgresql83 support
        sqlite: build sqlite support
        ipc: build IPC support
        pcntl: provide process control
        pear: add pear stuff
        readline: Add GNU readline functions
        sockets: Add socket communication functions
        t1lib: Add PostScript Type 1 font support with t1lib
        universal: Build for multiple architectures

ってことで、とりあえずMySQLPearを追加しておく。(とりあえずなので、あとで見直すかも)

$ sudo port -d install php5 +mysql5 +pear

ちなみにConfigure Optionは以下の通りでした。(画面の都合上、改行を入れています)

$ php -i | grep Configure
Configure Command =>
  '/SourceCache/apache_mod_php/apache_mod_php-44.1/php/configure'
  '--prefix=/usr' '--mandir=/usr/share/man'
  '--infodir=/usr/share/info' '--disable-dependency-tracking'
  '--with-apxs2=/usr/sbin/apxs' '--with-ldap=/usr'
  '--with-kerberos=/usr' '--enable-cli' '--with-zlib-dir=/usr'
  '--enable-trans-sid' '--with-xml' '--enable-exif'
  '--enable-ftp' '--enable-mbstring' '--enable-mbregex'
  '--enable-dbx' '--enable-sockets' '--with-iodbc=/usr'
  '--with-curl=/usr' '--with-config-file-path=/etc'
  '--sysconfdir=/private/etc'
  '--with-mysql-sock=/var/mysql' '--with-mysqli=/usr/bin/mysql_config'
  '--with-mysql=/usr' '--with-openssl' '--with-xmlrpc'
  '--with-xsl=/usr' '--without-pear'

(一度、pearをインストールしたから、--without-pearオプションになってしまってるっぽい気がするなぁ)

以降は、最初に書いたインストール方法なのですが、上記の手順の方が簡単ですね。(上記の方だとmbstringが有効になってますしね)

参考

上記の方法より以前にやってた方法です。参考までに残しておきます。

そのままの状態では、PEARはインストールされていないので、個別にインストールする必要があるらしく、PEARのサイトを見ると

といった説明が書かれているので、参考にしつつ以下のコマンドを実行します。

$ curl -o go-pear.php http://pear.php.net/go-pear
$ sudo php -q go-pear.php 

途中で訊かれてるパスの設定については、prefixの部分を/usr/share/pearと設定しました。(/Users/xxx ってのがイヤだったので)

1. Installation prefix ($prefix) : /usr/share/pear
2. Temporary files directory     : $prefix/temp
3. Binaries directory            : $prefix/bin
4. PHP code directory ($php_dir) : $prefix/PEAR
5. Documentation base directory  : $php_dir/docs
6. Data base directory           : $php_dir/data
7. Tests base directory          : $php_dir/tests

このままでは、pearコマンドへのパスが通ってないので、環境変数PATHへ追加しておきます(この例はbashです)。

export PATH=【中略】:/usr/share/pear/bin:$PATH

忘れずに反映しておきます。

$ source ~/.bashrc

HTTP_Requestを試しにいれてみます。

$ sudo pear install HTTP_Request

HTTP_RequestとNet_URLって

WARNING: "pear/HTTP_Request" is deprecated in favor of "pear/HTTP_Request2"
WARNING: "pear/Net_URL" is deprecated in favor of "pear/Net_URL2"

なんですね。へぇー、知らなかった。。ちなみにHTTP_Request2は、まだbetaみたいです。

あとinclude_pathが . となっているようので、php.iniの設定を変更します(/etc/php.iniでなく、実体は/private/etc/php.iniみたいです)。

$ cd /private/etc/
$ sudo cp php.ini.default php.ini

変更した部分は以下の通り。

$ diff -b php.ini php.ini.default 
469c469
< include_path = ".:/usr/share/pear/PEAR"
---
> ;include_path = ".:/php/includes"

include_pathが変更されたかどうかを確認しておきます。

$ php -i | grep include_path
include_path => .:/usr/share/pear/PEAR => .:/usr/share/pear/PEAR

以上で完了です。

おまけ

t.phpというファイル名でサンプルを作ってみた。

<?php
require_once('HTTP/Request.php');
$request = new HTTP_Request("http://d.hatena.ne.jp/solitary_shell/");
if (PEAR::isError($request->sendRequest())) {
    echo "Request error!\n";
} else {
    echo "Status code: ".$request->getResponseCode()."\n";
}
?>

んでもって実際に動かしてみた。

$ php t.php 
Status code: 200