How to install oracle instantclient and pdo_oci on ubuntu machine?

skonsoft picture skonsoft · Feb 21, 2014 · Viewed 41.8k times · Source

I need to install PDO_OCI in ubuntu machine, there is no default package that I could install with apt-get.

There are a lot of tutorials showing how to do it, but when I follow them, I have problems related to compilation (configure, make,...)

Here what I did:

  1. I followed this Tutorial to install instant client

  2. Install oci8

    pecl install oci8
    

    I get error:

    error: oci.h not found

  3. Install PDO_OCI

    mkdir -p /tmp/pear/download/
    cd /tmp/pear/download/
    pecl download pdo_oci
    phpize
    ./configure –with-pdo-oci=instantclient,/usr,11.2
    

    error:

    pdo_driver.h not found ...

Please do you have any serious tutorial that works perfectly on UBUNTU 12.04?

Answer

Andrey Grachev picture Andrey Grachev · Sep 8, 2014

The answer is a replication of this article (in Russian) which is in turn based on this post with some corrections. After several days of fruitless search it worked smoothly for me.

Prerequisites:

You should have administrator privileges

You should have php5 installed with the following packages:

sudo apt-get install php5 php5-dev php-pear php5-cli
sudo pecl install pdo

You should have libaio1 library installed:

sudo apt-get install libaio1

Installation of Oracle Instant Client

Dowload Oracle instant client for your processor architecture and OS from Oracle website (oracle.com/technetwork/database/features/instant-client/index-097480.html).

For Linux there are 2 options of instant client: RPM package for Linux, CentOS, Fedora, Red Hat Enterprise Linux, Mandriva Linux, SUSE Linux, etc. ZIP archive — for all others not supporting RPM.

There are 2 files to be downloaded:

instantclient-basic — Oracle instant client itself

instantclient-sdk — set of libraries for application development

Create directory for Oracle instant client ( /opt directory reserved for software extensions suits well for this purpose):

sudo mkdir -p /opt/oracle/

Move downloaded files to /opt/oracle and switch to destination folder (assuming that you downloaded "zip" archives to your user "downloads" directory):

sudo mv ~/downloads/instantclient-*.zip /opt/oracle/
cd /opt/oracle/

Extracting downloaded archives:

sudo unzip instantclient-basic-*-*.zip
sudo unzip instantclient-sdk-*-*.zip

Finally we have instantclient_11_2 directory created in /opt/oracle for Oracle instant client 11.2.0.2.0. Rename this directory to instantclient (pay attention to version number) and switch to it:

sudo mv instantclient_11_2 instantclient
cd instantclient

Next we'll have to create several additional directories and symlinks (pay attention to version number):

sudo ln -s /opt/oracle/instantclient/libclntsh.so.* /opt/oracle/instantclient/libclntsh.so
sudo ln -s /opt/oracle/instantclient/libocci.so.* /opt/oracle/instantclient/libocci.so
sudo ln -s /opt/oracle/instantclient/ /opt/oracle/instantclient/lib

sudo mkdir -p include/oracle/11.2/
cd include/oracle/11.2/
sudo ln -s ../../../sdk/include client
cd -

sudo mkdir -p lib/oracle/11.2/client
cd lib/oracle/11.2/client
sudo ln -s ../../../ lib
cd -

Create configuration file containing name of directory where Oracle instant client libraries are to be searched for and enable it:

echo /opt/oracle/instantclient/ | sudo tee -a /etc/ld.so.conf.d/oracle.conf
sudo ldconfig

As far as there is not directory /usr/include/php in Ubuntu, but the client is still searchin for it, we'll create symlink to it's equivalent - php5:

sudo ln -s /usr/include/php5 /usr/include/php

Installation of OCI8

After previous actions oci8 extension is installed with pecl command:

sudo pecl install oci8

you will be prompted for path to Oracle instant client, respond with:

instantclient,/opt/oracle/instantclient

Creating extension connection file:

echo "; configuration for php oci8 module" | sudo tee /etc/php5/conf.d/oci8.ini
echo extension=oci8.so | sudo tee -a /etc/php5/conf.d/oci8.ini

Installation of PDO_OCI

For installation of PDO_OCI download it from pear repository (pear.php.net).

Update pear packages list:

sudo pecl channel-update pear.php.net

Download and place archive to temp directory:

sudo mkdir -p /tmp/pear/download/
cd /tmp/pear/download/
sudo pecl download pdo_oci

Extract archive contents:

sudo tar xvf PDO_OCI*.tgz
cd PDO_OCI*

Here we'll have to ammend config.m4 file as it does not contain information on our version of Oracle instant client. Open the file and add changes marked with "+" (pay attension to version number):

sudo vim config.m4

Below is the diff of 2 files:

***************
*** 7,12 ****
--- 7,14 ----
if test -s "$PDO_OCI_DIR/orainst/unix.rgs"; then
PDO_OCI_VERSION=`grep '"ocommon"' $PDO_OCI_DIR/orainst/unix.rgs | sed 's/[ ][ ]*/:/g' | cut -d: -f 6 | cut -c 2-4`
test -z "$PDO_OCI_VERSION" && PDO_OCI_VERSION=7.3
+ elif test -f $PDO_OCI_DIR/lib/libclntsh.$SHLIB_SUFFIX_NAME.11.2; then
+ PDO_OCI_VERSION=11.2
elif test -f $PDO_OCI_DIR/lib/libclntsh.$SHLIB_SUFFIX_NAME.10.1; then
PDO_OCI_VERSION=10.1 
elif test -f $PDO_OCI_DIR/lib/libclntsh.$SHLIB_SUFFIX_NAME.9.0; then
***************
*** 119,124 ****
--- 121,129 ----
10.2)
PHP_ADD_LIBRARY(clntsh, 1, PDO_OCI_SHARED_LIBADD)
;;
+ 11.2)
+ PHP_ADD_LIBRARY(clntsh, 1, PDO_OCI_SHARED_LIBADD)
+ ;;
*)
AC_MSG_ERROR(Unsupported Oracle version! $PDO_OCI_VERSION)
;;
***************

Prepare environment for php extension with phpize (php.net/manual/ru/install.pecl.phpize.php) command:

sudo phpize

Configure package installer and install package (pay attention to version number):

sudo ./configure --with-pdo-oci=instantclient,/opt/oracle/instantclient/,11.2
sudo make
sudo make install

Create connection file for it:

echo "; configuration for php PDO_OCI module" | sudo tee /etc/php5/conf.d/pdo_oci.ini
echo extension=pdo_oci.so | sudo tee -a /etc/php5/conf.d/pdo_oci.ini

Restart apache and check if extensions were installed:

sudo /etc/init.d/apache2 restart
php -m