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 Tags:
  1. Alexey
    April 27th, 2017 at 02:56 | #1

    Great tutorial! Great job! Thank you so much! :*

  2. Soumen
    February 24th, 2017 at 23:44 | #2

    I am using Ubuntu 14.04, and instant client instantclient-basic-linux.x64-, and instantclient-sdk-linux.x64-, but the same error is showing, my php version is php5.9

  3. Soumen
    February 24th, 2017 at 23:40 | #4

    /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

  4. March 7th, 2016 at 08:34 | #5

    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,

    • March 8th, 2016 at 17:07 | #6

      @Omar – thanks

    • Alexey
      April 27th, 2017 at 03:00 | #7

      Hi there, just want to write about my decision of “no valid version”, I’ve used next command:

      sudo pecl install oci8-2.0.10

      I’ve got the same error.

  5. Ibrahim
    October 12th, 2015 at 22:59 | #8

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

  6. Ibrahim
    October 7th, 2015 at 21:02 | #9

    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

  7. October 7th, 2015 at 14:30 | #11

    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 😀

  8. Gerrit
    May 4th, 2015 at 07:49 | #12

    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!

  9. dev
    April 21st, 2015 at 10:34 | #13

    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.

  10. November 3rd, 2014 at 17:57 | #14

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

  11. Omar
    October 7th, 2014 at 08:48 | #15

    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 7th, 2014 at 09:45 | #16

      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:

  12. André
    September 3rd, 2014 at 17:21 | #18

    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 3rd, 2014 at 19:32 | #19
    • André
      September 3rd, 2014 at 19:37 | #20

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

      • September 3rd, 2014 at 19:48 | #21

        i am hoping you have root access ?

      • André
        September 3rd, 2014 at 20:20 | #22

        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 3rd, 2014 at 20:58 | #23

      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

  13. Sid
    May 29th, 2014 at 19:31 | #24

    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.

  14. Mohammad Ali
    April 28th, 2014 at 11:44 | #25

    can you tell me the procedure for windows 7 ?

  15. Francesco
    April 2nd, 2014 at 06:42 | #27

    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 3rd, 2014 at 15:58 | #28

      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.

  16. Matt
    February 27th, 2014 at 15:51 | #29

    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 27th, 2014 at 15:56 | #30

      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 27th, 2014 at 16:02 | #31

        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 4th, 2014 at 22:22 | #32

        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. February 26th, 2014 at 13:59 | #33


    I took your script and ran it on my machine just to see how it reacts and i got the below
    and dieArray
    [code][/code] => 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

  18. February 26th, 2014 at 02:37 | #34

    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 26th, 2014 at 02:39 | #35

      also try putting oci_internal_debug(true); before you connect

      • Eko Heri
        February 26th, 2014 at 03:22 | #36

        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 🙂


  19. Eko Heri
    February 26th, 2014 at 01:31 | #37

    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 26th, 2014 at 02:21 | #38

      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 26th, 2014 at 02:23 | #39

        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.

  20. December 17th, 2013 at 04:29 | #40

    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 18th, 2013 at 20:22 | #41

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

      • December 19th, 2013 at 04:53 | #42

        @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

  21. December 17th, 2013 at 02:08 | #43

    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 17th, 2013 at 03:58 | #44

      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

  22. November 5th, 2013 at 12:35 | #45

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

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

  23. July 27th, 2013 at 19:19 | #46

    /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 27th, 2013 at 19:37 | #47

      what OS are you trying this on ?

      • alex
        July 28th, 2013 at 05:40 | #48

        it’s ubuntu 13.04

      • alex
        July 28th, 2013 at 06:18 | #49

        ubuntu 13.04 64bit

    • alex
      July 28th, 2013 at 05:46 | #50

      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 28th, 2013 at 06:22 | #51

      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 1st, 2013 at 16:19 | #52

        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!

  24. Akash
    July 2nd, 2013 at 15:01 | #53

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

    • July 2nd, 2013 at 15:11 | #54

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

      • Akash
        July 2nd, 2013 at 18:09 | #55

        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 2nd, 2013 at 19:00 | #56


          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

  25. Omar
    June 27th, 2013 at 06:57 | #57

    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 27th, 2013 at 13:22 | #58

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

      • March 7th, 2016 at 08:49 | #59

        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,

  26. David Costello
    June 20th, 2013 at 18:12 | #60

    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 20th, 2013 at 18:31 | #61

      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 )

  27. David Costello
    June 20th, 2013 at 16:51 | #62

    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 20th, 2013 at 17:27 | #63


      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

  28. December 28th, 2012 at 18:56 | #64

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

  29. December 19th, 2012 at 12:57 | #65

    thanks for the tutorial, all work fine 😉

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

    • December 20th, 2012 at 20:12 | #66


      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

      • December 28th, 2012 at 02:14 | #67

        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 😉

  30. Francesco
    November 28th, 2012 at 09:46 | #68

    THANKS!!! You made my day 😉

  31. moti
    October 30th, 2012 at 15:19 | #69

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

  1. November 3rd, 2014 at 16:39 | #1
  2. June 1st, 2015 at 18:41 | #2