Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syahzul/473ef51b61d82f1f5e294bf5cb804fed to your computer and use it in GitHub Desktop.
Save syahzul/473ef51b61d82f1f5e294bf5cb804fed to your computer and use it in GitHub Desktop.
How to install PHP OCI8 for macOS Ventura/Sonoma on Apple Silicon Macs

How to install PHP OCI8 for macOS Ventura/Sonoma on Apple Silicon Macs

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.

Install Rosetta

In Terminal, there is no automatic detection for missing Rosetta to run older architecture command line tools.

If you haven't been prompted for any other app already, manually install Rosetta 2:

softwareupdate --install-rosetta

Shortcut to switch 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'" >> ~/.zshrc

Close and re-open the Terminal and run the following command:

arm

arch

Make sure our current architecture is arm64.

Install Command Line Tools for Xcode

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

xcode-select --install

Uninstall Homebrew for Apple Silicon

If 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)"

Switch to i386 architecture

Run the following command to switch to i386:

intel

arch

Make sure our current architecture is i386.

Install Homebrew for Intel

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 brew

If 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.

Oracle Instant Client for macOS (Intel x86)

Download the following files from Oracle Instant Client Downloads for macOS (Intel x86)

Create a folder for the files.

sudo mkdir -p /opt/oracle/instantclient

Now, extract the .dmg files and move the contents to /opt/oracle/instantclient.

Install PHP

Wen can proceed to install the latest version of PHP (which is 8.3 at the moment) and the dependencies. It may take a while to complete.

brew install php

Verify the PHP version installed on our machine.

php -v

PHP 8.3.3 (cli) (built: Feb 13 2024 15:41:14) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.3, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.3, Copyright (c), by Zend Technologies

Install PHP OCI8

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

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

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.

Allows Instant Client files run on macOS

By default, macOS will block the Instant Client files since the developer cannot be verified. We have to allow it one by one.

Open your System Settings > Privacy & Security > Security, and keep it open, don't close it.

Run the command below to trigger the warning.

php -m

macOS will pop up the warning dialog, 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 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!';
}

References

  1. How to install OCI8 on macOS (Monterey) M1 Processor with PHP 8.1
  2. M1 Mac — How to switch the Terminal between x86_64 and arm64
@aurawindsurfing
Copy link

Thanks a lot for this tutorial. Much much appreciated!

@rohitsuthar
Copy link

Thank you so much for this great tutorial. 🙌🙌🙌

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