Add the SSH2 extension to MAMP

If you want to use SSH or SFTP with PHP you need the SSH2 extension. Unfortunately MAMP doesn’t come with it out of the box. Moreover, it comes with no PHP sources making it a bit complicated to use pecl to install the extension. So here I document it, mostly for my future self and in the hope that I’ll spare a poor soul the trouble. The middle part of the instructions is a prerequisite for installing any other PHP extension, so here’s another utility to this.

OS X 10.11 El Capitan pre-requisites

Unfortunately OS X 10.11 El Capitan no longer includes the development headers for the OpenSSL library which are required to build libssh2. As a result, we have to install a copy of the library using Homebrew:

brew update
brew install openssl

This will install OpenSSL in the directory /usr/local/opt/openssl

Installing libssh2

First we need to install libssh2 which is a dependency to the PHP module. Download libssh2 from its page (direct link). In Terminal commands:

tar xvzf libssh2-1.4.3.tar.gz
cd libssh2-1.4.3
./configure --with-libssl-prefix=/usr/local/opt/openssl
make
sudo make install

Important note: the --with-libssl-prefix=/usr/local/opt/openssl part should only be used on Mac OS X El Capitan if you followed the instructions of the previous section for installing OpenSSL through Homebrew.

If you are missing autoconf you may try to install it using Homebrew:

brew install autoconf

Make it possible to use pecl with MAMP

You need to download the PHP sources matching your version, place them into the corresponding Applications/MAMP/bin/php/phpX.Y.Z/include.php directory and run ./configure against it to create the necessary headers. For example, for PHP 5.6.10:

cd /Applications/MAMP/bin/php/php5.6.10
wget http://de1.php.net/get/php-5.6.10.tar.bz2/from/this/mirror -O php-5.6.10.tar.bz2
tar xjf php-5.6.10.tar.bz2
mv php-5.6.10 php
mkdir include
mv php include/
cd include/php
./configure

Install SSH2 through PECL and activate it

Now you can simply do

pecl install ssh2

When asked about libssh2 location just press ENTER.

As mentioned above, if you are told that you are missing autoconf you may try to install it using Homebrew:

brew install autoconf

and then retry the installation with PECL.

Now open MAMP Pro’s main window and hit โŒ˜-4 or go to File > Edit Template > PHP 5.6.10. Find the Extensions section and add the line

extension=ssh2.so

Save the file, close the editor window and restart Apache. To confirm that everything is installed launch a phpinfo() page and loog for ssh2. It should be present.

For the CLI executable of PHP you also need to edit another file, /Applications/MAMP/bin/php/php5.6.10/conf/php.ini Again add the line

extension=ssh2.so

To confirm that everything is installed porperly for the CLI PHP binary do

php -i | grep ssh

you should get something like

Registered PHP Streams => https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar, zip, ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp
ssh2
libssh2 version => 1.4.3
banner => SSH-2.0-libssh2_1.4.3

That’s it!

3 Replies to “Add the SSH2 extension to MAMP”

  1. Hiya,

    Thanks for the guide. Couple of points – you have a typo here
    [i]… into the corresponding Applications/MAMP/bin/php/phpX.Y.Z/include.php directory ..[/i]

    Should have a ‘/’ between the include and php. Threw me for a couple of seconds on how the folders should look ๐Ÿ™‚

    Also, got stuck trying to get pecl working. Then found that the php my mac was pointing too was the local version not the MAMP version. Created a .bash_profile that pointed to MAMP and it ran better. See this link for more details:

    [url=http://stackoverflow.com/questions/4145667/how-to-override-the-path-of-php-to-use-the-mamp-path
    ][/url]

    When I ran pecl it showed this error:

    [i]”Failed to download pecl/ssh2 within preferred state “stable”, latest release is version 0.12, stability “beta”, use “channel://pecl.php.net/ssh2-0.12″ to install” [/i]

    So amended your install line to this:

    [i]pecl install channel://pecl.php.net/ssh2-0.12[/i]

    and it installed perfectly.

    Hope this helps others too ๐Ÿ™‚

  2. I had the same issues but had no pecl in my environment. Changed to the /Applications/MAMP/bin/php/php5.6.10/bin directory before running:

    pecl install channel://pecl.php.net/ssh2-0.12

  3. Thank you very much, this step by step works perfectly.
    It saved thousands of hours of my time.

    ndlr: why am I still in this php 5.6.10 again?

Leave a Reply

Your email address will not be published. Required fields are marked *

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