How to install ssh2.so for PHP 5.3.x on OSX Lion and MAMP

For a recent project I needed to use the ssh2 extension with PHP. I had tried to use phpseclib, but found that it doesn’t support for DSA keys (a requirement for my project). Using the instructions below, you can easily install ssh2 support for PHP using homebrew.

Using homebrew to install libssh2:

brew install libssh2

I was working with a fresh OSX Lion install, so I needed to install pear. Luckily it comes with Lion, so we just need to install and update.

sudo php -d detect_unicode=0 /usr/lib/php/install-pear-nozlib.phar
sudo pear upgrade

After pear is setup, we can use pecl to install the ssh2 extension:

sudo pecl install channel://pecl.php.net/ssh2-0.11.3

When prompted for the libssh2 prefix, just press enter for autodection:

libssh2 prefix? [autodetect] :

This next step is optional and should not be done if you already have a /private/etc/php.ini file. However, this step may help others who are working with a fresh install like I was:

sudo cp /private/etc/php.ini.default /private/etc/php.ini

Now we need to add the ssh2.so to the php.ini …so open /private/etc/php.ini with your favorite editor and add the following line:

extension=ssh2.so

After saving, run the following command and it should output ‘ssh2′:

php -m | grep ssh2

Now lets copy ssh2.so to the MAMP extension folder:

cp /usr/lib/php/extensions/no-debug-non-zts-20090626/ssh2.so /Applications/MAMP//bin/php/php5.3.6/lib/php/extensions/no-debug-non-zts-20090626/

Edit the php.ini template for MAMP’s PHP install and add:

extension=ssh2.so

MAMP PHP Template

Restart MAMP… ssh2 extension should now be installed for the system’s PHP and MAMP’s PHP.

Output of phpinfo(); will show this:

  • flexic

    Excellent instructions, I’ll throw in you need to restart Apache 2 on Lion to get the ssh2 library to register in phpinfo()

    sudo apachectl restart

    Thanks for the post!

  • Juho Juonio

    Awesome!

  • f*E!

    With MacPort you need to set prefix like that:

    libssh2 prefix? [autodetect] : /opt/local

  • http://twitter.com/danpastori Dan Pastori

    Extremely helpful article. Heads up, should fix the tweet button. I tried to share, but the tweet button didn’t seem to work. Keep up the good work!

    • logicmastermind

      Thanks for the heads up on the tweet button. It’s fixed now…