Space Cat, Prince Among Thieves

Install PHP's Built In Extensions in macOS

Should you run into errors related to missing php.h or other .h files, you should check out my post on fixing missing headers on macOS Mojave.


These directions are for working with the native installation of PHP. Your results may vary if you are using a brew, MAMP or otherwise installed version of PHP - I do not recommend this for those cases.

Important: As you are altering the built in version of PHP, you will need to ensure you have disabled System Integrity Protection before you begin. You can find instructions on how to do this here: How to: Disable macOS System Integrity Protection.

This was previously dedicated to installing the Mcrypt extension specifically but in reality can be used to install any of the following extensions distributed with the PHP source.

bcmath       ftp          mcrypt       pdo_oci      simplexml    wddx
bz2          gd           mysqli       pdo_odbc     skeleton     xml
calendar     gettext      mysqlnd      pdo_pgsql    snmp         xmlreader
com_dotnet   gmp          oci8         pdo_sqlite   soap         xmlrpc
ctype        hash         odbc         pgsql        sockets      xmlwriter
curl         iconv        opcache      phar         spl          xsl
date         imap         openssl      posix        sqlite3      zip
dba          interbase    pcntl        pspell       standard     zlib
dom          intl         pcre         readline     sysvmsg
enchant      json         pdo          recode       sysvsem
exif         ldap         pdo_dblib    reflection   sysvshm
fileinfo     libxml       pdo_firebird session      tidy
filter       mbstring     pdo_mysql    shmop        tokenizer

We need install the required dependencies. If you are not already using Homebrew you will need it.

$ brew install autoconf pkg-config

For certain extensions like mcrypt you may additionally need to install additional libraries such as:

$ brew install libmcrypt

Next we will download the PHP source. Verify the exact version of PHP you are running. This can be retrieved as follows. The version is highlighted.

$ php --version
PHP 7.1.19 (cli) (built: Aug 17 2018 18:03:17) ( NTS )
Copyright (c) 1997-2018 The PHP Group

Now we move into a working directory and download the source making sure to update the following for the version from above.

$ cd /tmp
$ curl -L http://php.net/get/php-{{php-version}}.tar.bz2/from/this/mirror > php.tar.bz2
$ open php.tar.bz2

Now we will compile and test the extension.

$ cd php-{{php-version}}/ext/{{extension}}
$ phpize
$ ./configure
$ make
$ make test
$ sudo make install

If all that goes well finally we'll need to add the following to our php.ini - I usually add at it at the end of the file.

extension = {{extension}}.so

You can verify your installation with the following:

$ php --info | grep {{extension}}\\.

Lastly, depending on your setup now you may want to restart apache.

$ sudo apachectl restart

Comment by: Kevin on

Kevin's Gravatar Where is php.ini?

Comment by: Jonathan on

Jonathan's Gravatar Thank you for this. I must've tried half a dozen different guides doing all sorts of strange things in my attempts to install mcrypt. This guide was the most clear, concise, and easy to follow. I am not certain what PEAR is being used for though.

Comment by: Jesse G. Donat on

Jesse G. Donat's Gravatar Good catch! The PEAR piece was left over from an older version of the guide that utilized PEAR's `pecl` command. I have updated the guide accordingly.

Comment by: Anant on

Anant's Gravatar Thank you , steps are pretty simple and its working

Comment by: kmabas on

kmabas's Gravatar Don't forget disable SIP in El capitan and earlier and if "phpize" doesn't work, is because you need to install xcode cli tools

Comment by: DMITRY LOBURETS on

DMITRY LOBURETS's Gravatar Great thank you! This is the only thing after an hour of googling which help to me!

Comment by: Umberto on

Umberto's Gravatar Hello there.

I would like to point some additional steps I had to do to get it set:
1. copy /etc/php.ini.default to /etc/php.ini
2. locate the mcrypt.so (in my case it was in
/usr/local/Cellar/php56-mcrypt/5.6.30_4/mcrypt.so

https://stackoverflow.com/questions/14357107/installing-mcrypt-on-osx-10-8-2-php-5-3-15-with-homebrew

Comment by: Umberto on

Umberto's Gravatar Hello there.

I would like to point some additional steps I had to do to get it set:
1. copy /etc/php.ini.default to /etc/php.ini
2. locate the mcrypt.so:
/usr/local/Cellar/php56-mcrypt/5.6.30_4/mcrypt.so
3. add the line below to php.ini:
extension = /usr/local/Cellar/php56-mcrypt/5.6.30_4/mcrypt.so

After that I would have opencart running well

https://stackoverflow.com/questions/14357107/installing-mcrypt-on-osx-10-8-2-php-5-3-15-with-homebrew

Comment by: shikha on

shikha's Gravatar after running make cmd, getting this error

/tmp/php-7.1.19/ext/mcrypt/mcrypt.c:25:10: fatal error: 'php.h' file not found

how to solve this one?
please help

Comment by: Jesse G Donat on

Jesse G Donat's Gravatar Mojave installation messes up some peoples header files for some unknown reason.

You can pretty easily reinstall them following my post here.

Comment by: Greg on

Greg's Gravatar Hi, I've try with intl extension on "sudo make isntall", I have an "Operation not permitted" error :

MacBook-Pro-de-Greg-2:intl greg$ sudo make install
Password:
Installing shared extensions: /usr/lib/php/extensions/no-debug-non-zts-20160303/
cp: /usr/lib/php/extensions/no-debug-non-zts-20160303/#INST@47872#: Operation not permitted
make: *** [install-modules] Error 1

Comment by: Jesse G Donat on

Jesse G Donat's Gravatar I think odds are good you have System Integrity Protection turned on.

I updated my post to note that you need to turn it off first, along with a link to directions on how to do so.

Comment by: Ben on

Ben's Gravatar
curl -L http://php.net/get/php-{{php-version}}.tar.bz2/from/this/mirror > php.tar.bz2
Outcome: -bash: syntax error near unexpected token

Comment by: Jesse G Donat on

Jesse G Donat's Gravatar Did you replace the `{{php-version}}` with your PHP Version - something like:
curl -L http://php.net/get/php-7.1.33.tar.bz2/from/this/mirror > php.tar.bz2

Comment by: Okky Sari on

Okky Sari's Gravatar Wow! Spent the whole day yesterday trying to get mcrypt on my (homebrew installed) php 5.6 and nothing worked :(

But this works flawlessly! Followed all the step, copied the newly built mcrypt library, edited php.ini, and voila! it magically works!

Can't thank you enough for this excellent post

Email address will never be publicly visible.

Basic HTML allowed.