Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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.

@beukeshu

This comment has been minimized.

Copy link

@beukeshu beukeshu commented Apr 4, 2019

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

@baig772

This comment has been minimized.

Copy link

@baig772 baig772 commented Apr 30, 2019

Followed the exact same steps but unable to get this working. OS is Ubuntu18.10, PHP7.2 (not fpm, installed as a service), Apache2.4. Can you help me here?

@ingeJulianLasso

This comment has been minimized.

Copy link

@ingeJulianLasso ingeJulianLasso commented Apr 30, 2019

Thank you very much for sharing, it worked perfectly with Ubuntu 18.04 and 16.04 with PHP 7.3.

Question, do you know how to enable pdo_oci or php_oci8? Thank you very much for any comments you may have on this.

@jmilagroso

This comment has been minimized.

Copy link

@jmilagroso jmilagroso commented May 18, 2019

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

@gigip

This comment has been minimized.

Copy link

@gigip gigip commented Jun 13, 2019

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

You, sir. thank you soooo much.
(Tested on Ubuntu disco)

@maxgr0

This comment has been minimized.

Copy link

@maxgr0 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

This comment has been minimized.

Copy link

@PhouvanhKCSV PhouvanhKCSV commented Sep 16, 2019

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

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

@gabbykweka

This comment has been minimized.

Copy link

@gabbykweka 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

This comment has been minimized.

Copy link

@nwaweru 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

This comment has been minimized.

Copy link

@Apatrid 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

This comment has been minimized.

Copy link

@smartyaunt smartyaunt commented Mar 7, 2020

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

@AriffAzmi

This comment has been minimized.

Copy link

@AriffAzmi AriffAzmi commented Mar 19, 2020

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

This comment has been minimized.

Copy link

@clcneogeek325 clcneogeek325 commented Apr 15, 2020

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

This comment has been minimized.

Copy link

@kczereczon 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

This comment has been minimized.

Copy link

@gustavoraamos gustavoraamos commented Oct 19, 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

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

This comment has been minimized.

Copy link

@stenioanibal stenioanibal 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.

@gigip

This comment has been minimized.

Copy link

@gigip 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

This comment has been minimized.

Copy link

@rarangels rarangels commented Feb 13, 2021

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

This comment has been minimized.

Copy link

@lulzimv 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

This comment has been minimized.

Copy link

@gigip 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

This comment has been minimized.

Copy link

@Rimicsani Rimicsani commented Mar 21, 2021

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

@lucassmacedo

This comment has been minimized.

Copy link

@lucassmacedo lucassmacedo commented Mar 23, 2021

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.

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