Oracle Instant Client + OCI8 on macOS Mojave for PHP
I have searched the web for days on how to install the Oracle database client on macOS for use with PHP 7 and read many, many, many instructions, tips and tricks. What I've learned from this adventure, is that everything 'Oracle' are some kind of 'I need a stiff drink', self-torture processes. So from all I have read and wasted my time on, I compiled the steps I took to get the client running on my iMac and MacBook Pro, both running Mojave.
Tweet
1) Oracle software
Download the following 3 files from Oracle into your Downloads-folder:
instantclient-basic-macos.x64-19.3.0.0.0dbru.zip
instantclient-sdk-macos.x64-19.3.0.0.0dbru.zip
instantclient-sqlplus-macos.x64-19.3.0.0.0dbru.zip
2) Folder for Oracle client
Create a folder where the Oracle client shall be saved permanently:
$ sudo mkdir -p /opt/oracle
$ cd /opt/oracle
3) Copy and unzip
Move and unzip the downloaded folders into the Oracle folder:
$ sudo cp ~/Downloads/instantclient-*.zip .
$ sudo unzip instantclient-basic-macos.x64-19.3.0.0.0dbru.zip
$ sudo unzip instantclient-sdk-macos.x64-19.3.0.0.0dbru.zip
$ sudo unzip instantclient-sqlplus-macos.x64-19.3.0.0.0dbru.zip
$ rm -rf *.zip
Now I have folder /opt/oracle/instantclient_19_3.
The version number may differ with your installation.
4) Create symbolic links
To be able to use the Oracle software that is now installed, you will have to create symlinks in order for the Mac to treat the files as executables:
$ sudo ln -s /opt/oracle/instantclient_19_3/sqlplus /usr/local/bin/
$ sudo ln -s /opt/oracle/instantclient_19_3/sdk/include/*.h /usr/local/include/
$ sudo ln -s /opt/oracle/instantclient_19_3/*.dylib /usr/local/lib/
$ sudo ln -s /opt/oracle/instantclient_19_3/*.dylib.12.1 /usr/local/lib/
$ sudo ln -s /opt/oracle/instantclient_19_3/*.dylib.19.1 /usr/local/lib/
5) [Re]install PEAR & PECL for PHP
$ curl -O https://pear.php.net/go-pear.phar
$ sudo php -d detect_unicode=0 go-pear.phar
You should now be at a prompt to configure PEAR.
- Type 1 and press return.
- Enter: /usr/local/pear
- Type 4 and press return.
- Enter: /usr/local/bin
- Press return a last time and the installer starts.
$ pear version
PEAR Version: 1.10.10
PHP Version: 7.1.33
Zend Engine Version: 3.1.0
Running on: Darwin IMAC2012.local 18.7.0 Darwin Kernel Version 18.7.0: Thu Jan 23 06:52:12 PST 2020; root:xnu-4903.278.25~1/RELEASE_X86_64 x86_64
6) Install Xcode
I you haven't already, open the Mac App Store via your Apple Menu and download and run Xcode:
7) Disable SIP
PECL looks for headers in /usr/include. But since El Capitan, /usr is protected by System Integrity Protection (SIP) .
This means that you cannot install the oci8 libraries for PHP, without disabling SIP.
Boot into Recovery Mode to do this:
- restart you Mac and hold cmd + R while it restarts.
- then select Terminal from the Utilities-menu
- and enter the following commands:
-bash-3.2# csrutil disable
Successfully disabled System Integrity Protection. Please restart the machine for the changes to take effect.
-bash-3.2# shutdown -r now
Let it boot normally and login again.
8) Install Xcode headers
When you are logged in again, open Terminal and install the headers, by using the Apple-provided installer:
$ sudo open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
9) Oracle ENV variables
Add the following environment variables to
/etc/profile
(
/etc/zprofile
on Catalina):$ sudo vi /etc/profile
export ORACLE_HOME=/opt/oracle/instantclient_19_3
export DYLD_LIBRARY_PATH=/opt/oracle/instantclient_19_3
export LD_LIBRARY_PATH=/opt/oracle/instantclient_19_3
where 19_3 should be replaced by the version you are installing.
Quit and reopen Terminal to continue.
10) Install oci8 via PECL
Now you can install oci8:
$ sudo pecl install oci8
Enter : instantclient,/usr/local/lib
When it succeeds, do as it says and add oci8 to PHP:
$ sudo vi /etc/php.ini
Add
extension=oci8.so
somewhere. My php.ini looks like this:
file_uploads = On
; http://php.net/post-max-size
post_max_size = 500M
; http://php.net/upload-max-filesize
upload_max_filesize = 40M
; UNIX: "/path1:/path2"
;***** Added by go-pear
include_path=".:/usr/local/pear/share/pear:/usr/local/share/php"
;*****
extension=oci8.so
11) Check PHP modules
$ php -m
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dba
dom
exif
fileinfo
filter
ftp
gd
hash
iconv
json
ldap
libxml
mbstring
mysqli
mysqlnd
oci8 <-- installed.
openssl
pcre
PDO
pdo_mysql
pdo_pgsql
pdo_sqlite
pgsql
Phar
posix
readline
Reflection
session
shmop
SimpleXML
snmp
soap
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tidy
tokenizer
wddx
xml
xmlreader
xmlrpc
xmlwriter
xsl
zip
zlib
When oci8 is not listed, and a warning like this is shown:
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/extensions/no-debug-non-zts-20160303/oci8.so' - dlopen(/usr/lib/php/extensions/no-debug-non-zts-20160303/oci8.so, 0x0009): dependent dylib '@rpath/libclntsh.dylib.19.1' not found for '/usr/lib/php/extensions/no-debug-non-zts-20160303/oci8.so' in Unknown on line 0
go back to step 4 and copy the fifth command, change the '12.1' into the version what the warning asks for.
I already did this for version '19.1'.
Show installed PHP modules again:
$ php -m
oci8 should now be listed.
12) Restart and check Apache
Restart Apache so your webserver can also use PHP with oci8:
$ sudo apachectl restart
Open a webbrowser and execute your PHP-info webpage.
I have made one for myself at
http://localhost/browserinfo.html
Look for oci8. You should see something like this:
13) Check PHP with OCI8
Now test what PHP says when you try to connect to an Oracle database.
I have set up a CentOS7 VM with Oracle 12c in Parallels Desktop for Mac 15 to which I can connect with Navicat to manage it.
Let's see if I can connect via PHP, too:
<?phpWhen I run this, I get:
ini_set('display_errors', 'on');
print('oci8 loaded: ' . extension_loaded('oci8') . "\n");
$ora = oci_connect('system', '1234Abcd', '//192.168.178.43/orcl.shared');
if (!$ora) {
$m = oci_error();
echo $m['message'], "\n";
exit;
}
else {
print "Connected to Oracle!\n";
}
// Close the Oracle connection
oci_close($ora);
?>
oci8 loaded: 1
Connected to Oracle!
14) Enable SIP again
When everything works for you, you can enable SIP again.
Boot back into recovery mode, open Terminal from the Utilities-menu and type:
-bash-3.2# csrutil enable
Successfully enabled System Integrity Protection. Please restart the machine for the changes to take effect.
-bash-3.2# shutdown -r now
Let it boot normally and login again.
15) Codesign errors with oci8.so
I don't know why, but PECL did/does not code sign the oci8.so.
Which means that, when you enabled SIP again, you may get code signing errors on oci8.so and will not be able to use it:
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/extensions/no-debug-non-zts-20160303/oci8.so' - dlopen(/usr/lib/php/extensions/no-debug-non-zts-20160303/oci8.so, 0x0009): code signature in (/usr/lib/php/extensions/no-debug-non-zts-20160303/oci8.so) not valid for use in process: mapped file has no cdhash, completely unsigned? Code has to be at least ad-hoc signed. in Unknown on line 0
I do not have an Apple Developer account, which means that I do not have an official code sign certificate.
So, I tried to ad-hoc sign the .so, but that did not help.
Then I tried with a self-signed code sign certificate , but that did not help either.
Which means, I will have to permanently disable SIP until this code sign issue is resolved.