Skip to content

Instantly share code, notes, and snippets.

@mazedlx
Last active February 21, 2024 10:05
Show Gist options
  • Save mazedlx/8e7764dfe68f08168d3f66a60256a863 to your computer and use it in GitHub Desktop.
Save mazedlx/8e7764dfe68f08168d3f66a60256a863 to your computer and use it in GitHub Desktop.
How to install OCI8 on Ubuntu 17.10 and PHP 7.1

How to install OCI8 on Ubuntu 17.10 and PHP 7.1

This guide refers to Instantclient Version 12.2.0.1.0

Install Oracle Instant Client and SDK

Step 1

Download the latest Oracle Instant Client and SDK from the Oracle website (Yeah, fuck you Oracle, you need to create an account to download the files).

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

Look for 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 the Oracle Instant Client files on your machine.

mkdir /opt/oracle

And move your files to /opt/oracle.

Step 3

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 symlink the .so files.

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

Step 5

Now lets add the folder to the ldconfig's config folder.

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

And update the dynamic linker runtime bindings.

ldconfig

Great! We're almost there!

Add the extension to PHP

To install the OCI8 extension, we need to get some additional packages.

Step 1

Run these commands to install the necessary packages:

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

Step 2

Install the oci8 extension via PECL.

echo "instantclient,/opt/oracle/instantclient_12_2" | pecl install oci8

Step 3

We now can tell PHP to load the OCI8 extension.

echo "extension=oci8.so" >> /etc/php/7.1/mods-available/oci8.ini

ln -s /etc/php/7.1/mods-available/oci8.ini /etc/php/7.1/cli/conf.d/20-oci8.ini

ln -s /etc/php/7.1/mods-available/oci8.ini /etc/php/7.1/fpm/conf.d/20-oci8.ini

Step 4

Check if the extension is enabled.

php -m | grep oci8
php-fpm7.1 -m | grep oci8

If you see oci8, you're almost done!

Step 5

Restart PHP-FPM.

service php7.1-fpm restart

Finished! Well done! Thanks for pulling through with this tedious procedure!

@C0rn3j
Copy link

C0rn3j commented Aug 8, 2019

Step 2 should be changed to read echo "instantclient,/opt/oracle/instantclient_19_3" | pecl install oci8 as that's the way to install it from a script too.

@mazedlx
Copy link
Author

mazedlx commented Aug 8, 2019

Step 2 should be changed to read echo "instantclient,/opt/oracle/instantclient_19_3" | pecl install oci8 as that's the way to install it from a script too.

@C0rn3j Thanks for the input. I changed it above.

@ambevgabriel
Copy link

ambevgabriel commented Sep 18, 2019

is not Working yet, when i gave the command php-fpm7.2 -m | grep oci8:
NOTICE: PHP message: PHP Warning: Module 'oci8' already loaded in Unknown on line 0
oci8
what i have to do ?

@mazedlx
Copy link
Author

mazedlx commented Sep 18, 2019

is not Working yet, when i gave the command php-fpm7.2 -m | grep oci8:
NOTICE: PHP message: PHP Warning: Module 'oci8' already loaded in Unknown on line 0
oci8
what i have to do ?

Take a look at /etc/php/7.1/mods-available/oci8.ini, I bet there are two lines like this:

extension=oci8.so
extension=oci8.so

Just delete one of those lines.

@ambevgabriel
Copy link

ambevgabriel commented Sep 19, 2019

is not Working yet, when i gave the command php-fpm7.2 -m | grep oci8:
NOTICE: PHP message: PHP Warning: Module 'oci8' already loaded in Unknown on line 0
oci8
what i have to do ?

Take a look at /etc/php/7.1/mods-available/oci8.ini, I bet there are two lines like this:

extension=oci8.so
extension=oci8.so

Just delete one of those lines.

i've deleted one, but the problem was in the installation, because the version the i had installed was the version 19_3. So if anyone has more question about i ask that to take a look the version you're installing. Thank's

@AlphaGeek509
Copy link

Dang I feel that I'm missing something. I followed things to a 'T'. I'm using /opt/oracle/instantclient_18_3 for my installation. I had this module working on php 7.2 since April and just yesterday upgraded to 7.3. Now things aren't coming back on line. I get down to php -m | grep oci8 and I have the following error.

$bash:/etc/php/7.3/mods-available$ php -i | grep ini
PHP Warning:  PHP Startup: Unable to load dynamic library 'oci8.so' (tried: /usr/lib/php/20180731/oci8.so (/usr/lib/php/20180731/oci8.so: cannot open shared object file: No such file or directory), /usr/lib/php/20180731/oci8.so.so (/usr/lib/php/20180731/oci8.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0

I've checked my oci8.ini file for duplicated lines like @mazedlx mentioned but that didn't work cause I only have a single line for an entry.

Any thoughts on what I can do?

@mazedlx
Copy link
Author

mazedlx commented Oct 10, 2019

I think the problem is that you've upgraded PHP to 7.3, but you haven't compiled oci8 for this version yet.

@AlphaGeek509
Copy link

@mazedlx - Thanks for the suggestion. Not sure what you mean that my OCI8 wasn't compiled for PHP 7.3. After some googling and thinking I decided to perform a

pecl uninstall oci8

I followed any prompts that came up. After the uninstall I started back over with the above tutorial at Step 2. Followed everything in the tutorial for my 18_3 install version and it worked. Thanks for your help.

@mazedlx
Copy link
Author

mazedlx commented Oct 10, 2019

@mazedlx - Thanks for the suggestion. Not sure what you mean that my OCI8 wasn't compiled for PHP 7.3. After some googling and thinking I decided to perform a

pecl uninstall oci8

I followed any prompts that came up. After the uninstall I started back over with the above tutorial at Step 2. Followed everything in the tutorial for my 18_3 install version and it worked. Thanks for your help.

I meant exactly that. 😊👍

@C0rn3j
Copy link

C0rn3j commented Feb 1, 2021

Still works for 20.04, tested with PHP 7.4 and 8.0 with Oracle 21.1.0.0.0

Only URLs have slightly changed and current version of oci8 is PHP 8.0 only.

rm -rf /opt/oracle
mkdir -p /opt/oracle && cd /opt/oracle
# Check for updates on https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html
wget https://download.oracle.com/otn_software/linux/instantclient/211000/instantclient-basic-linux.x64-21.1.0.0.0.zip
wget https://download.oracle.com/otn_software/linux/instantclient/211000/instantclient-sdk-linux.x64-21.1.0.0.0.zip
unzip instantclient-basic-linux.x64-21.1.0.0.0.zip 
unzip instantclient-sdk-linux.x64-21.1.0.0.0.zip
rm instantclient-basic-linux.x64-21.1.0.0.0.zip instantclient-sdk-linux.x64-21.1.0.0.0.zip
echo "/opt/oracle/instantclient_21_1" > /etc/ld.so.conf.d/oracle-instantclient.conf
ldconfig
pecl channel-update pecl.php.net
# Because install throws an error code if package is already installed
pecl uninstall oci8 
# https://pecl.php.net/package/oci8
# PHP 8.x
echo "instantclient,/opt/oracle/instantclient_21_1" | pecl install oci8
# PHP 7.x
#echo "instantclient,/opt/oracle/instantclient_21_1" | pecl install oci8-2.2.0
echo "extension=oci8.so" > /etc/php/8.0/mods-available/99-oracle-oci8.ini
phpenmod 99-oracle-oci8

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