Oracle - How to install OCI8 with PHP8.4 in Ubuntu 24

Version Date Notes By
1.0 2025-02-05 Initial release BRB
1.1 2025-06-16 Updated for PHP 8.4 BRB

This is also the fix for the following error: Undefined constant "Yajra\Pdo\OCI_DEFAULT”.

Step 1

Download Oracle Instant Client and SDK from Oracle (this is for version 21.16):

wget https://download.oracle.com/otn_software/linux/instantclient/2116000/instantclient-basic-linux.x64-21.16.0.0.0dbru.zip
wget https://download.oracle.com/otn_software/linux/instantclient/2116000/instantclient-sdk-linux.x64-21.16.0.0.0dbru.zip

Step 2

Create a new directory for the Oracle Instant Client:

sudo mkdir /usr/lib/oracle/21.1/client64 -p

Step 3

Extract files:

sudo cp instantclient-basic-linux.x64-21.16.0.0.0dbru.zip /usr/lib/oracle/21.1/client64
sudo cp instantclient-sdk-linux.x64-21.16.0.0.0dbru.zip /usr/lib/oracle/21.1/client64

cd /usr/lib/oracle/21.1/client64

sudo unzip instantclient-basic-linux.x64-21.16.0.0.0dbru.zip
sudo unzip instantclient-sdk-linux.x64-21.16.0.0.0dbru.zip

sudo mv instantclient_21_1 lib

Step 4

Create symbolic link to the new Instant Client files:

cd /usr/lib/oracle/21.1/client64/lib/

sudo ln -s libclntsh.so.21.1 libclntsh.so (It may already exist, continue)
sudo ln -s libocci.so.21.1 libocci.so     (It may already exist, continue)

Step 5

Edit/Create this file with the path to the lib (for LDCONFIG):

sudo nano /etc/ld.so.conf.d/oracle.conf

Add this to the file and save: /usr/lib/oracle/21.1/client64/lib

Step 6

Update Dynamic Linker

ldconfig

Step 7

Install php-dev php-pear build-essential and libaio1

sudo apt-get install php-dev php-pear build-essential libaio1t64

Step 8

Update PECL to install OCI8 from it:

sudo pecl channel-update pecl.php.net

Install OCI8 from PECL:

sudo pecl install oci8

During install it will require the path to Instant Client, write this: instantclient,/usr/lib/oracle/21.1/client64/lib

At the end something similar to this should appear:

Build process completed successfully
Installing '/usr/lib/php/20240924/oci8.so'
install ok: channel://pecl.php.net/oci8-3.4.0
configuration option "php_ini" is not set to php.ini location
You should add "extension=oci8.so" to php.ini

NOTE:

If this does not show up correctly, PECL may still be using a prior PHP API version to compile the Oracle extension.

To go around this, you need to manually compile the oci8 extension, don't worry, it's pretty simple.

First, ensure phpize and php-config point to PHP 8.4 versions:

php -v
phpize -v
php-config --version

If not, you can alter them to point to the correct version:

sudo update-alternatives --config php
sudo update-alternatives --config phpize
sudo update-alternatives --config php-config

Download and compile oci8:

pecl download oci8
tar -xvzf oci8-*.tgz

cd oci8-*

phpize

./configure --with-oci8=instantclient,/usr/lib/oracle/21.1/client64/lib

make
sudo make install

If successful, it will output something like: Installing shared extensions: /usr/lib/php/20240924/

Step 9

Load OCI8 into PHP:

Add to mods-available

cd /etc/php/8.4/mods-available/
sudo vi oci.ini

Add this to the file: extension = oci8.so

Step 10

Create symlink to the created ini file:

cd /etc/php/8.4/apache2/conf.d
sudo ln -s /etc/php/8.4/mods-available/oci.ini 20-oci.ini

Restart Apache:

sudo systemctl restart apache2

Step 11 (Testing)

Check if OCI is loaded in PHP:

php -i | grep oci

Should output something like this:

oci8
oci8.connection_class => no value => no value
oci8.default_prefetch => 100 => 100
oci8.events => Off => Off
oci8.max_persistent => -1 => -1
oci8.old_oci_close_semantics => Off => Off
oci8.persistent_timeout => -1 => -1
oci8.ping_interval => 60 => 60
oci8.privileged_connect => Off => Off
oci8.statement_cache_size => 20 => 20

If you don't get this output, then the 20-oci.ini symlink may not be working, if this is the case, then add extension=oci8.so directly to your php.ini files for a quick fix in:

  • /etc/php/8.4/fpm/php.ini
  • /etc/php/8.4/cli/php.ini

Your Laravel application should now be able to connect to an external Oracle database without originating an error from PHP.

You can also clear PECL's cache: sudo pecl clear-cache

Guide's sources in case you have other issues:

  • https://gist.github.com/eSkiSo/781269c79b4dd740e90fcc059c1985ae
  • https://durak.org/sean/pubs/software/php-8.4.0/oci8.installation.html