Home > Linux > Connect from PHP to Oracle DB ( using OCI8 on Ubuntu with Oracle Instant Client and SDK )

Connect from PHP to Oracle DB ( using OCI8 on Ubuntu with Oracle Instant Client and SDK )

I will be doing this on VirtualBox with Ubuntu 12.04 installed on it and up to date.

Please note that the OCI8 extensions, PHP version might be different or greater based on what OS version you are trying to install

Before we begin, make sure you have the following

  1. A working instance of LAMP ( Linux, Apache, php and MySQL ) – at least Apache and PHP
  2. Root privileges
  3. A file called phpinfo.php( in your webroot which has phpinfo() ) to verify OCI8 after installation

You can install LAMP very easily with tasksel : sudo apt-get install tasksel -> tasksel ->choose LAMP server

We will need 2 packages from Oracle ( 32 or 64 bit according to your machine )  – current Version – zip versions

  1. You can get the 2 downloads ( 32-bit ) from here http://www.oracle.com/technetwork/topics/linuxsoft-082809.html
  2. Instant Client Package – Basic: All files required to run OCI, OCCI, and JDBC-OCI applications
  3. Unzip all files in this to a directory called /opt/oracle_instantclient ( of-course you can put it anywhere you like )
  4. Instant Client Package – SDK: Additional header files and an example makefile for developing Oracle applications with Instant Client
  5. Unzip this into the same directory /opt/oracle_instantclient
  6. Your /opt/oracle_instantclient directory must now contain all files from Instant Client Package – Basic and an SDK directory from Instant Client Package – SDK
  7. Next we need to create some symbolic links to “.so” files – you will see a file called libclntsh.so.version in /opt/oracle_instantclient  directory. In my case the file name is libclntsh.so.11.1. We will create a symbolic link named libclntsh.so  to the file  libclntsh.so.11.1  in the same directory using the command ln -s libclntsh.so.11.1 libclntsh.so
  8. Next we will install the php5-dev package command sudo apt-get install php5-dev
  9. Next we will install the package libaio using the command sudo apt-get install libaio-dev
  10. Then we install pear package using sudo apt-get install php-pear 
  11. Next, we will install the oci8 extension using sudo pecl install oci8 
  12. This will ask for Oracle Home Directory – give the path of the instant client instantclient,/opt/oracle_instantclient 
  13. In the above, we are telling pecl that its an instantclient and then giving the path to the instantclient directory
  14. If the installation is successful, you should see messages like “Build Process completed successfully”
  15. Add the oracle oci8.so to php.ini using
  16. sudo echo “extension=oci8.so” >> /etc/php5/apache2/php.ini 
  17. sudo echo “extension=oci8.so” >> /etc/php5/cli/php.ini 
  18. Be careful of double quotes in the above commands – dont copy paste from here – after executing, check your php.ini files
  19. ( Tip from Omar ) An alternative is to add this to the oci8.ini file using the command sudo echo “extension=oci8.so” > /etc/php5/conf.d/oci8.ini
  20. Restart apache using service apache2 restart 
  21. Open a web browser and navigate to the phpinfo file ( in my case it is localhost/phpinfo.php )
  22. Search for oci8 and you should find oci8 block with variables ( this means you have successfully compiled PHP with the oci8 extension )
  23. Next step is to test connecting to an oracle instance using oci_connect();
  24. Contact your Oracle DBA and get the following information hostname, port, service name, service type, username, password and then try out an example from http://www.php.net/manual/en/function.oci-connect.php
  25. If you get the error, oci_connect() is not defined, then something went wrong. Try looking at apache error logs or use php -v 
Categories: Linux
  1. moti
    October 30, 2012 at 3:19 PM

    Great guide, I step by step and everything works !!! KUWTGJ🙂

  2. Francesco
    November 28, 2012 at 9:46 AM

    THANKS!!! You made my day😉

  3. Fery Wardiyanto
    December 19, 2012 at 12:57 PM

    thanks for the tutorial, all work fine😉

    but I cant use sqlplus at all, any solution??

    • December 20, 2012 at 8:12 PM


      what do you mean by “i cannot use sqlplus at all” ? – this post tells you how to connect PHP to Oracle and has nothing to do with sqlplus

      • Fery Wardiyanto
        December 28, 2012 at 2:14 AM

        oh my bad,,, sorry, I didn’t check InstantClient’s installation folder. they doesn’t come with sqlplus like so..🙂

        now, I need to use sqlplus, what should I do?

        btw, thanks for the response😉

  4. December 28, 2012 at 6:56 PM

    here is the link on how to install and use sqlplus – https://help.ubuntu.com/community/Oracle%20Instant%20Client
    hope it helps

  5. David Costello
    June 20, 2013 at 4:51 PM

    Thanks fo rthe info.For some reason it fails when running the oci8 install with a message of “cannot find -lclntsh”. Any thoughts? All other dependencies in the instructions were followed without an issue

    • June 20, 2013 at 5:27 PM


      i think its because of symbolic link to the libclntsh in step 7. i googled and found many users with the same problem – make sure that you have the symbolic link set up correct ( with correct permissions for the user ) and try again – let me know how it goes

  6. David Costello
    June 20, 2013 at 6:12 PM

    Thanks, setting the permission did get me a bit further along. Now it fails with ” Instant Client SDK header files not found” even though the SDK directory is in the directory with the correct permissions?

    • June 20, 2013 at 6:31 PM

      make sure you have both the oracle libraries of the same version – the current version is Version
      next thin to check is the directory structure. put the sdk directory inside the instant client
      next thing to check is the file permissions for the entire instant client directory ( if nothing works – just try to give others = rwx and test )

  7. Omar
    June 27, 2013 at 6:57 AM

    Many thanks Naveen,
    I tried this procedure on raring 13.04 and it’s fine on this version too!
    I prefered to create a new oci8.ini php configuration file in /etc/php5/conf.d folder instead of adding the extension directive to the php.ini file.
    So instead of running these commands:

    sudo echo ‘extension=oci8.so’ >> /etc/php5/apache2/php.ini
    sudo echo ‘extension=oci8.so’ >> /etc/php5/cli/php.ini

    what I did was:
    echo ‘extension=oci8.so’ > /etc/php5/conf.d/oci8.ini

    • June 27, 2013 at 1:22 PM

      Thanks for the tip Omar – i will add it to the post

      • March 7, 2016 at 8:49 AM

        Hi Naveen,
        the folder /etc/php5/conf.d is no more available, there is a new folder /etc/php5/mods-available in which the .ini files are stored, then they are linked in /etc/php5/apache2/conf.d and /etc/php5/cli/conf.d .

        so the command:
        echo ‘extension=oci8.so’ > /etc/php5/conf.d/oci8.in

        must be replaced with:
        sudo bash -e ‘echo “extension=oci8.so” > /etc/php5/mods-available/oci8.ini’
        cd /etc/php5/cli/conf.d/; sudo ln -s ../../mods-available/oci8.ini
        cd /etc/php5/apache2/conf.d/; sudo ln -s ../../mods-available/oci8.ini

        Many thanks,

  8. Akash
    July 2, 2013 at 3:01 PM

    I follow all the steps you are given . but it given “Call to undefined function oci_connect()” . how can i resolve this ???

    • July 2, 2013 at 3:11 PM

      this means the installation failed – check your phpinfo information – can you see the oci8 extension block ?

      • Akash
        July 2, 2013 at 6:09 PM

        No there is not any oci8 extension block. but during installation i do not get any error message. But “Additional .ini files parsed ” in phpinfo shows “/etc/php5/apache2/conf.d/oci8.ini” . please help me

      • July 2, 2013 at 7:00 PM


        Its difficult to guess the problem without knowing all the details. I would say start looking at these first What system have you got ( 32 bit or 64 ? ) and have you downloaded the correct oracle libraries (32 or 64) Have you installed all the required dependencies and do all your directories have the correct permissions Have you correctly put the extension in the php.ini files ( step 16, 17 ) Have you restarted apache after doing all this ? Do you see any errors when you type the command php -v

        The first step is to get the oci8 block to appear in your phpinfo output

  9. July 27, 2013 at 7:19 PM

    /usr/bin/ld: skipping incompatible /opt/oracle_instantclient/libclntsh.so when searching for -lclntsh
    /usr/bin/ld: cannot find -lclntsh
    collect2: error: ld returned 1 exit status
    make: *** [oci8.la] Error 1
    ERROR: `make’ failed

    • July 27, 2013 at 7:37 PM

      what OS are you trying this on ?

      • alex
        July 28, 2013 at 5:40 AM

        it’s ubuntu 13.04

      • alex
        July 28, 2013 at 6:18 AM

        ubuntu 13.04 64bit

    • alex
      July 28, 2013 at 5:46 AM

      more info

      /bin/bash /tmp/pear/temp/pear-build-rootLFVJo0/oci8-1.4.10/libtool –mode=link cc -DPHP_ATOM_INC -I/tmp/pear/temp/pear-build-rootLFVJo0/oci8-1.4.10/include -I/tmp/pear/temp/pear-build-rootLFVJo0/oci8-1.4.10/main -I/tmp/pear/temp/oci8 -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib -I/usr/local/lib/instantclient_11_2/sdk/include -DHAVE_CONFIG_H -g -O2 -o oci8.la -export-dynamic -avoid-version -prefer-pic -module -rpath /tmp/pear/temp/pear-build-rootLFVJo0/oci8-1.4.10/modules oci8.lo oci8_lob.lo oci8_statement.lo oci8_collection.lo oci8_interface.lo -Wl,-rpath,/usr/local/lib/instantclient_11_2 -L/usr/local/lib/instantclient_11_2 -lclntsh
      libtool: link: cc -shared -fPIC -DPIC .libs/oci8.o .libs/oci8_lob.o .libs/oci8_statement.o .libs/oci8_collection.o .libs/oci8_interface.o -L/usr/local/lib/instantclient_11_2 -lclntsh -O2 -Wl,-rpath -Wl,/usr/local/lib/instantclient_11_2 -Wl,-soname -Wl,oci8.so -o .libs/oci8.so
      /usr/bin/ld: skipping incompatible /usr/local/lib/instantclient_11_2/libclntsh.so when searching for -lclntsh
      /usr/bin/ld: cannot find -lclntsh
      collect2: error: ld returned 1 exit status
      make: *** [oci8.la] Помилка 1
      ERROR: `make’ failed

      please help


    • alex
      July 28, 2013 at 6:22 AM

      perhaps solution:

      using %oracle_home% (e.g /u01/app/oracle/product/11.2.0/xe on ubuntu 13.04) instead instantclient,/usr/local/lib/instantclient_11_2

      it’s work…

      • Chris
        November 1, 2013 at 4:19 PM

        After a lot of head scratching on this one I should have just read the error message. The system is not having a problem finding the lclntsh file, it has found it and rejected it on the error line before. My solution was to ensure I was using the correct version, the link in the guide takes you to the 32bit download page, if you are on 64 bit you need to use the appropriate instantclient download http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html doh!

  10. November 5, 2013 at 12:35 PM

    after add oci8.so to ini files remove single quates from newly added lines

    ‘extension=oci8.so’ to extension=oci8.so

  11. December 17, 2013 at 2:08 AM

    Dear I tried all the steps above but still the oci8 block is not coming in the phpinfo.

    My machine is Ubuntu 12.04 64 bit and I selected 64 bit instant client packages only.

    • December 17, 2013 at 3:58 AM

      check the permissions on libclntsh.so – most cases its the permissions or the 32-64 bit mismatch or restarting apache.
      also make sure you have put the extension in the right php ini file

  12. December 17, 2013 at 4:29 AM

    Permission is like below see the folder listing.

    lrwxrwxrwx 1 www-data www-data 43 Dec 17 04:27 libclntsh.so -> /opt/oracle_instantclient/libclntsh.so.10.1
    -rwxrwxrwx 1 www-data www-data 25M Dec 17 04:25 libclntsh.so.10.1

    Regarding packages I used are 64 bit itself, namely sdk-, basic-

    Also I added extension entry in /etc/php5/apache2/php.ini

    Any clue

    • December 18, 2013 at 8:22 PM

      i am going to try this on a 64 bit and i will let you know

      • December 19, 2013 at 4:53 AM

        @progress2011 – its because of the single quotes in the php ini file – i think you copy pasted the command from here. I just tried the installation on 64 bit and it works.

        just open you php ini and check if you have additional quotes for extension=oci8.so
        remove them and restart apache
        let me know how it goes

  13. Eko Heri
    February 26, 2014 at 1:31 AM

    I’ve done all the step and successfully display in the phpinfo. But when i try to oci connect using this script:

    The result is only:
    Its here and die

    When i run php -v it display no error. Is there any way to trace this problem? I run in ubuntu 12.04 32 bit.


    • February 26, 2014 at 2:21 AM

      what script are you using to connect to oracle ?
      do you have the correct database credentials to connect ?
      can you turn on error reporting in your script and see if any errors are being generated ?

      • Eko Heri
        February 26, 2014 at 2:23 AM

        Sorry i think the script is missing. Here are the script:
        echo “Its here”;


        if(!@($conn = oci_connect(‘user’,’pass’,$db)))
        { echo ” and die”;
        $e = oci_error();

        I think the credential is correct because i can run this script in my partner PC which run with XAMPP and Windows.

  14. February 26, 2014 at 2:37 AM

    your script seems to be failing when connecting to the database. If you have set up everything properly, the only thing i can think of is your access and credentials – are there any firewall rules or database rules that have to be tweaked ?

    as i told earlier – enable error reporting in your PHP script – put the two lines at the top of your script and run it – do you get any errors ?

    here is the way to connect to oracle – ( see example 2 )

    • February 26, 2014 at 2:39 AM

      also try putting oci_internal_debug(true); before you connect

      • Eko Heri
        February 26, 2014 at 3:22 AM

        Dear Naveen,

        I’ve tried it and change the code like this:
        echo “Its here”;

        ini_set(‘display_errors’, ‘1’);

        if(!@($conn = oci_connect(‘user’,’pass’,$db)))
        { echo ” and die”;
        $e = oci_error();

        The result in my lamp is : Its here and die
        When i try in xampp with my partner PC the result is : Its here.

        I can connect to the database server using Oracle SQL Developer from my PC. I think it means i can access the server. Any other idea?\
        I agree with you that my problem is when try to connecting to the Oracle because i haven’t execute any query. But i think i cant do query until successfully connect to db🙂


  15. February 26, 2014 at 1:59 PM


    I took your script and ran it on my machine just to see how it reacts and i got the below
    and dieArray
    => 12170
    [message] => ORA-12170: TNS:Connect timeout occurred
    [offset] => 0
    [sqltext] =>

    the script should print out something if there is a connection error

    sry i cannot help you further – but if you do find the problem, post back and let me know

  16. Matt
    February 27, 2014 at 3:51 PM

    Awesome guide, big help! Having trouble with adding oci8 to php5… I’m getting “Permission denied” errors, despite being able to manually edit php.ini?

    Any ideas?

    user@websvr01:/etc/php5/apache2$ sudo echo “extension=oci8.so” >> /etc/php5/apache2/php.ini
    -bash: /etc/php5/apache2/php.ini: Permission denied

    • February 27, 2014 at 3:56 PM

      are you sure you have root privileges ? it should ask you for the root password before opening the file

      if you do have root, you can also just try to open the file manually – just use the command nano /etc/php5/apache2/php.ini
      type in the oci8 line and save

      • Matt
        February 27, 2014 at 4:02 PM

        Well, I was successfully using sudo for other commands, but ssh’ing back in as root (rather than escalating regular account) seemed to have worked! Still a bit puzzled by that. I’m by no means an expert, but I use ‘sudo’ all the time.

        Anyways, thanks again for the guide… now to see if I can connect to an old Oracle 9i db

      • Matt
        March 4, 2014 at 10:22 PM

        hmmmm…. Using instant client, I get ORA-03134: Connections to this server version are no longer supported. How do I uninstall v12. to try an older version?

  17. Francesco
    April 2, 2014 at 6:42 AM

    Matt :
    hmmmm…. Using instant client, I get ORA-03134: Connections to this server version are no longer supported. How do I uninstall v12. to try an older version?

    I’ve the same problem…can anyone helps me?🙂

    Thanks a lot

    • Matt
      April 3, 2014 at 3:58 PM

      If I recall correctly, I simply deleted the oci folder, then went through the motions with an older package. Not sure if that’s the right way,but the older package worked on 9i.

  18. Mohammad Ali
    April 28, 2014 at 11:44 AM

    can you tell me the procedure for windows 7 ?

  19. Sid
    May 29, 2014 at 7:31 PM

    Thank you so much for this article. I looked through quite a few resources but wasn’t able to get it to work until I got here. Very precise instructions.

  20. André
    September 3, 2014 at 5:21 PM

    I don’t know why, but i can’t install oci8 extension (step 11).

    ratna@ricochete:/usr/lib/oracle/12.1/client64/lib$ sudo pecl install oci8
    sudo: unable to resolve host ricochete
    No releases available for package “pecl.php.net/oci8”
    install failed

    Any ideas?

    • September 3, 2014 at 7:32 PM
    • André
      September 3, 2014 at 7:37 PM

      ratna@ricochete:/etc/php5/apache2$ sudo echo “extension=oci8.so” >> /etc/php5/apache2/php.ini
      bash: /etc/php5/apache2/php.ini: Permission denied

      • September 3, 2014 at 7:48 PM

        i am hoping you have root access ?

      • André
        September 3, 2014 at 8:20 PM

        Sorry, i pasted the wrong commands. But… i have root access certainly. Im still working in the first problem, i cant figure out why oci8 is not instaled…

    • September 3, 2014 at 8:58 PM

      I would suggest first try to update all the libraries for your current distribution to the latest
      Then try re-initializing the channel according to the link above
      If nothing works, i would recommend first starting with a fresh copy of your distribution

  21. Omar
    October 7, 2014 at 8:48 AM

    if at step 12 you get some error like:
    “Expected an ORACLE_HOME top level directory but /opt/oracle_instantclient appears to be an Instant Client directory”
    that means you typed only the path: /opt/oracle_instantclient when prompted.
    You have to type the whole string:
    Good Luck,

    • October 7, 2014 at 9:45 AM

      There were two errors in my last comment. And here is the correction.
      Could you, please delete the previous comment and keep this one?
      Many thanks.

      I tried this tutorial on ubuntu 14.04.1 Trusty Tahr.
      I’ve got the error:
      bash: /etc/php5/conf.d/oci8.ini: No such file or directory
      as a result of the command (step 19):
      sudo echo ‘extension=oci8.so’ > /etc/php5/conf.d/oci8.ini
      I solved it by:

      sudo echo ‘extension=oci8.so’ > /etc/php5/mods-available/oci8.ini
      cd /etc/php5/apache2/conf.d; sudo ln -s ../../mods-available/oci8.ini 20-oci8.ini
      cd /etc/php5/cli/conf.d; sudo ln -s ../../mods-available/oci8.ini 20-oci8.ini

      The php5.5.9 shipped with ubuntu14.04.1 has a /etc/php5 directory structure different than the old version. Actually the .ini files have been moved from:

      • October 7, 2014 at 3:58 PM

        @Omar – Thanks for the updates !

  22. November 3, 2014 at 5:57 PM

    Hey Omar… nice update!… work great for Ubuntu 14.04 LTS!!!

  23. dev
    April 21, 2015 at 10:34 AM

    Tried this guide on ubuntu 14.04 and it works fine.

    Please be aware to choose correctly between 32-bit or 64-bit. Otherwise you will have a problem when step 11: Next, we will install the oci8 extension using sudo pecl install oci8.

  24. Gerrit
    May 4, 2015 at 7:49 AM

    I did an release-upgrade from ubuntu 12.04 to 14.04, with oci8 already installed. After the upgrade, the oci8 got broke. I uninstalled and re-installed oci8, and after that it worked again.🙂. Thanks Omar for your update about 14.04!

  25. October 7, 2015 at 2:30 PM

    I get this error on Ubuntu 15.04 executing the command (“sudo pecl install oci8”):

    compilation terminated.
    Makefile:183: recipe for target ‘oci8.lo’ failed
    make: *** [oci8.lo] Error 1
    ERROR: `make’ failed

    After many tests, I solved it using the gksudo (“gksudo pecl install oci8”)
    Works fine😀

  26. Ibrahim
    October 7, 2015 at 9:02 PM

    Even after 3 years, this post has been realy helpful!! Thanks, oci connect is working now, but I have a question, using all this installation and configuration, Could i work with PDO for Oracle? Thanks

  27. Ibrahim
    October 12, 2015 at 10:59 PM

    Thanks for your help Naveen Nayak, I had to make several configurations, but now PDO_OCI support is working.

  28. March 7, 2016 at 8:34 AM

    at the step 11: “Next, we will install the oci8 extension using sudo pecl install oci8”
    I’ve got this error:

    pecl/oci8 requires PHP (version >= 7.0.0), installed version is 5.5.9-1ubuntu4.14
    No valid packages found
    install failed

    to solve it I’ve used the command:
    sudo pecl install oci8-1.4.10

    as suggested in: https://pecl.php.net/package/oci8
    Good Luck,

  1. November 3, 2014 at 4:39 PM
  2. June 1, 2015 at 6:41 PM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: