Skip to content

Instantly share code, notes, and snippets.

@eSkiSo
Last active October 31, 2024 14:26
Show Gist options
  • Save eSkiSo/781269c79b4dd740e90fcc059c1985ae to your computer and use it in GitHub Desktop.
Save eSkiSo/781269c79b4dd740e90fcc059c1985ae to your computer and use it in GitHub Desktop.
Install OCI8 (Oracle) in Ubuntu 20 with PHP 7.4

Install OCI8 on Ubuntu 20.04 with PHP 7.4

Step 1

Download Oracle Instant Client and SDK from Oracle:

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

Download files:

Basic Package (ZIP): instantclient-basic-linux.x64-21.1.0.0.0.zip

SDK Package (ZIP): instantclient-sdk-linux.x64-21.1.0.0.0.zip

Step 2

Create new folders for Oracle Instant Client on server

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

Step 3

Extract files

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

    cd /usr/lib/oracle/21.1/client64

    sudo unzip instantclient-basic-linux.x64-21.1.0.0.0.zip
    sudo unzip instantclient-sdk-linux.x64-21.1.0.0.0.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 libaio1

Step 8

Update PECL to install OCI8 from it

	sudo pecl channel-update pecl.php.net

Install OCI8 from PCEL (desired version, default seems not to find it for me, i used php7.)

    sudo pecl install oci8 (php 8)
    sudo pecl install oci8-2.2.0  (php 7.)

During install it will require the path to Instant Client, write this:

	instantclient,/usr/lib/oracle/21.1/client64/lib

At the end something like this should appear:

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

Step 9

Load OCI8 into PHP

Add to mods-available

	cd /etc/php/7.4/mods-available/
	sudo nano oci.ini

Add this to the file:

extension = oci8.so

Step 10

Create sym link to the created ini file

	cd /etc/php/7.4/apache2/conf.d
	sudo ln -s /etc/php/7.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

Credits

Based on a post from Carlos Juan

https://www.cjorellana.net/2020/12/como-instalar-oci8-en-ubuntu-2004-y-php.html

@ahmadasjad
Copy link

getting error for:
Ubuntu
Linux *** 5.15.0-1013-oracle #17-Ubuntu SMP Fri Jun 24 10:14:16 UTC 2022 aarch64 aarch64 aarch64 GNU/Linux
PHP 8.1.2 (cli) (built: Jun 13 2022 13:52:54) (NTS)

Followed instruction:
https://docs.oracle.com/en/cloud/paas/autonomous-database/dedicated/adbbw/index.html
https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html

/bin/bash /tmp/pear/temp/pear-build-rootJI40hE/oci8-3.2.1/libtool --mode=link cc -shared -I/tmp/pear/temp/pear-build-rootJI40hE/oci8-3.2.1/include -I/tmp/pear/temp/pear-build-rootJI40hE/oci8-3.2.1/main -I/tmp/pear/temp/oci8 -I/usr/include/php/20210902 -I/usr/include/php/20210902/main -I/usr/include/php/20210902/TSRM -I/usr/include/php/20210902/Zend -I/usr/include/php/20210902/ext -I/usr/include/php/20210902/ext/date/lib -I/opt/oracle/instantclient_21_7/sdk/include -DHAVE_CONFIG_H -g -O2 -o oci8.la -export-dynamic -avoid-version -prefer-pic -module -rpath /tmp/pear/temp/pear-build-rootJI40hE/oci8-3.2.1/modules oci8.lo oci8_lob.lo oci8_statement.lo oci8_collection.lo oci8_interface.lo oci8_failover.lo -Wl,-rpath,/opt/oracle/instantclient_21_7 -L/opt/oracle/instantclient_21_7 -lclntsh libtool: link: cc -shared -fPIC -DPIC .libs/oci8.o .libs/oci8_lob.o .libs/oci8_statement.o .libs/oci8_collection.o .libs/oci8_interface.o .libs/oci8_failover.o -L/opt/oracle/instantclient_21_7 -lclntsh -g -O2 -Wl,-rpath -Wl,/opt/oracle/instantclient_21_7 -Wl,-soname -Wl,oci8.so -o .libs/oci8.so /usr/bin/ld: skipping incompatible /opt/oracle/instantclient_21_7/libclntsh.so when searching for -lclntsh /usr/bin/ld: cannot find -lclntsh: No such file or directory /usr/bin/ld: skipping incompatible /opt/oracle/instantclient_21_7/libclntsh.so when searching for -lclntsh collect2: error: ld returned 1 exit status make: *** [Makefile:224: oci8.la] Error 1 ERROR: make' failed`

@ahmadasjad
Copy link

@enboig
Copy link

enboig commented Jul 26, 2022

My problem was having multiple PHP versions installed, it was using the wrong php-dev version.

It fails for me, using kubuntu 20.04, OCI 21.6, and php7.2 (from https://launchpad.net/~ondrej/+archive/ubuntu/php) The error occurs on sudo pecl install oci8-2.2.0:

...................
libtool: compile:  cc -I. -I/tmp/pear/temp/oci8 -I/tmp/pear/temp/pear-build-rootD9uBmD/oci8-2.2.0/include -I/tmp/pear/temp/pear-build-rootD9uBmD/oci8-2.2.0/main -I/tmp/pear/temp/oci8 -I/usr/include/php/20210902 -I/usr/include/php/20210902/main -I/usr/include/php/20210902/TSRM -I/usr/include/php/20210902/Zend -I/usr/include/php/20210902/ext -I/usr/include/php/20210902/ext/date/lib -I/usr/lib/oracle/21.6/client64/lib/sdk/include -DHAVE_CONFIG_H -g -O2 -DZEND_COMPILE_DL_EXT=1 -c /tmp/pear/temp/oci8/oci8_interface.c -MMD -MF oci8_interface.dep -MT oci8_interface.lo  -fPIC -DPIC -o .libs/oci8_interface.o
/tmp/pear/temp/oci8/oci8_interface.c: In function ‘zif_oci_fetch_all’:
/tmp/pear/temp/oci8/oci8_interface.c:1476:3: warning: implicit declaration of function ‘Z_PARAM_ZVAL_DEREF_EX’; did you mean ‘Z_PARAM_ZVAL_EX’? [-Wimplicit-function-declaration]
 1476 |   Z_PARAM_ZVAL_DEREF_EX(array, 0, 1)
      |   ^~~~~~~~~~~~~~~~~~~~~
      |   Z_PARAM_ZVAL_EX
/tmp/pear/temp/oci8/oci8_interface.c:1476:37: error: expected ‘;’ before ‘_optional’
 1476 |   Z_PARAM_ZVAL_DEREF_EX(array, 0, 1)
      |                                     ^
      |                                     ;
make: *** [Makefile:222: oci8_interface.lo] Error 1
ERROR: `make' failed

Any idea?

@fxcosta
Copy link

fxcosta commented Jul 29, 2022

thanks a lot!!!!

@fxcosta
Copy link

fxcosta commented Jul 29, 2022

Just in case it wasn't clear, you also need to add the oci8.so module in the fpm php.ini:

sudo echo "extension=oci8.so" >> /etc/php/7.4/fpm/php.ini
service php7.4-fpm restart

For me it only worked after that because the cli configuration files are different from the ones used in http

@Ahmadfauzi1111
Copy link

any Idea ??

/tmp/pear/temp/oci8/oci8_interface.c: In function ‘zif_oci_fetch_all’:
/tmp/pear/temp/oci8/oci8_interface.c:1476:3: warning: implicit declaration of function ‘Z_PARAM_ZVAL_DEREF_EX’; did you mean ‘Z_PARAM_ZVAL_EX’? [-Wimplicit-function-declaration]
1476 | Z_PARAM_ZVAL_DEREF_EX(array, 0, 1)
| ^~~~~~~~~~~~~~~~~~~~~
| Z_PARAM_ZVAL_EX
/tmp/pear/temp/oci8/oci8_interface.c:1476:37: error: expected ‘;’ before ‘_optional’
1476 | Z_PARAM_ZVAL_DEREF_EX(array, 0, 1)
| ^
| ;
make: *** [Makefile:222: oci8_interface.lo] Error 1
ERROR: `make' failed

@eSkiSo
Copy link
Author

eSkiSo commented Aug 3, 2022

any Idea ??

/tmp/pear/temp/oci8/oci8_interface.c: In function ‘zif_oci_fetch_all’: /tmp/pear/temp/oci8/oci8_interface.c:1476:3: warning: implicit declaration of function ‘Z_PARAM_ZVAL_DEREF_EX’; did you mean ‘Z_PARAM_ZVAL_EX’? [-Wimplicit-function-declaration] 1476 | Z_PARAM_ZVAL_DEREF_EX(array, 0, 1) | ^~~~~~~~~~~~~~~~~~~~~ | Z_PARAM_ZVAL_EX /tmp/pear/temp/oci8/oci8_interface.c:1476:37: error: expected ‘;’ before ‘_optional’ 1476 | Z_PARAM_ZVAL_DEREF_EX(array, 0, 1) | ^ | ; make: *** [Makefile:222: oci8_interface.lo] Error 1 ERROR: `make' failed

Are you using php v7.4?
That error seems like the one “enboig” was having https://gist.github.com/eSkiSo/781269c79b4dd740e90fcc059c1985ae?permalink_comment_id=4244857#gistcomment-4244857

@Ahmadfauzi1111
Copy link

yes I have changed several instant clients but still the same error

@eSkiSo
Copy link
Author

eSkiSo commented Aug 3, 2022

What version of PHP are you using?

@aashestakov
Copy link

If there are several php versions in your system or you are using version 7.4 for example then try to claim version via special flag:
sudo pecl -d php_suffix=7.4 install oci8-2.2.0

@lakatosgabor
Copy link

Hi.

My error:
No releases available for package "pecl.php.net/oci8"
Install failed

If the pecl install not working, try it donwload manuality.
Check this website: https://www.php.net/manual/en/oci8.installation.php
I used phpize "Installing OCI8 from PECL Using phpize"

Good luck
G.

@eSkiSo
Copy link
Author

eSkiSo commented Jan 19, 2023

You maybe were missing to update the pecl list:

sudo pecl channel-update pecl.php.net

@lakatosgabor
Copy link

No, i did try update the pecl. It's not working. I did download with manuality, and it's fine.

@lhervi
Copy link

lhervi commented Mar 1, 2023

aashestakov

God bless you aashestakov :)

@ngoanhphong19730708
Copy link

ngoanhphong19730708 commented Apr 27, 2023

I installed, all the step are the same guide. After that php not working . I can not run any php file.
Pls tell me why ?. Thanks

@acaciobernardo
Copy link

Thank you!!

@Ishak-ali
Copy link

Thank you so much Eskiso, it worked like charm

@hopeseekr
Copy link

hopeseekr commented Jan 13, 2024

Hi the process work fine till step 8, when i run "sudo pecl install oci8-2.2.0" i have got this response

Notice: Trying to access array offset on value of type bool in PEAR/REST.php on line 187 PHP Notice: Trying to access array offset on value of type bool in /usr/share/php/PEAR/REST.php on line 187 No releases available for package "pecl.php.net/oci8" install failed and can't find any way to go further!!

I’m on PHP 7.4.2, have found how to “Manually Installing OCI8 on Linux as a Shared Extension” here , but i’d like understand the pecl problem.

Hey @marcoriccine: This problem is caused when your PHP doesn’t have ext-xml installed. I found that out while running this very tutorial and getting blocked myself.

@hopeseekr
Copy link

For PHP 8.1 and above, you have to use pecl install oci8-3.2.1.

What a mess! The extension writers should hold their heads in absolute shame for pushing distinct PECL packages per oracle versions. Can you imagine if we had 7 different mysqli extensions for each version of MySQL??

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