How to install PHP5 PDO_OCI & OCI8 (and other) extensions for Ubuntu
Updated 2015–11–10: this guide was tested to work well on Debian 7.6
I’m currently working on a PHP project that requires connection to an existing Oracle DB. We use Yii 2.0 as our PHP framework, and Ubuntu 14.10 as the development environment.
After some searching, I figured out that Yii 2.0 supports Oracle DB through PDO only (via PDO_OCI extension) that is quite old and requires some tricks to install. Official support for Oracle OCI8 is currently not yet available in Yii core, so we stuck with using PDO_OCI which is still EXPERIMENTAL.
The PDO, PDO_OCI extensions from pecl install
are obsolete because latest PHP version has them built-in its core. After attempt of several approaches we ended up with Compiling these extensions from PHP source following steps and tricks described in this post. I also include steps to install better maintained Oracle OCI8extension in this post for whom that want to use it without the help of Yii DB Connection.
Install Oracle Instant Client
Download Oracle Instant Client packages
First, select right client for your OS here, Instant Client for Linux x86–64 in my case for my 64-bit Ubuntu 14.10.
Then Accept License Agreement and download basic
& devel
rpm packages for your Oracle version. Our Oracle DB version is 11g r2, so I need to download:
- oracle-instantclient11.2-basic-11.2.0.1.0–1.x86_64.rpm
- oracle-instantclient11.2-devel-11.2.0.1.0–1.x86_64.rpm
Install the instant client
Convert .rpm
to Debian software package (.deb
) using alien
:
sudo apt-get instal alien
sudo alien -d oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.rpm
sudo alien -d oracle-instantclient11.2-devel-11.2.0.1.0-1.x86_64.rpm
This will output oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.deb
& oracle-instantclient11.2-devel-11.2.0.1.0-1.x86_64.deb
which can be installed via Ubuntu's dpkg
command:
sudo dpkg -i oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.deb
sudo dpkg -i oracle-instantclient11.2-devel-11.2.0.1.0-1.x86_64.deb
After successful installation, client library with be installed in /usr/share/oracle/11.2/client64
, and developer header files with be in /usr/include/oracle/11.2/client64
.
Export ORACLE_HOME
environment variable
We need these environment variable to compile PDO_OCI & OCI8 extensions:
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
sudo ldconfig
Download, compile and install PHP PDO_OCI
and OCI8
extensions
Download PHP source
Assuming that you already have PHP installed on your system, you just need to compile and install the extensions separately.
At first, check your current PHP version by command php -v
and download corresponding PHP source archive from http://php.net/downloads.php. In my case, I downloaded PHP-5.5.9
from http://php.net/get/php-5.5.9.tar.bz2/from/a/mirror, & uncompressed it using command:
tar xjf php-5.5.9.tar.bz2
Then prepare pdo_oci
extension for compiling using:
cd php-5.5.9/ext/pdo_oci
phpize
This will prepare configure
script for you.
Note: You need
php5-dev
package to usephpize
command. If you don't have it on your system, just install bysudo apt-get install php5-dev
Next, proceed with configure
to create Makefile
:
./configure
You may get an error yells that configure: error: Cannot find php_pdo_driver.h
. This is because the configure
script tries to look for php include files in /usr/include/php
that has been changed to /usr/include/php5
for latest PHP versions. The quick trick here is to create a soft link for legacy path:
sudo ln -s /usr/include/php5/ /usr/include/php
and run the ./configure
script again. It should succeed now. Proceed with the compilation:
make
It might scream another error similar to ***php-5.5.9/ext/pdo_oci/php_pdo_oci_int.h:21:17: fatal error: oci.h: No such file or directory
. To bypass this, we need another trick here:
- Edit
Makefile
file:
vim Makefile
- Find the line start with
EXTRA_INCLUDES =
and add path to installed Oracle Instant Client header files (/usr/include/oracle/11.2/client64
) to it, if this line does NOT exist, just add it near the beginning of the file. Final result should be:
...
EXTRA_INCLUDES = -I/usr/include/oracle/11.2/client64
...
Then run make
again, the compilation should be complete now.
Finally, install the compiled extension to the system:
sudo make install
This will copy pdo_oci.so
file to /usr/lib/php5/yyyyMMdd/pdo_oci.so
Install OCI8 and other extensions can be done easily following the same steps, with the tricks mentioned above.
Enable new extensions
The final step is to enable required extensions and restart web server (suppose you are using Apache 2 here)
First, create pdo_oci.ini
in /etc/php5/mods-available/
:
sudo vim /etc/php5/mods-available/pdo_oci.ini
with following content:
; priority=20
extension=pdo_oci.so
Then, enable it and restart Apache:
sudo php5enmod pdo_oci
sudo service apache2 restart
This will automatically create a soft link to just-created pdo_oci.ini
file for you in /etc/php5/apache2/conf.d
similar to /etc/php5/apache2/conf.d/20-pdo_oci.ini -> ../../mods-available/pdo_oci.ini
, where prefix 20 is the default priority of the extension.
To ensure that everything works as expected, you can create and info.php
file with following content under your web-root folder:
<?php
phpinfo();
?>
And open it in browser (http://localhost/info.php, for example), you should see information about pdo_oci
extension installed as following:
And it’s DONE.