Installing libsodium for PHP on macOS Mojave 10.14

While mcrypt installation has been the subject of [developer] fits and [yours truly’s] restarts, mcrypt is in fact going away. libsodium is the new player in town, a point I anticipated long ago being the venerable tech expert that I am just found out being the accountant who unfortunately bears the burden of having used dBase and Paradox in a past life.

That said, the process is relatively similar to mcrypt i.e. you will need brew (and I suggest XCode command line tools, just in case). Instructions for both are readily available via the only hyperlink in the paragraph immediately proceeding this one. Further, you need not bother with PHP source, but you might as well turn off SIP beforehand. Now let’s get started.

Open terminal and type …

$ brew install libsodium

There is a very good chance you will not have PEAR, and hence PECL, on your machine, so we’ll proceed to get that going next.

$ curl -s -O
$ sudo php install-pear-nozlib.phar -d /usr/local/lib/php -b /usr/local/bin

When that is done you’ll get a notification asking you to modify your php.ini file. That message may look like so:

You may want to add: /usr/local/lib/php to your php.ini include_path

You’ll then navigate to /private/etc, open said file, and add the following line in the include path section …


Next, it is worth checking to make sure PEAR is there. In terminal type this:

pear version

At which point you should see something like this …

PEAR Version: 1.10.9
PHP Version: 7.1.23
Zend Engine Version: 3.1.0
Running on: Darwin computer.local 18.5.0 Darwin Kernel Version 18.5.0: Mon Mar 11 20:40:32 PDT 2019; root:xnu-4903.251.3~3/RELEASE_X86_64 x86_64

Like ‘ol mcrypt before it, libsodium won’t actually run in PHP unless we plug it in. Only this time instead of compiling the .so extension from source we are going to leverage PECL to do that job for us. Enter the following command:

$ sudo pecl install libsodium

If all goes well you should you should see the following from the PHP test page you keep on file for just such occasions:

Alternatively, you can dump the following code into a file (call it sodium_test.php):


Pointing your browser at that should produce a result like so:

array(3) { [0]=> int(10) [1]=> int(2) [2]=> string(6) "1.0.17" }

Version 2.0.20 is relatively recent, nevertheless your results may vary.

MG signing off (because this encryption stuff is hard work … for someone else)

Credits: Paragon Initiative and Stack Overflow.

Disclaimer: The instructions above are provided without warranty, expressed or implied, as to fitness for particular purpose and/or merchantability and/or the possibility of mental breakdown. Use the associated encryption technologies at your own risk.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.