Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save syahzul/adfac83e1d930f400855b5425564dfda to your computer and use it in GitHub Desktop.
Save syahzul/adfac83e1d930f400855b5425564dfda to your computer and use it in GitHub Desktop.
How to install OCI8 on macOS (Monterey) M1 Processor with PHP 8.1

How to install OCI8 on macOS 12.5 (Monterey) M1 Processor

Note
If your using macOS Ventura or Sonoma, please refer to How to install PHP OCI8 for macOS Ventura/Sonoma on Apple Silicon Macs

Please note that all installations below must use Terminal with Rosetta:

  1. Homebrew Intel Mac Version
  2. PHP 8.1 or 8.0
  3. OCI8 extension

And use Terminal without Rosetta for:

  1. Installing Command Line Tools for Xcode
  2. Uninstalling Homebrew M1 version

Rosetta Terminal

Duplicate Terminal that located under /Applications/Utilities and renames it to Terminal-Rosetta.

Right click on Terminal-Rosetta > Get Info > Open using Rosetta.

Run it once and macOS will prompt to install Rosetta on your machine. Once completed, re-open your Terminal-Rosetta.

Command Line Tools for Xcode

Run the command below if you haven't installed it on your machine.

xcode-select --install

Homebrew

Uninstall M1 version

If you have installed the Homebrew for M1 processor previously, remove it. To uninstall, open Terminal (running without Rosetta) and use the following command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"

Install Intel Mac version

Install the Homebrew using Terminal-Rosetta.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Oracle Instant Client

Create a folder to store Oracle Instant Client files.

sudo mkdir /opt/oracle

Download the following files from Oracle website

Unzip and move it to the /opt/oracle/instantclient_19_8.

Create symlinks

sudo mkdir -p /usr/local/{include,lib}

sudo ln -s /opt/oracle/instantclient_19_8/sdk/include/*.h /usr/local/include/
sudo ln -s /opt/oracle/instantclient_19_8/*.dylib /usr/local/lib/
sudo ln -s /opt/oracle/instantclient_19_8/*.dylib.19.1 /usr/local/lib/
sudo ln -s /usr/local/lib/libclntsh.dylib.19.1 /usr/local/lib/libclntsh.dylib

PHP 8.1

Install PHP and the dependencies. It may take a while to complete.

brew install php

Verify the PHP version installed on your machine.

php -v

PHP 8.1.10 (cli) (built: Sep  3 2022 12:09:27) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.10, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.10, Copyright (c), by Zend Technologies

Install the latest version of oci8 (v3.2.1) with PECL.

echo instantclient,/opt/oracle/instantclient_19_8 | pecl install oci8

PHP 8.0

Install PHP and the dependencies. It may take a while to complete.

brew install [email protected]

Verify the PHP version installed on your machine.

php -v

PHP 8.0.23 (cli) (built: Sep  4 2022 09:20:50) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.23, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.23, Copyright (c), by Zend Technologies

Install oci8 version 3.0.1 with PECL.

echo instantclient,/opt/oracle/instantclient_19_8 | pecl install oci8-3.0.1

PHP 7.4

Install PHP and the dependencies. It may take a while to complete.

brew install [email protected]

Verify the PHP version installed on your machine.

php -v

PHP 7.4.30 (cli) (built: Sep  4 2022 09:01:37) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.30, Copyright (c), by Zend Technologies

Install oci8 version 2.2.0 with PECL.

echo instantclient,/opt/oracle/instantclient_19_8 | pecl install oci8-2.2.0

Note:

  1. If you ever got an error command not found: pecl, make sure PHP bin folder is included in your environment path.
  2. Use the appropriate version of oci8 for your PHP. Refer https://pecl.php.net/package/oci8 for more info.

Instant Client files Security & Privacy

By default, macOS will block the files since the developer cannot be verified. Open your System Preferences > Security & Privacy > General, and keep it open, then run the command below:

php -m

macOS will pop up the warning, just click Open (if available) or Cancel, and click Allow Anyway on your Security & Privacy window.

Re-run the command above until no more warning and make sure oci8 is on the list.

php -m | grep oci8

Test it out

Create a file containing the following codes.

<?php

if (function_exists('oci_connect')) {
    echo 'OCI8 is working!';
}
else {
    echo 'Whoopss...not working!';
}

Known Issues

  1. Not working for macOS Ventura and Sonoma. Refer here

References

@nasrulhazim
Copy link

nice. will try it out later on.

@shafiqruslan
Copy link

In OS Ventura, duplicating Terminal.App and renaming it for Rosetta2 option is disabled feature.

Can follow the link i posted below on how to switching terminal defaults architecture on M1 Mac.

https://vineethbharadwaj.medium.com/m1-mac-switching-terminal-between-x86-64-and-arm64-e45f324184d9

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