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…

  1. You need the dev tools provided by installing xcode from the OSX cd
  2. Use pecl to download the package (and try and install it):

    sudo pecl install channel://

    This will fail

  3. 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

  4. 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)

  5. 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:

    < static
    < #if defined(LIBSSH2_APINO) && LIBSSH2_APINO < 200412301450

    > #if LIBSSH2_VERSION_NUM < 0×001000

  6. Build the extension:

    sudo phpize && sudo ./configure –with-ssh2 && sudo make

  7. Copy the .so file to the PHP extensions directory

    sudo cp modules/ /usr/lib/php/extensions/no-debug-non-zts-20090626/

  8. Add the extension to the php.ini:

    (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)

  9. Check that the extension is being used by php:

    php -i | grep ssh2

    You should see (amongst other stuff):

    libssh2 version => 1.1

  10. 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:

13 Responses to “Installing ssh2 PHP extension under OSX”

  1. nasumi Says:

    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/’ – dlopen(/Applications/XAMPP/xamppfiles/lib/php/php-5.3.0/extensions/no-debug-non-zts-20090626/, 9): no suitable image found. Did find:\n\t/Applications/XAMPP/xamppfiles/lib/php/php-5.3.0/extensions/no-debug-non-zts-20090626/ mach-o, but wrong architecture in Unknown on line 0

    Do you know any reason why?

  2. ethan Says:

    @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)

  3. ethan Says:

    @nasumi The documentation at 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

  4. TQ White II Says:

    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.

  5. TQ White II Says:

    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.


    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/ /opt/local/lib/php/extensions/no-debug-non-zts-20090626/

    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.

  6. Derak Says:

    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?

  7. ethan Says:

    @Derak it sounds like you just need to restart apache (step 10) and you should be ok

  8. Derak Says:

    I have restarted apache and I have also rebooted. I still have the same problem.

  9. ethan Says:

    @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

  10. yardsspan Says:

    If you’re not able to get the ssh2 PHP extension installed, don’t worry – there’s a far easier solution available – phpSecLib:

    By virtue of being pure-PHP it should work on pretty much any server.

  11. klinquist Says:

    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/
    Expected in: flat namespace

    dyld: Symbol not found: _libssh2_session_init_ex
    Referenced from: /usr/lib/php/extensions/no-debug-non-zts-20090626/
    Expected in: flat namespace

    Any ideas?

  12. ethan Says:

    @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?

    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/

    /usr/lib/php/extensions/no-debug-non-zts-20090626/ Mach-O 64-bit bundle x86_64

    If you want to take this offline, email me: ethan at this domain

  13. Michael Shafrir Says:

    ssh2 0.11.3 fixes the issues with 64-bit Mac OS X

Leave a Reply