Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 48 You must be signed in to star a gist
  • Fork 27 You must be signed in to fork a gist
  • Save Yukibashiri/cebaeaccbe531665a5704b1b34a3498e to your computer and use it in GitHub Desktop.
Save Yukibashiri/cebaeaccbe531665a5704b1b34a3498e to your computer and use it in GitHub Desktop.
How to install OCI8 on Ubuntu 18.04 and PHP 7.2 (Instant Client 12.2)

How to install OCI8 on Ubuntu 18.04 and PHP 7.2

Source: http://www.syahzul.com/2016/04/06/how-to-install-oci8-on-ubuntu-14-04-and-php-5-6/

Source: https://medium.com/@kabeza/setup-oracle-instant-client-with-apache-and-php-7-on-ubuntu-server-17-04-66cea2297d6f

Source: https://gist.github.com/hewerthomn/81eea2935051eb2500941a9309bca703#file-install_oci8_ubuntu_16-04_php7-1-md

Install Oracle Instant Client and SDK

Step 1

Download the Oracle Instant Client and SDK from Oracle website. (Need to login in Oracle page)

http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html

Files: instantclient-basic-linux.x64-12.2.0.1.0.zip and instantclient-sdk-linux.x64-12.2.0.1.0.zip.

Step 2

Create a new folder to store Oracle Instant Client zip files on your server.

Upload the Instant Clients files inside this folder.

mkdir /opt/oracle

Step 3

Now we need to extract the files.

cd /opt/oracle
unzip instantclient-basic-linux.x64-12.2.0.1.0.zip
unzip instantclient-sdk-linux.x64-12.2.0.1.0.zip

Step 4

Next, we need to create a symlink to Instant Client files.

ln -s /opt/oracle/instantclient_12_2/libclntsh.so.12.1 /opt/oracle/instantclient_12_2/libclntsh.so
ln -s /opt/oracle/instantclient_12_2/libocci.so.12.1 /opt/oracle/instantclient_12_2/libocci.so

Step 5

Add the folder to our ldconfig.

echo /opt/oracle/instantclient_12_2 > /etc/ld.so.conf.d/oracle-instantclient

Step 6

Update the Dynamic Linker Run-Time Bindings

ldconfig

Done. Now we can proceed to the next part.

Install Additional Packages

To install the OCI8 extension, we need to install some additional package on our server.

Step 1

Run these command:

apt-get install php-dev php-pear build-essential libaio1

Step 2

Once installed, we need to get the OCI8 file. But, before that we need to update PECL channel.

pecl channel-update pecl.php.net

Then.

pecl install oci8

When you are prompted for the Instant Client location, enter the following:

instantclient,/opt/oracle/instantclient_12_2

Step 3

We need to tell PHP to load the OCI8 extension.

echo "extension =oci8.so" >> /etc/php/7.2/fpm/php.ini
echo "extension =oci8.so" >> /etc/php/7.2/cli/php.ini
echo "extension =oci8.so" >> /etc/php/7.2/apache2/php.ini

We also need to add on apache those environment variables.

echo "export LD_LIBRARY_PATH=/opt/oracle/instantclient_12_2" >> /etc/apache2/envvars
echo "export ORACLE_HOME=/opt/oracle/instantclient_12_2" >> /etc/apache2/envvars

echo "LD_LIBRARY_PATH=/opt/oracle/instantclient_12_2:$LD_LIBRARY_PATH" >> /etc/environment

Step 4

Refresh the server. If you are accessing through SSH, then

exit

or

sudo shutdown -r now

Check if the extension is enabled.

php -m | grep 'oci8'

If returns oci8, its works!

Step 5

Restart the PHP-FPM

service php7.2-fpm restart

Now you can connect to Oracle DBMS from your PHP applications.

@maxgr0
Copy link

maxgr0 commented Aug 21, 2019

replace step 5 with:
echo "/usr/lib/oracle/19.3/client64/lib" | sudo tee -a /etc/ld.so.conf.d/oracle.conf

to make it working

@PhouvanhKCSV
Copy link

Step 5, after adding ".conf" then it is working

echo /opt/oracle/instantclient_12_2 > /etc/ld.so.conf.d/oracle-instantclient.conf

@gabbykweka
Copy link

gabbykweka commented Nov 25, 2019

For the error PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/20170718/oci8.so ..., My case was not Step 5. oci8.so file was installed in different PHP Version. For me it was installed in PHP7.3. I had to run the below lines to change default PHP version to 7.2 and reinstall oic8
sudo update-alternatives --set phpize /usr/bin/phpize7.2
sudo update-alternatives --set php /usr/bin/php7.2
sudo update-alternatives --set php-config /usr/bin/php-config7.2

@nwaweru
Copy link

nwaweru commented Dec 10, 2019

OMG! Please update step 5 i.e. add the .conf to the oracle configuration file:

echo /opt/oracle/instantclient_12_2 > /etc/ld.so.conf.d/oracle-instantclient.conf

@Apatrid
Copy link

Apatrid commented Dec 13, 2019

Installing additional packages / Step 1 - be sure to use php7.2 version:
apt-get install php7.2-dev php-pear build-essential libaio1

@smartyaunt
Copy link

php-fpm un work, cli is ok, please help me

@AriffAzmi
Copy link

Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.1 LTS
Release:	18.04
Codename:	bionic
PHP 7.2.18-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: May  3 2019 09:24:01) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.18-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

Did exactly the steps and found out libclntsh.so.12.1 is not found.

 jay@ThinkPad  /opt/oracle  ldd /usr/lib/php/20170718/oci8.so
	linux-vdso.so.1 (0x00007ffd58797000)
	libgtk3-nocsd.so.0 => /usr/lib/x86_64-linux-gnu/libgtk3-nocsd.so.0 (0x00007f05c2e8b000)
	libclntsh.so.12.1 => not found
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f05c2a9a000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f05c2896000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f05c2677000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f05c32c0000)

Tried:

export LD_LIBRARY_PATH=/opt/oracle/instantclient_12_2
sudo sh -c "echo /opt/oracle/instantclient_12_2 > /etc/ld.so.conf.d/oracle-instantclient.conf"
sudo ldconfig

And then:

 ✘ jay@ThinkPad  /opt/oracle  php -m | grep 'oci8'
oci8

References:
ldconfig what?
libclntsh.so not found
unable to load oci8 on php7.2

Thank you very very much for usefull tips. It works !

@clcneogeek325
Copy link

For the error PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/20170718/oci8.so ..., My case was not Step 5. oci8.so file was installed in different PHP Version. For me it was installed in PHP7.3. I had to run the below lines to change default PHP version to 7.2 and reinstall oic8
sudo update-alternatives --set phpize /usr/bin/phpize7.2
sudo update-alternatives --set php /usr/bin/php7.2
sudo update-alternatives --set php-config /usr/bin/php-config7.2

I had the same problem, I deleted php7.4 and the problem was solved

@kczereczon
Copy link

kczereczon commented May 25, 2020

Thanks for the rundown. Works except for one step point.

After following all the steps, php -m gave the following warning:

PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/20170718/oci8.so' (tried: /usr/lib/php/20170718/oci8.so (libmql1.so: cannot open shared object file: No such file or directory), /usr/lib/php/20170718//usr/lib/php/20170718/oci8.so.so (/usr/lib/php/20170718//usr/lib/php/20170718/oci8.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0

Which means that libmql1.so could not be found. The solution:

Rename the file /etc/ld.so.conf.d/oracle-instantclient to /etc/ld.so.conf.d/oracle-instantclient.conf. I noticed that /etc/ld.so.conf loads files looking like /etc/ld.so.conf.d/*.conf, thus the rename.

Check that your system can find/resolve all the shared libraries with:

ldd /usr/lib/php/20170718/oci8.so

I had the same problem.

I had to set up LD_LIBRARY_PATH correctly I've added
export LD_LIBRARY_PATH={path}:$LD_LIBRARY_PATH
it into /home/{user}/.bashrc instead of /etc/environment

You can check you variable with
echo $LD_LIBRARY_PATH
if your path show up, everything is setup correctly

@gustavoraamos
Copy link

Thanks for the rundown. Works except for one step point.

After following all the steps, php -m gave the following warning:

PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/20170718/oci8.so' (tried: /usr/lib/php/20170718/oci8.so (libmql1.so: cannot open shared object file: No such file or directory), /usr/lib/php/20170718//usr/lib/php/20170718/oci8.so.so (/usr/lib/php/20170718//usr/lib/php/20170718/oci8.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0

Which means that libmql1.so could not be found. The solution:

Rename the file /etc/ld.so.conf.d/oracle-instantclient to /etc/ld.so.conf.d/oracle-instantclient.conf. I noticed that /etc/ld.so.conf loads files looking like /etc/ld.so.conf.d/*.conf, thus the rename.

Check that your system can find/resolve all the shared libraries with:

ldd /usr/lib/php/20170718/oci8.so

Nicely done, mate!

I had this exact problem and I did manage to solve doing what you suggested.

P.S. after rename the file with .conf, and before the ldd command, I had to execute ldconfig first to load the change.

Thank you!

@stenioanibal
Copy link

Be carefull with the latest update of OCI8 in pecl (3.0.0, released in NOV/2020). I losed about 2 hours with this, trying to install and it's dies in mid of make install proccess. All of my alternatives is gone and, when I tryed to install the 2.2.0 version (1 before the latest), just works like a charm.

@gigip
Copy link

gigip commented Dec 11, 2020

Be carefull with the latest update of OCI8 in pecl (3.0.0, released in NOV/2020). I losed about 2 hours with this, trying to install and it's dies in mid of make install proccess. All of my alternatives is gone and, when I tryed to install the 2.2.0 version (1 before the latest), just works like a charm.

Yeah, that's because php8 came out, so now pecl install oci8 will install 3.0 by default. Now you need to run pecl install oci8-2.2.0 if you have php < 8

@rarangels
Copy link

I have the following problem: could not find driver

In phpinfo():

OCI8 Support enabled
OCI8 DTrace Support disabled
OCI8 Version 2.2.0
Oracle Run-time Client Library Version 12.2.0.1.0
Oracle Compile-time Instant Client Version 12.2

In console, i use: php -m | grep 'oci8' and I get: oci8

My test code php. Any ideas?

$this->DB = "";
        $this->SERVER = "";
        $this->USER = "";
        $this->PASS = "";
        $this->tns = "(
					DESCRIPTION =
					(
						ADDRESS_LIST =
						(	
							ADDRESS = 
							(
								PROTOCOL = TCP
							)
							(
								HOST =".$this->SERVER."
							)
							(
								PORT = 1521
							)
						)
					)
					(
						CONNECT_DATA =
						(
							SERVICE_NAME = ".$this->DB."
						)
					)
				)";

        try {
            $this->conn = new PDO("oci:dbname=".$this->tns, $this->USER, $this->PASS);
            if ($this->conn) {
                echo('se conecto');
            }
        } catch (PDOException $e) {
            echo('Error: '.$e->getMessage());
        }

@lulzimv
Copy link

lulzimv commented Feb 16, 2021

Hi
Unable to work this tutorial , l have ubuntu 18.4 and using behind proxy , but when l execute sudo pecl install oci8 , it waiting and then error , after a read error with -vvv , its require php8.0 , installed php8.0 then l got some other error

is anyone manage to install ?!

@gigip
Copy link

gigip commented Feb 16, 2021

Hi
Unable to work this tutorial , l have ubuntu 18.4 and using behind proxy , but when l execute sudo pecl install oci8 , it waiting and then error , after a read error with -vvv , its require php8.0 , installed php8.0 then l got some other error

is anyone manage to install ?!

Read my comment above.

@Rimicsani
Copy link

Hi everyone
What are the main changes to apply on nginx and php8?
Thanks a lot!

@lucassmacedo
Copy link

The OCI8 extension lets you access Oracle Database.

Use 'pecl install oci8' to install for PHP 8.

Use 'pecl install oci8-2.2.0' to install for PHP 7.

Use 'pecl install oci8-2.0.12' to install for PHP 5.2 - PHP 5.6.

Use 'pecl install oci8-1.4.10' to install for PHP 4.3.9 - PHP 5.1.

The OCI8 extension can be linked with Oracle client libraries from Oracle Database 10.2 or later. These libraries are found in your database installation, or in the free Oracle Instant Client from https://www.oracle.com/database/technologies/instant-client.html.

Oracle's standard cross-version connectivity applies. For example, PHP OCI8 linked with Instant Client 19c can connect to Oracle Database 11.2 onward. See Oracle's note "Oracle Client / Server Interoperability Support" (ID 207303.1) for details.

@arderyp
Copy link

arderyp commented Jun 4, 2021

Imagine an alternate universe where you could simply issue apt install php7.4-oci8, like we do for every other php module...

Why on earth is this still so convoluted?

@drupol
Copy link

drupol commented Jul 5, 2021

Imagine an alternate universe where you could simply issue apt install php7.4-oci8, like we do for every other php module...

Why on earth is this still so convoluted?

Because AFAIK, Oracle doesn't allow his stuff to be bundled.

@clcneogeek325
Copy link

personally I am solving this using docker containers, is easier.

@drupol
Copy link

drupol commented Jul 5, 2021

personally I am solving this using docker containers, is easier.

Which one are you using ?

@arderyp
Copy link

arderyp commented Jul 6, 2021

@drupol, yes, i forgot the /s. Oracle has made this bed for us, and now those poor unfortunate souls among us who are forced to use their products must sleep in it. I hope to drop them as a dependency altogether in the coming weeks. 🤞

@KasparAas
Copy link

KasparAas commented May 30, 2022

When doing php --version I always get

Warning: PHP Startup: Unable to load dynamic library 'oci8.so' (tried: /opt/homebrew/Cellar/php@7.4/7.4.29/pecl/20190902/oci8.so (dlopen(/opt/homebrew/Cellar/php@7.4/7.4.29/pecl/20190902/oci8.so, 0x0009): symbol not found in flat namespace '_OCIAttrGet'), /opt/homebrew/Cellar/php@7.4/7.4.29/pecl/20190902/oci8.so.so (dlopen(/opt/homebrew/Cellar/php@7.4/7.4.29/pecl/20190902/oci8.so.so, 0x0009): tried: '/opt/homebrew/Cellar/php@7.4/7.4.29/pecl/20190902/oci8.so.so' (no such file), '/opt/homebrew/lib/php/pecl/20190902/oci8.so.so' (no such file))) in Unknown on line 0

I use osx and Valet for PHP (7.4)

I've followed this tutorial:

What is there that I'm missing?

@Apatrid
Copy link

Apatrid commented May 30, 2022

@markup-we This tutorial is for PHP 7.2 and you are using 7.4 ?

@sambolek
Copy link

Got the same issue on M1 mac. Related: https://gist.github.com/nasrulhazim/308c9f59a2ddba364b4b908ee9238265?permalink_comment_id=4028646#gistcomment-4028646

Unfortunately, haven't found a way to run oci8 on M1 mac, but others reported running homebrew under Rosetta works (but I don't want to re-install my entire valet-based system to do it).

Might have to do it via docker.

@mfeilen
Copy link

mfeilen commented Oct 19, 2023

If someone needs to install it on Ubunutu 22.04 here are some suggestions:

change step 2 pecl install oci8 to:
Temporary change the php version systemwide to 7.2 (got that from https://stackoverflow.com/questions/66850189/php-oci8-installation-fails-on-ubuntu-18)

update-alternatives --set phpize /usr/bin/phpize7.2
update-alternatives --set php /usr/bin/php7.2
update-alternatives --set php-config /usr/bin/php-config7.2

You may want to revert this later. Check /etc/alternatives symlinks for php, phpize and php-config to the right binary. If php-config does not exists for other versions, the required php-dev package may not installed.

Then compile with right oci version - otherwise you will get some compatibility compile errors

pecl install oci8-2.2.0 

change step 3 to:

echo "extension=oci8.so" >/etc/php/7.2/mods-available/oc8.ini
ln -s /etc/php/7.2/mods-available/oci8.ini /etc/php/7.2/cli/conf.d/20-oci8.ini
ln -s /etc/php/7.2/mods-available/oci8.ini /etc/php/7.2/cgi/conf.d/20-oci8.ini
ln -s /etc/php/7.2/mods-available/oci8.ini /etc/php/7.2/apache2/conf.d/20-oci8.ini

@drupol
Copy link

drupol commented Oct 19, 2023

An alternative would be use Nix, and there you can use any version of PHP/oci8 that you want without messing with your underlaying OS.

@rusman-plat-d
Copy link

php-fpm un work, cli is ok, please help me

have you solve it? i have same problem

@KasparAas
Copy link

I was able to get it working on PHP 8.2 using Docker. I downloaded the needed ZIP files and added them to my docker/ folder. This is the part from Docker:

Install Oracle Instantclient

RUN mkdir /opt/oracle

COPY ./docker/instantclient-basic-linux.x64-21.6.0.0.0dbru.zip /opt/oracle
COPY ./docker/instantclient-sdk-linux.x64-21.6.0.0.0dbru.zip /opt/oracle
COPY ./docker/instantclient-sqlplus-linux.x64-21.6.0.0.0dbru.zip /opt/oracle

RUN unzip /opt/oracle/instantclient-basic-linux.x64-21.6.0.0.0dbru.zip -d /opt/oracle
&& unzip /opt/oracle/instantclient-sdk-linux.x64-21.6.0.0.0dbru.zip -d /opt/oracle
&& unzip /opt/oracle/instantclient-sqlplus-linux.x64-21.6.0.0.0dbru.zip -d /opt/oracle
&& rm -rf /opt/oracle/*.zip
&& mv /opt/oracle/instantclient_21_6 /opt/oracle/instantclient

ARG ORACLE_HOME=/opt/oracle/
ENV ORACLE_HOME=/opt/oracle/
ARG LD_LIBRARY_PATH=/opt/oracle/instantclient/
ENV LD_LIBRARY_PATH /opt/oracle/instantclient:${LD_LIBRARY_PATH}
RUN ldconfig

for PHP 8.2

#RUN echo 'instantclient,/opt/oracle/instantclient' | pecl install oci8

for PHP 8.1

RUN echo 'instantclient,/opt/oracle/instantclient' | pecl install oci8-3.2.1

Install Oracle extensions

RUN docker-php-ext-enable oci8
&& docker-php-ext-configure pdo_oci --with-pdo-oci=instantclient,/opt/oracle/instantclient
&& docker-php-ext-install pdo_oci

@lucassmacedo
Copy link

FROM kooldev/php:8.1-nginx

ENV LD_LIBRARY_PATH /usr/local/instantclient
ENV ORACLE_HOME /usr/local/instantclient

Download and unarchive Instant Client v11

RUN apk add --update libaio libnsl &&
curl -o /tmp/instaclient-basic.zip https://raw.githubusercontent.com/bumpx/oracle-instantclient/master/instantclient-basic-linux.x64-11.2.0.4.0.zip &&
curl -o /tmp/instaclient-sdk.zip https://raw.githubusercontent.com/bumpx/oracle-instantclient/master/instantclient-sdk-linux.x64-11.2.0.4.0.zip &&
curl -o /tmp/instaclient-sqlplus.zip https://raw.githubusercontent.com/bumpx/oracle-instantclient/master/instantclient-sqlplus-linux.x64-11.2.0.4.0.zip &&
unzip -d /usr/local/ /tmp/instaclient-basic.zip &&
unzip -d /usr/local/ /tmp/instaclient-sdk.zip &&
unzip -d /usr/local/ /tmp/instaclient-sqlplus.zip &&
ln -s /usr/local/instantclient_11_2 ${ORACLE_HOME} &&
ln -s ${ORACLE_HOME}/libclntsh.so.* ${ORACLE_HOME}/libclntsh.so &&
ln -s ${ORACLE_HOME}/libocci.so.* ${ORACLE_HOME}/libocci.so &&
ln -s ${ORACLE_HOME}/lib* /usr/lib &&
ln -s ${ORACLE_HOME}/sqlplus /usr/bin/sqlplus &&
ln -s /usr/lib/libnsl.so.3 /usr/lib/libnsl.so.1 &&
docker-php-ext-configure oci8 --with-oci8=instantclient,$ORACLE_HOME &&
docker-php-ext-install oci8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment