Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Header File missing while installing in Mac Catalina 10.15.6 #1159

Closed
arijitnaskar opened this issue Aug 17, 2020 · 9 comments
Closed

Header File missing while installing in Mac Catalina 10.15.6 #1159

arijitnaskar opened this issue Aug 17, 2020 · 9 comments

Comments

@arijitnaskar
Copy link

Bug Report

running: phpize
grep: /usr/include/php/main/php.h: No such file or directory
grep: /usr/include/php/Zend/zend_modules.h: No such file or directory
grep: /usr/include/php/Zend/zend_extensions.h: No such file or directory

Environment

MacOS Catalina v-10.15.6
PHP Version 7.3.11

Test Script

sudo pecl install mongodb

Expected and Actual Behavior

Driver not installed.
error:

/private/tmp/pear/install/mongodb/php_phongo.c:22:10: fatal error: 'php.h' file not found
#include <php.h>

Debug Log

Probable Root Cause:
Apple has deprecated having a /usr/include distinct from the SDK

@arijitnaskar arijitnaskar changed the title Header File missing while installing Mac Catalina 10.15.6 Header File missing while installing in Mac Catalina 10.15.6 Aug 17, 2020
@jmikola
Copy link
Member

jmikola commented Aug 17, 2020

This appears to be a common problem (beyond any one extension) due to filesystem changes in MacOS Catalina. The following pages may provide some solutions (and both link off to duplicate issues reported against other PHP extensions):

Alternatively, you can opt to install a newer version of PHP with Homebrew and then install the extension using the pecl command that ships with that PHP version.

@arijitnaskar
Copy link
Author

This appears to be a common problem (beyond any one extension) due to filesystem changes in MacOS Catalina. The following pages may provide some solutions (and both link off to duplicate issues reported against other PHP extensions):

Alternatively, you can opt to install a newer version of PHP with Homebrew and then install the extension using the pecl command that ships with that PHP version.

I have already seen these posts.
May be I sound stupid, Just wondering, why should I make changes in OS files? The extension installer files should handle this.
Also My default PHP is enabled. I don't want to change it with Homebrew.

@jmikola
Copy link
Member

jmikola commented Aug 17, 2020

Just wondering, why should I make changes in OS files? The extension installer files should handle this.

If you feel this is a problem with the extension installer, the appropriate place to report that issue would be through bugs.php.net and list "PECL" as the "affected package". I'm not sure that going that route will reach a positive outcome, though.

Individual PHP extensions (e.g. this driver, Xdebug, and other PECL packages) are not in a position to address this issue as there is no "installer" within the package to speak of (apart from a Makefile). The root cause of this issue is Apple removing a standard include path for its bundled version of PHP's headers. The apparent solution to that requires modifying the phpize script used by pecl with the new path, as is done in the two links I shared above. If you do attempt to use this solution, the docs for Building the MongoDB PHP Driver from source may be helpful.

Reading a bit further, the corresponding blog post from the Stack Overflow author suggests that this approach might still pose a problem. They refer to Xdebug in the blog post, but this point likely applies to any extension compiled as an .so file:

Note that building xdebug from source code and actually trying to use that version of xdebug.so (for example by referencing the built file in xdebug/module/xdebug.so after using make install) with the build-in PHP should end up in a "code signature" error. As described here and here, even after signing the binary, MacOS won't allow system binaries to interact with non-system binaries for security reasons. The only real solution to use a custom version of xdebug would be to compile and use you own instance of PHP instead of the build in one.

Their conclusion points to using Homebrew (or an equivalent approach) to get a separate version of PHP installed.

Also My default PHP is enabled. I don't want to change it with Homebrew.

According to this r/php thread, Apple has actually started deprecating its bundled interpreters (e.g. PHP, Python, Ruby) and has plans to remove them in future OS versions. I expect the reasons for doing so may overlap with other changes Apple has made re: include paths and restricting the loading of non-system binaries (as a security measure). I do not use MacOS, but most developers I've spoken with do advise using Homebrew to provision a reliable and up-to-date PHP environment on MacOS.

I'm afraid I can't provide any more assistance here beyond directing you to the external resources above.

@arijitnaskar
Copy link
Author

Just wondering, why should I make changes in OS files? The extension installer files should handle this.

If you feel this is a problem with the extension installer, the appropriate place to report that issue would be through bugs.php.net and list "PECL" as the "affected package". I'm not sure that going that route will reach a positive outcome, though.

Individual PHP extensions (e.g. this driver, Xdebug, and other PECL packages) are not in a position to address this issue as there is no "installer" within the package to speak of (apart from a Makefile). The root cause of this issue is Apple removing a standard include path for its bundled version of PHP's headers. The apparent solution to that requires modifying the phpize script used by pecl with the new path, as is done in the two links I shared above. If you do attempt to use this solution, the docs for Building the MongoDB PHP Driver from source may be helpful.

Reading a bit further, the corresponding blog post from the Stack Overflow author suggests that this approach might still pose a problem. They refer to Xdebug in the blog post, but this point likely applies to any extension compiled as an .so file:

Note that building xdebug from source code and actually trying to use that version of xdebug.so (for example by referencing the built file in xdebug/module/xdebug.so after using make install) with the build-in PHP should end up in a "code signature" error. As described here and here, even after signing the binary, MacOS won't allow system binaries to interact with non-system binaries for security reasons. The only real solution to use a custom version of xdebug would be to compile and use you own instance of PHP instead of the build in one.

Their conclusion points to using Homebrew (or an equivalent approach) to get a separate version of PHP installed.

Also My default PHP is enabled. I don't want to change it with Homebrew.

According to this r/php thread, Apple has actually started deprecating its bundled interpreters (e.g. PHP, Python, Ruby) and has plans to remove them in future OS versions. I expect the reasons for doing so may overlap with other changes Apple has made re: include paths and restricting the loading of non-system binaries (as a security measure). I do not use MacOS, but most developers I've spoken with do advise using Homebrew to provision a reliable and up-to-date PHP environment on MacOS.

I'm afraid I can't provide any more assistance here beyond directing you to the external resources above.

Thanks a lot for the detail explanation. It helped me a lot to understand the problem.
As you mentioned at the last part, I have decided to use Homebrew PHP. But if I do so now, there will be multiple versions of PHP active in my system. I searched google to disable the default php of mac, before I install Homebrew. But could not find a good post. Can you please guide me with some resources on how to disable the default PHP.
I am sorry for asking all these basic questions. I am windows user my whole life. Just switched to Mac before 1 month.

@alcaeus
Copy link
Member

alcaeus commented Aug 18, 2020

While there are multiple versions of php on the system, the important part is which one is being loaded through the path. Running which php will show which one is running. If it show /usr/bin/php, it is using the system one, while /usr/local/bin/php points to the brew installed one. The important part is that you prepend the homebrew binary folder (/usr/local/bin) into the path. I'm not sure what brew does when installing (as it's been a while since I've installed it), but for example, my .bash_profile file has the following line:

export PATH="$HOME/bin:/usr/local/bin:/usr/local/sbin:$PATH"

This makes sure that any binaries installed using homebrew take precedence over the system-provided ones.

@jmikola
Copy link
Member

jmikola commented Aug 18, 2020

To add on to what @alcaeus mentioned above, you'll also want to make sure the correct version of pecl (if installing with PECL) or phpize (if compiling from source) is used. Similarly, each PHP installation will have their own php.ini files (for each of their web and CLI environments). phpinfo() (and php -i) reports INI files/paths and extension directories, so you can consult that output if you run into any trouble adding extension-mongodb.so to your INI files. PECL does attempt to modify the INI on its own, but that's not always possible so manually modifying your INI is a common last step.

@arijitnaskar
Copy link
Author

While there are multiple versions of php on the system, the important part is which one is being loaded through the path. Running which php will show which one is running. If it show /usr/bin/php, it is using the system one, while /usr/local/bin/php points to the brew installed one. The important part is that you prepend the homebrew binary folder (/usr/local/bin) into the path. I'm not sure what brew does when installing (as it's been a while since I've installed it), but for example, my .bash_profile file has the following line:

export PATH="$HOME/bin:/usr/local/bin:/usr/local/sbin:$PATH"

This makes sure that any binaries installed using homebrew take precedence over the system-provided ones.

I have installed PHP with Homebrew. Then I have updated my apache httpd
I have updated my .bash_profile and I ran source ~/.bash_profile to update the path
Now in My browser index.php with phpinfo() showing php.ini path '/usr/local/etc/php/7.4' - which is my homebrew installed php
But when I run which php in terminal, its showing '/usr/bin/php' - which is the default mac php
And the ultimate problem is I followed the blog suggested by @jmikola . Everything went perfect as described in the blog post, but the mongo driver is still not working.
First I changed the homebrew php.ini. It didn't work, then I changed default php.ini, but still no luck.

@jmikola
Copy link
Member

jmikola commented Aug 19, 2020

Double-check that the pecl and/or phpize commands you have run correspond to your Homebrew installation. If compilation succeeds, I expect there is a mongodb.so file for Homebrew's PHP 7.4 environment somewhere on your system and it's a matter of referencing it from the correct INI file(s). Several of the user-submitted comments on https://www.php.net/manual/en/mongodb.installation.homebrew.php walk through the full process of using pecl to install the extension and then updating the INI files. It may entail creating a new mongodb.ini file in the PHP environment's INI directory (an alternative to modifying php.ini directly).

But when I run which php in terminal, its showing '/usr/bin/php' - which is the default mac php

I assume you followed the instructions on https://formulae.brew.sh/formula/php (between the package dependencies and analytics) for configuring Apache with PHP. That page mentions a command to display the path to the php.ini files ($(brew --prefix)/etc/php/7.4/). I'm not familiar with the command, but I expect brew --prefix can be used as a starting point to find where the php binary is installed. You could also attempt searching for a php binary $(brew --prefix) path.

https://stackoverflow.com/a/60297362/162228 may provide additional guidance.

First I changed the homebrew php.ini. It didn't work, then I changed default php.ini, but still no luck.

If you're using Homebrew, there should be absolutely no reason to interact with configuration files for the MacOS-bundled version of PHP. The two environments are entirely separate. I realize you were likely trying to explore all possible options, but it seemed important to point that out.

@arijitnaskar
Copy link
Author

arijitnaskar commented Oct 15, 2020

Thanks @jmikola @alcaeus for initial guidance.
Finally I have resolved the issue. Posting the solution as this may help others.

My OS version: Catalina 10.15.7

  1. Install Homebrew PHP from here . Follow all the steps and modify httpd.conf and restart Apache.
  2. add phpinfo() in your index.php and open localhost. It should show your homebrew php version. If not then step 1 is not done successfully. Do some more research and complete step 1. If phpinfo() is showing correct homebrew php version, move to the next step.
  3. Now run which php if the result is same as your homebrew PHP path ( the will be somthing like this- /usr/local/... ) you are good to move on.
    If which php is showing some other path ( like /usr/bin/php ) then update path variable. Follow this post. Check with 'which php' after setup the path.
  4. Now follow this steps to install mongodb php driver.
  5. Now <?php $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017"); ?> should connect mongodb with your application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants