4

I've looked around several other questions like this one, guide like this or this but I still had no luck.

Here's what i've done so far:

  1. Downloaded gmp with brew using brew install autoconf gmp
  2. Downloaded PHP (i'm using version 7.3.24) from source
  3. Copied Gmp directory to /Applications/MAMP/bin/php/php7.3.24/include/php/ext
  4. Entered that dir and launched phpize
  5. Launched ./configure --with-php-config=/Applications/MAMP/bin/php/php7.3.24/bin/php-config
  6. Launched make
  7. Launched make install

So far, so good. No errors, all seems fine but if i look at this point to my cli php -m (or php -i) there's no gmp extension loaded. So i went to my cli php.ini file and my web php.ini file and manually added extension=gmp.so (the file exist in this path /Applications/MAMP/bin/php/php7.3.24/include/php/ext/gmp/modules/gmp.so).

I even tried to specify the full path, but still no luck. Neither my cli or my phpinfo(); shows GMP enabled. I'm kinda confused atm and can't think about anything else.

What am I missing? Obviously, I restarted MAMP PRO like a dozen times, even my mac itself.

  • MacOs version: 11.4 Big Sur on iMac 24' M1
  • MAMP PRO Version: 6.3.1
  • PHP Version used: 7.3.24

Update 08/07/21

I updated MAMP PRO and it installed PHP version 7.3.27 so i went all over it again, download php from source https://github.com/php/php-src/releases?after=php-8.0.4RC1, copied ext/gmp into /Applications/MAMP/bin/php/php7.3.27/include/php/ext, launched /Applications/MAMP/bin/php/php7.3.27/bin/phpize, ./configure --with-php-config=/Applications/MAMP/bin/php/php7.3.27/bin/php-config, make and make install. No errors.

Output of make install is:

Installing shared extensions:     /Applications/MAMP/bin/php/php7.3.27/lib/php/extensions/no-debug-non-zts-20180731/
Installing header files:          /Applications/MAMP/bin/php/php7.3.27/include/php/
shtool:install:Warning: source and destination are the same - skipped

I added extension=gmp.so to php.ini via MAMP interface, file, edit template -> php -> php7.3.27. Restarted MAMP and nothing new on phpinfo();

Update 12/07/21

As per Hakre request i've run the following command into cli and this is the result:

[~]$ php -n -d extension=gmp.so -i
PHP Warning:  PHP Startup: Unable to load dynamic library 'gmp.so' (tried: /Applications/MAMP/bin/php/php7.3.27/lib/php/extensions/no-debug-non-zts-20180731/gmp.so (dlopen(/Applications/MAMP/bin/php/php7.3.27/lib/php/extensions/no-debug-non-zts-20180731/gmp.so, 9): no suitable image found.  Did find:
    /Applications/MAMP/bin/php/php7.3.27/lib/php/extensions/no-debug-non-zts-20180731/gmp.so: mach-o, but wrong architecture
    /Applications/MAMP/bin/php/php7.3.27/lib/php/extensions/no-debug-non-zts-20180731/gmp.so: mach-o, but wrong architecture), /Applications/MAMP/bin/php/php7.3.27/lib/php/extensions/no-debug-non-zts-20180731/gmp.so.so (dlopen(/Applications/MAMP/bin/php/php7.3.27/lib/php/extensions/no-debug-non-zts-20180731/gmp.so.so, 9): image not found)) in Unknown on line 0
phpinfo()
PHP Version => 7.3.27

which is quite interesting if you look at this img The extension actually exists

For Raptor:

This is the output of the commands you've asked.

[~]$ which php                    
/Applications/MAMP/bin/php/php7.3.27/bin/php
[~]$ php --ini | grep "Loaded Configuration File"
Loaded Configuration File:         /Applications/MAMP/bin/php/php7.3.27/conf/php.ini
[~]$ php --version
PHP 7.3.27 (cli) (built: Mar 16 2021 12:04:51) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.27, Copyright (c) 1998-2018 Zend Technologies
[~]$ 

Ofc there are multiple php's installed since MAMP alone fill it with like 4-5 version + there's the default one from MacOSx which i've overwrote with .zshrc bash profile

export PATH=/Applications/MAMP/bin/php/php7.3.27/bin:$PATH

That line is from my .zshrc

Could the issue be related to the new M1 chip? I'm using a new iMac bought just at the end of June2021. Maybe is this related?

For Haridarshan:

Let me start saying that i tried to use .configure with no additional params, then i tried with CC="gcc -arch i386" CXX="g++ -arch i386" and even with CC="gcc -arch arm64" CXX="g++ -arch arm64" (i found arm64 in configure.log). None of them produced a valid .so, if i didn't miss any other info from the command line. About the test u've asked me to make, this is the result:

[~]$ file /Applications/MAMP/bin/php/php7.3.27/bin/php
/Applications/MAMP/bin/php/php7.3.27/bin/php: Mach-O 64-bit executable x86_64
Diego
  • 1,610
  • 1
  • 14
  • 26
  • 2
    Have you tried to use `php` cli and only load the extension (blank, `-n`)? `php -n -d extension=gmp.so -i`? If there is a problem loading the .so file, it will be shown on standard error. Also it might help to come to a command verifying the exact file from disk is loaded). – hakre Jul 12 '21 at 07:20
  • What is that command for exactly? because actually i tried to edit my cli php.ini file aswell but had no luck with php -i to show the loaded extension. Also i never see an error about the issue with the .so file. Where should i look for it? – Diego Jul 12 '21 at 07:22
  • 1
    That command is php and the `-n` switch tells php to not load any .ini files - that is a blank slate for bare-level trouble-shooting. Then it's `-d` for a configuration parameter which is next `extension=gmp.so` that is telling php to load the extension you have problems with. Then `-i` to show the configuration, you already know that switch. – hakre Jul 12 '21 at 07:24
  • _"Where should i look for it?"_ it will display errors on standard error, that is the common channel for diagnostic messages in your shell. E.g. it will be shown in your terminal where you execute the command. – hakre Jul 12 '21 at 07:25
  • Ok thanks. I'll test it asap, i have an update running and cant stop. – Diego Jul 12 '21 at 07:50
  • what is the output of `php --ini | grep "Loaded Configuration File"`, `which php`, `php -m | grep gmp`, and `php --version`? I suspect there are more than 1 PHP installed. – Raptor Jul 12 '21 at 08:01
  • Last, what did MAMP log tell? Might have something like *Unable to load dynamic library* – Raptor Jul 12 '21 at 08:07
  • 1
    Not sure if you must use MAMP PRO, but Homebrew has PHP too. If you're using Homebrew PHP version, you can use PECL to install the extension easily. – Raptor Jul 12 '21 at 08:14
  • I've updated my question with the commands output. I know brew has PHP to offer but i develop daily using MAMP, that's why i want to make it work for MAMP expecially – Diego Jul 12 '21 at 08:17
  • 1
    Now that i look better at the output it says "/Applications/MAMP/bin/php/php7.3.27/lib/php/extensions/no-debug-non-zts-20180731/gmp.so: mach-o, but wrong architecture" so there must be something wrong with that file. I have a new iMac with M1 chip. Might that be the reason? – Diego Jul 12 '21 at 08:27
  • @Diego: Yes, it has the Arm architecture. It might be that the build tools you use have the sources for a different architecture (X86_64 for example) _or_ needs a hint to compile for Arm architecture. Unfortunately, this is only a comment and guess. But I think this error message is pointing into the right direction. And finally it is an error message you can reproduce. If it would be me, I would next search for this error message. The good news probably is: I think the underlying problem has been solved already (compile PHP GMP Extension for Arm/ Apple M1). Brew project perhaps has more. – hakre Jul 12 '21 at 09:34
  • @Diego you can check the OS architecture against which the library is build using `file` command. `file ` e.g. _file /opt/remi/php80/root/usr/bin/php_ but before that check for php and whatever architecture it is build for, you'll have build other libraries with same architecture which you can do by passing `CFLAGS="-arch i386"` while executing `./configure` command. You can refer my comments on this [question](https://stackoverflow.com/q/68020269/2324206) – Haridarshan Jul 12 '21 at 10:39
  • One more thing, when building/compiling PHP you can enable support for GMP by using `--with-gmp` – Haridarshan Jul 12 '21 at 10:48
  • @Haridarshan if i understood that comment correctly isn't that something i shouldn't do? The php i'm using is the one shipped with MAMP PRO – Diego Jul 12 '21 at 10:58
  • 2
    Doesn't matter if you're using php shipped with MAMP Pro or compiling php from source, all you'll have to make sure is every library which php is going to use must have same architecture and when you're compiling libraries from source it might possible the architecture differs and when that happens we need to pass compiler flags to force compiler to build library against specific arch – Haridarshan Jul 12 '21 at 11:03
  • If it helps, the brew PHP and GMP formulas https://formulae.brew.sh/formula/php / https://formulae.brew.sh/formula/gmp have sources linked how they are build and their builds include Apple Silicon Big Sur (which I guess is yours and next to other architectures). – hakre Jul 13 '21 at 07:22
  • @Haridarshan and hakre thanks for the tips but i can't follow you. The compiling thing is out of my knowledge. I really don't know how to proceed with the tests. I don't know how i could get advantage from your suggestions while keeping MAMP clean and safe. All my projects run with that MAMP PHP – Diego Jul 13 '21 at 13:02
  • @Diego lets go step by step. First, in question you mentioned you build PHP (8 & 7.4) from source so, are you using shipped one or the one which you've compiled from source, either way check the architecture of the php using `file` command and share the output – Haridarshan Jul 13 '21 at 13:48
  • Maybe i'm not using the correct terms to explain this but i didn't build PHP itself. I just downloaded the php package from source to extract the GMP extension directory. I only build that one – Diego Jul 13 '21 at 13:52
  • I got it and that's okay, using `file` command we will find the architecture of PHP and for that same architecture we will compile **GMP** extension. – Haridarshan Jul 14 '21 at 10:36
  • I edited my question when u've asked me about the file command. I launched it on the php executable. Is that what you were looking for? – Diego Jul 14 '21 at 10:49
  • 1
    @Diego as php is showing x86_64 bit arch, try compiling gmp with x86_64 arch and for that you just need to replace i386 with `x86_64`. Once **.so** is compiled check its arch via file command – Haridarshan Jul 14 '21 at 10:52
  • 1
    @Haridarshan finally after your suggestion phpinfo() says gmp is enabled!!!! in the while i've struggled with another error but i solved that too, i think we should complete these questions with as much detail as we can since this story got so dark and weird. Maybe we can help somebody else. See the other problem here: https://stackoverflow.com/questions/68379240/gnu-mp-library-version-4-2-or-greater-required – Diego Jul 14 '21 at 13:57

1 Answers1

2

There can be many reasons why a PHP extension is not loaded, but it is not always easy to directly point to reasons (and fixes) as the distance between compiling from sources to showing phpinfo() and then finally missing the extension is large.


Get the real error message first

One way to reduce the distance in trouble-shooting is to see if the extension can be loaded by PHP and if not, showing an error message.

A common test for that is to use the CLI SAPI (PHP on the commandlineDocs) as it allows to reduce and easier control the PHP runtime environment while being compatible with the extension.

To start PHP with the default configuration (no .ini files), loading only the single extension binary to test and showing the configuration information, run:

$ php -n -d extension=gmp.so -i

Excerpt from OptionsDocs, also there is php --help:

-n               No php.ini file will be used
-d foo[=bar]     Define INI entry foo with value 'bar'
-i               PHP information

This should provoke an error (shown in the terminal on standard error) or show the extension loaded in the PHP information output (on standard output).

Alternatively, to reduce the output and only check for the error, execute an empty PHP statement with the -r command-line switch:

-r <code>        Run PHP <code> without using script tags <?..?>

The example with the GMP extension in question:

$ php -n -d extension=gmp.so -r ';'

This will exit non-zero (exit status) if there is a problem loading the extension displaying and error message on standard error and would exit with zero status in case the extension could be loaded:

$ php -n -d extension=gmp.so -r ';'
PHP Warning:  PHP Startup: Unable to load dynamic library 'gmp.so' (tried: /Applications/MAMP/bin/php/php7.3.27/lib/php/extensions/no-debug-non-zts-20180731/gmp.so (dlopen(/Applications/MAMP/bin/php/php7.3.27/lib/php/extensions/no-debug-non-zts-20180731/gmp.so, 9): no suitable image found.  Did find:
    /Applications/MAMP/bin/php/php7.3.27/lib/php/extensions/no-debug-non-zts-20180731/gmp.so: mach-o, but wrong architecture
    /Applications/MAMP/bin/php/php7.3.27/lib/php/extensions/no-debug-non-zts-20180731/gmp.so: mach-o, but wrong architecture), /Applications/MAMP/bin/php/php7.3.27/lib/php/extensions/no-debug-non-zts-20180731/gmp.so.so (dlopen(/Applications/MAMP/bin/php/php7.3.27/lib/php/extensions/no-debug-non-zts-20180731/gmp.so.so, 9): image not found)) in Unknown on line 0
$ echo $?
254

As the example shows, the error is already in "PHP Startup" which is the typical phase where PHP signals diagnosis messages loading extensions.


Dlopen 9: no suitable image found: mach-o, but wrong architecture

The error message above shows that a) PHP is first of all unable to load the extension (as a dynamic library, .so file, a shared object file, that is the compiled extension) and b) that it failed to load as no suitable image was found:

PHP Warning: PHP Startup: Unable to load dynamic library 'gmp.so' (tried: <path> (dlopen(<path>, 9): no suitable image found. Did find: <path>: mach-o, but wrong architecture ...), <path> (dlopen(<path>.so, 9): image not found)) in Unknown on line 0

That means the file is available on disk (can be opened) but the image is not suitable, which means it does not match the architecture.


(there is some noise in the try for gmp.so.so that is done by php so one can pass -d extension=gmp without the extension to work directly, e.g. in a php-.ini to work on both *nix (.so) or windows (.dll). This part can be ignored, that is "image not found" as the file does not exists which is expected)


It must be the same architecture as PHP itself as PHP is already running and wants to load the binary extension - they need to fit.

To get the architecture of PHP, locate the PHP command:

$ which php
/Applications/MAMP/bin/php/php7.3.27/bin/php

This is the absolute path to the php binary. With it, it is now possible with the file(1) utility to obtain more information about it:

$ file /Applications/MAMP/bin/php/php7.3.27/bin/php
/Applications/MAMP/bin/php/php7.3.27/bin/php: Mach-O 64-bit executable x86_64

(or call $ file "$(which php)" for running both at once)

It shows the php binaries info incl. the x86_64 architecture at the end:

Mach-O 64-bit executable x86_64

As the shared object image to load (the compiled php extension gmp.so file) also needs to match it, the same file(1) utility can be used on the compiled extensions .so file in the same manner.

The comparison then should show the difference.

With this information at hand finally the extension can be compiled with the appropriate architecture.


Closing notes:

On Apple Silicon M1 I'm not specifically profound of compiling and its architectures and others can tell it better. From what I've seen you manged it by running brew with the arch(1) utility setting the architecture by arch -x86_64 <command> to x86_64. On Apple Silicon this may require more tooling, namely Rosetta.

It seems to be something common M1 users blog about (via Austen Cameron in Nov 2020) but this is entirely not my system.

From my own understanding it should be possible to set the architecture with compiler flags or on the configure line and thats normally it.

As brew has the information how to compile an extension on a system (the brew formula, here for gmp) it is perhaps most straight forward to go with it and run the install under the correct architecture.

With the caveat that you need to start brew in (?) the correct architecture with the arch(1) utility as well (and the brew install).

hakre
  • 193,403
  • 52
  • 435
  • 836
  • 1
    I reached the solution, please edit your answer and add all the details, you can even find part of the story (for a related issue) here: https://stackoverflow.com/questions/68379240/gnu-mp-library-version-4-2-or-greater-required/68379616#68379616. As soon as you update the answer i'll accept it and give the reward – Diego Jul 14 '21 at 14:06
  • 1
    @Diego: I could track the trouble-shooting ongoing with the `file(1)` utility step to reveal the architecture of php and therefore to learn about the different architecture of the compiled .so file. After that I assume you did the install via brew setting the correct architecture. The concrete commands for that I've excluded in the answer here so far as I can imagine there was a lot of going back and forth for you, but the concrete point here was the architecture mismatch right? The other problem is build dependencies ;) – hakre Jul 14 '21 at 16:34
  • 1
    yeah i think you got the point. Basically it was all arch-related issue but it wasn't so obvious to begin with. Maybe the M1 chip with brew is still in develop and things get weird fast. Anyways, seems a good answer. I'll give the reward and we can close this case – Diego Jul 15 '21 at 07:25
  • 1
    @Diego: the moment you wrote it is Apple M1 I was also directly side-tracked by it a bit. Maybe that's another learning: Using X86_64 on M1 can be fine, it must not be ARM (only can be). – hakre Jul 15 '21 at 07:29