Install the PECL SSH2 extension to MAMP PRO and the PHP CLI in OSX.
Or madness in general. So as you may know, I’ve started using PHPStorm to work locally. I decided to dust off an old SSH2 wrapper I wrote and play with how PHPStorm uses PHPUnit and does refactoring. I knew that I would have to install the extension via PECL and was like, “Oh it can’t be that bad to do it through MAMP PRO” and as you may have guessed, I sealed my own fate for a couple hours. One thing I can say is that, even though I use a convenient app like MAMP PRO to set up my local development environment, I’m glad my sysadmin-fu is up to snuff enough to fly without the conveniences because after this ordeal, I feel like I might as well have made my MAMP stack from scratch with all the hoops I jumped tonight. (I’m pretty sure that last sentence is also one of the worst run-on sentences ever).
The first thing you need to know is that the PECL command is located at the directory
/Applications/MAMP/bin/php/php5.3.6/bin
when I tried to run
sudo ./pecl install -a ssh2 channel://pecl.php.net/ssh2-0.11.3
from that directory, I got the error:
Notice: unserialize(): Error at offset 267 of 1133 bytes in Config.php on line 1050 PHP Notice: unserialize(): Error at offset 267 of 1133 bytes in /Applications/MAMP/bin/php/php5.3.6/lib/php/PEAR/Config.php on line 1050 ERROR: The default config file is not a valid config file or is corrupted.
I found out later that the error is most likely caused by the file
/Applications/MAMP/bin/php/php5.3.6/conf/pear.conf
So if you get an error, try doing
sudo mv /Applications/MAMP/bin/php/php5.3.6/conf/pear.conf /Applications/MAMP/bin/php/php5.3.6/conf/pear.conf.bkp
If you’re still getting the error, you can also the following:
- Install Xcode Tools from your Mac OS X DVD or download it from Apple Developer Connection
- Download the complete PHP source code from php.net into /Applications/MAMP/bin/php/php5.3.6/
- Create an include directory in the the php5.3.6 directory.
- Unzip/tar the php source archive and move it to/Applications/MAMP/bin/php/php5.3.6/include/
- Rename the php source directory to just php (so then it will look like /Applications/MAMP/bin/php/php5.3.6/include/php)
- In Terminal use the following commands to compile the extension:
cd /Applications/MAMP/bin/php/php5.3.6/include/php
./configure
That should allow you to run the PECL command however, you will probably get a complaint about not having libssh installed. So you will have to
- go download libssh2 (http://www.libssh2.org/). As much as I like git, I recommend downloading the latest release tarball instead of cloning the repo. For some reason, I got a version incompatibility error when trying to run the PECL install with the repo version and had to do all these steps over with the tarball release version.
- unzip the directory and cd to the upacked directory in the terminal.
- run: ./configure; make all install;
libssh should compile and place the output file in src/*.o - copy all headers from the include folder within the libssh2 directory to
/usr/local/include/ - copy all compiled files (*.o) from the src folder within the libssh2
directory to /usr/local/lib/
After you’ve successfully ran the the PECL install, it should tell you to Add the line
extension=ssh2.so
to the Dynamic Extensions area of your php.ini file. I believe both MAMP and MAMP PRO allow you to edit the necessary ini through File>Edit Template.
Lastly, restart apache. You should be able to see the SSH2 extension when you load a phpinfo() page.
Bonus round:
I’m not sure how I have PHP CLI working in OSX but if you’ve followed the steps above and try to verify the extension install with through the cli, it won’t show up.
$ php -a
php> phpinfo();
This is because (if you’re also using MAMP PRO) the binaries are different. The good news is that you can make things happy in 3 easy steps
- run `sudo ln -s /Applications/MAMP/bin/php/php5.3.6/lib/php/extensions/no-debug-non-zts-20090626/ssh2.so /usr/lib/php/extensions/no-debug-non-zts-20090626/ssh2.so` in the terminal.
- Edit the /etc/php.ini (copy /etc/php.ini.default if /etc/php.ini doesn’t exist) like you did for the MAMP php.ini(s)
- profit.
Note: I still have to install PHPUnit into my current setup so that may or may not elicit another blog post.
Sources:
- http://smbjorklund.no/how-enable-pecl-uploadprogress-extention-mamp
- http://php.net/manual/en/ref.ssh2.php
- http://forum.mamp.info/viewtopic.php?t=13815
Related articles
- Using MySQL Workbench with MAMP (phpprotip.com)
Mamp is a great tool when you want to work on a php project with small technicall requirements, such as Drupal or WordPress projects. But when you want to do more complex things, it has just so many limitations and unfixed bugs: http://forum.mamp.info/viewtop…
In my opinion, if you need PECL or if you need to compile php by yourself, Mamp is just not the right tool anymore. It's lame, i like their UI.
For 90% of my use case, MAMP is perfect for my development needs. The limitations are the trade off for not playing sysadmin for projects.
I do agree that if your needs are complex, you're better off compiling from scratch/playing sysadmin and last night it felt I probably should've did that to start off with. Thanks for the link on more CLI tricks.
Hopefully the future iterations of MAMP will build in PECL/PEAR support into their product. I don't think I needed to compile PHP to get things to work, I think I just needed to move that file. Since I did those compilations steps first, I have no way of knowing which steps produced the desired effect. I recommended the file moving because after doing the compile steps, I still received the error. Only after moving that file did things work. I have no way of telling if the compilation steps had any influence on that outcome.