Compiling the OCI8 and PDO_OCI PHP extensions on OS X and Linux

Yet another case of too much misinformation polluting the illustrious intarwebs, this article will, very briefly, discuss how to compile PHP with oracle support.

Firstly, if you haven’t already, follow my guide for installing the oracle instantclient (found here). Once that’s done, you’re ready to begin.

Step 1: Download PHP

You’ll need to get the PHP source from http://www.php.net/download. Extract the source into a location of your choice. cd into the ext/ directory in the source tree.

1
cd /tmp/src/php-5.3.0/ext

Step 2: Compile oci8

Now cd into the ext/oci8 directory. Complete the following steps:

1
2
3
phpize
./configure --with-oci8=instantclient,$ORACLE_HOME,10.2
make && make install

Be sure that you have the three environment variables discussed in the previous article set. Those were $ORACLE_HOME, $TNS_ADMIN, and $LD_LIBRARY PATH (or $DYLD_LIBRARY_PATH for OS X)

If you installed a different version of the instantclient, replace the ’10.2′ in the configure line with your version. The correct version can be quickly obtained by glancing at the files in $ORACLE_HOME (for instance, the file libocci.so.10.2 would indicate the 10.2 release)

Step 3: Installing PDO_OCI

The steps for installing PDO_OCI are basically the same, with one minor revision. cd into the ext/pdo_oci directory, and complete the following steps:

1
2
3
phpize
./configure --with-pdo_oci=instantclient,$ORACLE_HOME,10.2
make && make install

Again, make sure that your environment variables are set. This is imperative; without those variables set PHP doesn’t know where to look for the appropriate libraries.

Step 4: Edit php.ini

The final step is to edit php.ini, and add the following lines

1
2
extension=oci8.so
extension=pdo_oci.so

Once that’s done, you can test your installation by running the following:

1
php -r '$db = new PDO("oci:dbname=dbname", "user", "password");'

If you get an error, check to make sure you’ve followed each step, and that $ORACLE_HOME , $TNS_ADMIN and $LD_LIBRARY_PATH or $DYLD_LIBRARY_PATH are set.

Restart your web server and enjoy.

Leave a comment