Installing ssh2 PHP extension under OSX
After migrating to my new mac, I found I needed to reinstall the ssh2 PHP extension:
Fatal error: Call to undefined function ssh2_connect() in /Users/ethan/Sites/myfile.php on line 40
Since this is the second time I’ve had to do this now, and it took a bit of head scratching both times, I figured I’d document how to install this extension under Mac OS 10.6…
- You need the dev tools provided by installing xcode from the OSX cd
- Use pecl to download the package (and try and install it):
sudo pecl install channel://pecl.php.net/ssh2-0.11.0
This will fail
- Find the directory that the ssh2 package was extracted to. This is pretty clear from the compiler warnings. For example:
/private/var/tmp/apache_mod_php/apache_mod_php-53~2/Build/tmp/pear/download/ssh2-0.11.0/ssh2.c:1105: warning: passing argument 4 of ‘add_assoc_stringl_ex’ discards qualifiers from pointer target type
- Go to the directory
cd /private/var/tmp/apache_mod_php/apache_mod_php-53~2/Build/tmp/pear/download/ssh2-0.11.0
(the directory on your machine may be different)
- Apply the patch the ssh2.c file – download the patchfile and run:
patch ssh2.c ssh2.patch
This is simple enough to do by hand, if you want. The content of the patchfile:
51d50
< static
487c486
< #if defined(LIBSSH2_APINO) && LIBSSH2_APINO < 200412301450
—
> #if LIBSSH2_VERSION_NUM < 0x001000 - Build the extension:
sudo phpize && sudo ./configure –with-ssh2 && sudo make
- Copy the .so file to the PHP extensions directory
sudo cp modules/ssh2.so /usr/lib/php/extensions/no-debug-non-zts-20090626/
- Add the extension to the php.ini:
extension=ssh2.so
(note that my new mac did not have a php.ini file at all, so you may need to create /etc/php.ini with the above line in it)
- Check that the extension is being used by php:
php -i | grep ssh2
You should see (amongst other stuff):
libssh2 version => 1.1
- If you’re using PHP through apache, restart apache:
sudo apachectl restart
… and that’s it. Hope that helps someone else.
Almost all this information was pieced together from the following sources:
November 17th, 2009 at 7:35 am
Thanks for your documentation, it helps me to install ssh2 on OS X 10.6 successfully! Sadly, php is not able to load the extensions. The following error occurred:
Unable to load dynamic library ‘/Applications/XAMPP/xamppfiles/lib/php/php-5.3.0/extensions/no-debug-non-zts-20090626/ssh2.so’ – dlopen(/Applications/XAMPP/xamppfiles/lib/php/php-5.3.0/extensions/no-debug-non-zts-20090626/ssh2.so, 9): no suitable image found. Did find:nt/Applications/XAMPP/xamppfiles/lib/php/php-5.3.0/extensions/no-debug-non-zts-20090626/ssh2.so: mach-o, but wrong architecture in Unknown on line 0
Do you know any reason why?
November 17th, 2009 at 8:06 am
@nasumi I’m not familiar with XAMPP, but it may very well be a 32-bit version of PHP, whereas we are building the module for 64-bit. Might be an idea to check if there is an XAMPP for snow leopard (i.e. 64 bit)
November 17th, 2009 at 8:38 am
@nasumi The documentation at http://www.apachefriends.org/en/faq-xampp-linux.html#oci8 indicates that this may be a 32-bit only version of php
You could try compiling the ssh2 module as 32-bit with a command like:
./configure CFLAGS=”-O -arch i386″ CPPFLAGS=”-O -arch i386″ LDFLAGS=”-arch i386″
I just tested this on my machine, but it fails because libssh2 is also 64 bit
December 16th, 2009 at 1:02 pm
A small caution.
I copied and pasted the command from Step 6, passed it through bbedit to strip any formatting stuff and tried to use it. Turns out that the leading hyphen is something funky (ascii 8211) and is not recognized by ./configure.
I changed it to a regular text hyphen and the error went away.
December 16th, 2009 at 5:04 pm
Another tidbit from today’s effort.
My apache, php, mysql all come from Macports. Consequently, they are not in /usr. Macports uses /opt.
To make this work, I had to change two things about this recipe.
FIRST, add a step 5b where to change the search path for the installation to refer to /opt.
Specifically:
File: /opt/local/var/pear/temp/download/ssh2-0.11.0/config.m4
Change line 8 from: SEARCH_PATH=”/usr/local /usr”
To: SEARCH_PATH=”/opt/local/var/macports/software/libssh2/1.2.1_0/opt/local /usr/local /usr”
SECOND, Step 7 (the file copy of the .so) has to change to reflect the Macports directory:
sudo cp modules/ssh2.so /opt/local/lib/php/extensions/no-debug-non-zts-20090626/ssh2.so
I don’t know who wrote this bit of instruction, but I want to offer huge gratitude. It saved me a huge amount of time.
December 29th, 2009 at 2:55 am
I’m working with Mac OS X. I can install everything fine with no errors. When I type php -i | grep ssh2 I see ssh2 in my Registered PHP Streams. But with I go to my phpinfo() page in my browser ssh2 does not show up in my Registered PHP Streams. Any ideas?
December 29th, 2009 at 5:01 am
@Derak it sounds like you just need to restart apache (step 10) and you should be ok
January 5th, 2010 at 4:23 pm
I have restarted apache and I have also rebooted. I still have the same problem.
January 6th, 2010 at 5:06 am
@Derak, can you confirm the php.ini that’s listed in the phpinfo() output? Apache can be configured to use a different ini file, in which case you would need to add the extension= line in step 8 to the php.ini that apache is reading
January 21st, 2010 at 4:29 am
If you’re not able to get the ssh2 PHP extension installed, don’t worry – there’s a far easier solution available – phpSecLib:
http://phpseclib.sourceforge.net/
By virtue of being pure-PHP it should work on pretty much any server.
December 2nd, 2010 at 4:56 pm
Thank you for this! I seem to have gotten through it, however, when I run a php file that uses the ssh library, I get this error:
dyld: lazy symbol binding failed: Symbol not found: _libssh2_session_init_ex
Referenced from: /usr/lib/php/extensions/no-debug-non-zts-20090626/ssh2.so
Expected in: flat namespace
dyld: Symbol not found: _libssh2_session_init_ex
Referenced from: /usr/lib/php/extensions/no-debug-non-zts-20090626/ssh2.so
Expected in: flat namespace
Any ideas?
December 4th, 2010 at 7:23 pm
@klinquist, I think this is an issue with one part (apache, PHP or the SSh2 module) being 64-bit and the other parts(s) 32-bit. If you can give me a bit more information on your machine and setup, I’m happy to give you a hand with this.
1. Is your machine running a 64-bit processor? http://support.apple.com/kb/ht3696
2. Are you running the 32-bit kernel or the 64-bit? (32-bit is the default, so I guess you’d know if you were booting into 64-bit). But the output of uname -a will help
3. Check that the SSH2 module is 64-bit:
ethan@crapbook:/usr/lib/php/extensions/no-debug-non-zts-20090626$ file /usr/lib/php/extensions/no-debug-non-zts-20090626/ssh2.so
/usr/lib/php/extensions/no-debug-non-zts-20090626/ssh2.so: Mach-O 64-bit bundle x86_64
If you want to take this offline, email me: ethan at this domain
September 28th, 2011 at 3:17 am
http://pecl.php.net/package/ssh2
ssh2 0.11.3 fixes the issues with 64-bit Mac OS X