LinuxConnect 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 11.2.0.3.0 – 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

Comments

  1. Alexey

    April 27, 2017 2:56 am

    Great tutorial! Great job! Thank you so much! :*
  2. Soumen

    February 24, 2017 11:44 pm

    I am using Ubuntu 14.04, and instant client instantclient-basic-linux.x64-11.2.0.3.0, and instantclient-sdk-linux.x64-11.2.0.3.0, but the same error is showing, my php version is php5.9
    1. February 27, 2017 4:42 pm

      @Soumen, check if you have the required versions of all the libraries - this post was for Ubuntu 12.04 and I have not kept track of library updates check if gcc is installed correctly also check this: http://stackoverflow.com/questions/335928/ld-cannot-find-an-existing-library see answer from ephemient give it a shot on a vanilla install of ubuntu and see if it works
  3. Soumen

    February 24, 2017 11:40 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
  4. March 7, 2016 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. March 8, 2016 5:07 pm

      @Omar - thanks
    2. Alexey

      April 27, 2017 3:00 am

      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 12, 2015 10:59 pm

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

    October 7, 2015 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
    1. October 7, 2015 9:21 pm

      @Ibrahim yes you can use PDO for Oracle - but it still needs some instant client set up as far as i can tell - detaile here http://php.net/manual/en/ref.pdo-oci.php Oracle does not officially support PDO - it recommends Oci8 extension Some more insights @stackoverflow http://stackoverflow.com/questions/2563732/oracle-pdo-oci-vs-oci8
  7. October 7, 2015 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 :D
  8. June 1, 2015 6:41 pm

    […] Fonte: https://naveensnayak.wordpress.com/2012/09/17/connection-from-php-to-oracle-database-using-oci8-on-u… […]
  9. Gerrit

    May 4, 2015 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!
  10. dev

    April 21, 2015 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.
  11. November 3, 2014 5:57 pm

    Hey Omar... nice update!... work great for Ubuntu 14.04 LTS!!!
  12. November 3, 2014 4:39 pm

    […] is not particularly simple. Here’s what I did, on Ubuntu 12.04. My instructions are based on these by Naveen S Nayak, plus some bits I had to figure […]
  13. Omar

    October 7, 2014 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: instantclient,/opt/oracle_instantclient Good Luck,
    1. October 7, 2014 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: /etc/php5/conf.d to /etc/php5/mods-available .
      1. October 7, 2014 3:58 pm

        @Omar - Thanks for the updates !
  14. André

    September 3, 2014 5:21 pm

    I don't know why, but i can't install oci8 extension (step 11). Error: 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?
    1. September 3, 2014 7:32 pm

      This might help ? http://www.howforge.com/how-install-oci8-php-5-ubuntu#comment-3834
    2. André

      September 3, 2014 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
      1. September 3, 2014 7:48 pm

        i am hoping you have root access ?
      2. André

        September 3, 2014 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...
    3. September 3, 2014 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
  15. Sid

    May 29, 2014 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.
  16. Mohammad Ali

    April 28, 2014 11:44 am

    can you tell me the procedure for windows 7 ?
    1. April 28, 2014 9:05 pm

      a quick google search gave me this - hope it helps http://www.codeproject.com/Tips/523381/PHP-with-Oracle-g-on-Windows-bit http://www.oracle.com/technetwork/articles/dsl/technote-php-instant-084410.html
  17. Francesco

    April 2, 2014 6:42 am

    Matt : hmmmm…. Using instant client 12.1.0.1.0, I get ORA-03134: Connections to this server version are no longer supported. How do I uninstall v12.1.0.1.0 to try an older version?
    I've the same problem...can anyone helps me? :) Thanks a lot
    1. Matt

      April 3, 2014 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. Matt

    February 27, 2014 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
    1. February 27, 2014 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
      1. Matt

        February 27, 2014 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
      2. Matt

        March 4, 2014 10:22 pm

        hmmmm.... Using instant client 12.1.0.1.0, I get ORA-03134: Connections to this server version are no longer supported. How do I uninstall v12.1.0.1.0 to try an older version?
  19. February 26, 2014 1:59 pm

    Eko, 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
  20. February 26, 2014 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 ? http://www.dzone.com/snippets/let-php-show-all-errors here is the way to connect to oracle - ( see example 2 ) http://us1.php.net/manual/en/function.oci-connect.php
    1. February 26, 2014 2:39 am

      also try putting oci_internal_debug(true); before you connect
      1. Eko Heri

        February 26, 2014 3:22 am

        Dear Naveen, I've tried it and change the code like this: echo "Its here"; oci_internal_debug(true); error_reporting(E_ALL); ini_set('display_errors', '1'); $db = '(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.1)(PORT = 1521)))(CONNECT_DATA=(SID=DBEAI)))'; if(!@($conn = oci_connect('user','pass',$db))) { echo " and die"; $e = oci_error(); print_r($e); } 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 :) Thanks
  21. Eko Heri

    February 26, 2014 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. Thanks
    1. February 26, 2014 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 ?
      1. Eko Heri

        February 26, 2014 2:23 am

        Sorry i think the script is missing. Here are the script: echo "Its here"; $db = '(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.1)(PORT = 1521)))(CONNECT_DATA=(SID=DBEAI)))'; if(!@($conn = oci_connect('user','pass',$db))) { echo " and die"; oci_internal_debug(TRUE); $e = oci_error(); print_r($e); } I think the credential is correct because i can run this script in my partner PC which run with XAMPP and Windows.
  22. December 17, 2013 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-10.2.0.5.0-linux-x64.zip, basic-10.2.0.5.0-linux-x64 Also I added extension entry in /etc/php5/apache2/php.ini Any clue
    1. December 18, 2013 8:22 pm

      i am going to try this on a 64 bit and i will let you know
      1. December 19, 2013 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
  23. December 17, 2013 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.
    1. December 17, 2013 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
  24. November 5, 2013 12:35 pm

    after add oci8.so to ini files remove single quates from newly added lines 'extension=oci8.so' to extension=oci8.so
  25. July 27, 2013 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
    1. July 27, 2013 7:37 pm

      what OS are you trying this on ?
      1. alex

        July 28, 2013 5:40 am

        it's ubuntu 13.04
      2. alex

        July 28, 2013 6:18 am

        ubuntu 13.04 64bit
    2. alex

      July 28, 2013 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 thnx
    3. alex

      July 28, 2013 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...
      1. Chris

        November 1, 2013 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!
  26. Akash

    July 2, 2013 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 ???
    1. July 2, 2013 3:11 pm

      this means the installation failed - check your phpinfo information - can you see the oci8 extension block ?
      1. Akash

        July 2, 2013 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
        1. July 2, 2013 7:00 pm

          Akash, 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
  27. Omar

    June 27, 2013 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
    1. June 27, 2013 1:22 pm

      Thanks for the tip Omar - i will add it to the post
      1. March 7, 2016 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,
  28. David Costello

    June 20, 2013 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?
    1. June 20, 2013 6:31 pm

      make sure you have both the oracle libraries of the same version - the current version is Version 11.2.0.3.0 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 )
  29. David Costello

    June 20, 2013 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
    1. June 20, 2013 5:27 pm

      David, 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
  30. December 28, 2012 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
  31. December 19, 2012 12:57 pm

    thanks for the tutorial, all work fine ;) but I cant use sqlplus at all, any solution??
    1. December 20, 2012 8:12 pm

      Fery, 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
      1. December 28, 2012 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 ;)
  32. Francesco

    November 28, 2012 9:46 am

    THANKS!!! You made my day ;)
  33. moti

    October 30, 2012 3:19 pm

    Great guide, I step by step and everything works !!! KUWTGJ :)

Post a comment

Your email address will not be published. Required fields are marked *