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:
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:
Restart MAMP… ssh2 extension should now be installed for the system’s PHP and MAMP’s PHP.
Output of phpinfo(); will show this: