Note
If your using macOS Monterey or below, please refer to How to install OCI8 on macOS (Monterey) M1 Processor with PHP 8.1
macOS Ventura and above, duplicating and renaming Terminal.app are disabled by Apple. In order to install the OCI8, we need to switch between arm64 and x86_64 architecture.
Open the Terminal and run the following codes to add aliases to switch between architecture.
echo "alias intel='env /usr/bin/arch -x86_64 /bin/zsh --login'" >> ~/.zshrc
echo "alias arm='env /usr/bin/arch -arm64 /bin/zsh --login'" >> ~/.zshrcClose and re-open the Terminal and run the following command:
arm
archMake sure our current architecture is arm64.
Run the command below if you haven't installed it on your machine.
xcode-select --installIf you have installed the Homebrew for Apple Silicon previously, remove it. To uninstall, open the Terminal and use the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"Run the following command to switch to i386:
intel
archMake sure our current architecture is i386.
Run the following command to start installing Homebrew.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"After installation completed, verify that we have installed Intel version, not Apple Silicon by using the command below:
which brewIf the output is /usr/local/bin/brew, then it's for Intel.
But if the output is /opt/homebrew/bin/brew, it's for Apple Silicon. You have to start all over again by uninstalling it using the steps above.
Download the following files from Oracle website
Unzip and move it to the /opt/oracle/instantclient_19_8.
Wen can proceed to install the latest version of PHP (which is 8.2 at the moment) and the dependencies. It may take a while to complete.
brew install phpVerify the PHP version installed on our machine.
php -v
PHP 8.2.10 (cli) (built: Aug 29 2023 15:31:38) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.10, Copyright (c) Zend Technologies
with Zend OPcache v8.2.10, Copyright (c), by Zend TechnologiesInstall the latest version of oci8 (v3.2.1) with PECL.
echo instantclient,/opt/oracle/instantclient_19_8 | pecl install oci8Note:
- If you ever got an error
command not found: pecl, make sure PHP bin folder is included in your environment path. - Use the appropriate version of oci8 for your PHP. Refer https://pecl.php.net/package/oci8 for more info.
By default, macOS will block the Instant Client files since the developer cannot be verified. Open your System Preferences > Privacy & Security > Security, and keep it open, then run the command below:
php -mmacOS will pop up the warning, click on Open (if available) or Cancel, and click Allow Anyway on your Privacy & Security window. You might have to do it a few times until no more warning dialog appears.
Run the command to make sure oci8 is enabled.
php -m | grep oci8Create a file containing the following codes.
<?php
if (function_exists('oci_connect')) {
echo 'OCI8 is working!';
} else {
echo 'Whoopss...not working!';
}References:
Thanks a lot for this tutorial. Much much appreciated!