Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • 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 - outdated

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

Caution

Finally Oracle release Instant Client for arm64 architecture. Refer new tutorial How to install PHP OCI8 for macOS Ventura/Sonoma on Apple Silicon Macs (arm64 version) here.

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 PHP OCI8 for macOS Ventura/Sonoma on Apple Silicon Macs (arm64 version)
  2. How to install OCI8 on macOS (Monterey) M1 Processor with PHP 8.1
  3. M1 Mac — How to switch the Terminal between x86_64 and arm64
@mtziortzis
Copy link

Just a heads up, there is now an MacOS ARM version available for instant client
https://www.oracle.com/database/technologies/instant-client/downloads.html
FINALLY!

I was using the above but today i switched to the native arm one. Everything's fine!

@syahzul
Copy link
Author

syahzul commented Jun 25, 2024

Just a heads up, there is now an MacOS ARM version available for instant client https://www.oracle.com/database/technologies/instant-client/downloads.html FINALLY!

I was using the above but today i switched to the native arm one. Everything's fine!

Finally! Thanks for the info.

@syahzul
Copy link
Author

syahzul commented Jun 25, 2024

I published new tutorial on how to install oci8 using instant client for arm64 here https://gist.github.com/syahzul/d61d8ccea7c5959a84ed52d14159d8a9.

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