Howto install Oracle OCI8 instantclient on Ubuntu 18.04
I want to share some information about my OCI8 installation to access Oracle databases.
If you got errors like "PHP Startup: Unable to load dynamic library 'oci8.so'"
or "checking Oracle Instant Client library version compatibility... configure: error: Link from libclntsh.so to /opt/oracle/instantclient/libclntsh.so.*.1 not found"
or just want to install Quick&Easy OCI8 to get a connection between your php and an Oracle database, you are at the right place!
Solution 1:
First prerequirements are a working apache2 and php7.2 (Ubunti 18.04) environement.
Download the basic (like instantclient-basic-linux.x64-12.2.0.1.0.zip) and the sdk (instantclient-sdk-linux.x64-12.2.0.1.0.zip) package from the Oracle Website http://www.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index.html
Upload both files to your webserver, you can use WinSCP for it
Unzip both files on server, in my case, you will get a new folder named "instantclient_12_2"
4a. Create destination folder
mkdir /opt/oracle
4b. Move and rename the instantclient folder
mv instantclient_12_2 /opt/oracle/instantclient
4c. Change rights on folder
chown -R root:www-data /opt/oracle
- Check if you have the required packages for installing OCI8
apt install php7.2-dev php-pear build-essential libaio1
- Create necessary soft links
ln -s /opt/oracle/instantclient/libclntsh.so.12.1 /opt/oracle/instantclient/libclntsh.so
ln -s /opt/oracle/instantclient/libocci.so.12.1 /opt/oracle/instantclient/libocci.so
7a. Add instant client to ld config files
echo /opt/oracle/instantclient > /etc/ld.so.conf.d/oracle-instantclient.conf
7b. Update Dynamic Linker Run-Time Bindings
ldconfig
8a. Now install OCI8 by pecl
pecl install oci8
8b. The OCI8 installation is asking you for the right folder
instantclient,/opt/oracle/instantclient
9a. Add oci lib to the cli php config (console php)
echo "extension = oci8.so" >> /etc/php/7.2/cli/php.ini
9b. Add oci lib to the apache php config
echo "extension = oci8.so" >> /etc/php/7.2/apache2/php.ini
10a. Set environement variables for the cli version (you will need to reboot the server after)
echo "LD_LIBRARY_PATH=\"/opt/oracle/instantclient\"" >> /etc/environment
echo "ORACLE_HOME=\"/opt/oracle/instantclient\"" >> /etc/environment
10b. Set environement variables for the apache version
echo "export LD_LIBRARY_PATH=\"/opt/oracle/instantclient\"" >> /etc/apache2/envvars
echo "export ORACLE_HOME=\"/opt/oracle/instantclient\"" >> /etc/apache2/envvars
- Restart Apache
service apache2 restart
- You're done, now you can test your connection to the Oracle database
=PHP CONNECTION EXAMPLE=
<?php
// Create connection to Oracle, change HOST IP and SID string!
$db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 000.000.000.000)(PORT = 1521)))(CONNECT_DATA=(SID=XXX)))";
// Enter here your username (DBUSER) and password!
$conn = oci_connect("DBUSER", "PASSWORD",$db);
if (!$conn) {
$m = oci_error();
echo $m['message']. PHP_EOL;
exit;
}
else {
print "Oracle database connection online". PHP_EOL;
}
?>