Installing the SSH2 PHP extension on CentOS is actually pretty simple. Although it isn’t available as a straight yum install, it boils down to a few short steps.

Before we start, please ensure that you have the EPEL repo installed. If you don’t, goto http://fedoraproject.org/wiki/About_EPEL to get instructions.

Firstly, we’re going to install the dependencies:

yum install gcc php-devel php-pear libssh2 libssh2-devel

These will allow us to build the SSH2 extension using pecl.

pecl install -f ssh2


After running that command, it should stop at a line like:

..
........done: 22,740 bytes
5 source files, building
running: phpize
Configuring for:
PHP Api Version:         20041225
Zend Module Api No:      20060613
Zend Extension Api No:   220060519
libssh2 prefix? [autodetect] :

All you have to do is hit Enter and it should detect the proper path. Once the install is completed, you just have to tell PHP to load the extension when it boots.

touch /etc/php.d/ssh2.ini
echo extension=ssh2.so > /etc/php.d/ssh2.ini

Now restart your webserver and test to see if the changes took effect.

/etc/init.d/httpd restart
php -m | grep ssh2

You should get a line returned with ‘ssh2’.

  • Comment by
    Jacob
    30 Jun 2011

    Hi LeCook,

    Did you follow the first step? If you have libssh2 installed in a non-standard directory, this will cause the install to fail.

    Also, do you have openssl installed? Try yum install openssl.

    Cheers

  • Comment by
    Jacob
    2 Sep 2011

    @Weave, thanks for that! Let’s blame that on some sort of weird copy+paste and fat finger combination..

  • Comment by
    Jacob
    26 Oct 2011

    Hi Freddy,

    This usually happens when you don’t install the ‘libssh2-devel’ package. Can you paste the output of ‘yum info libssh2-devel’?

  • Comment by
    Jacob
    5 Mar 2012

    @icode: Make sure that you have the EPEL repo setup and enabled. Check it out here http://fedoraproject.org/wiki/About_EPEL

  • Comment by
    Jacob
    21 Mar 2012

    @Arun

    Installation in Ubuntu should be as simple as ‘sudo apt-get install libssh2-1-dev libssh2-php’. Once you’ve done that, skip the rest of the steps until:

    /etc/init.d/httpd restart
    php -m | grep ssh2

    You’ll want to do

    /etc/init.d/apache2 restart
    php -m | grep ssh2

  • Comment by
    Jacob
    12 Apr 2012

    Jason, did you get that figured out? Seems as though libssh2 didn’t install. Can you paste the output of ‘yum install libssh2’?

  • Comment by
    Arlo
    18 Jun 2012

    Great tutorial but I’m falling over at the last step.

    Running php -m gives:
    PHP Warning: PHP Startup: Unable to load dynamic library ‘/usr/lib/php/modules/ssh2.so’ – /usr/lib/php/modules/ssh2.so: undefined symbol: libssh2_sftp_readdir_ex in Unknown on line 0

    The .so is there, readable, etc and (grep) it contains the string (at least) libssh2_sftp_readdir_ex

    nm says the function is undefined (but declared?)

    I assume it’s linking this function in from somewhere else whilst building (it’s not in the code but it is in a clean build’s output from the code.

    I’ve searched the system for anything older but it appears to be the only ssh2.so. I can’t find a definition of this error beyond the obvious.

    Any suggestions? Any idea where it’s picking this function declaration up from?

    Comment by
    Arlo
    18 Jun 2012

    … SOLVED

    the ssh2.so build was building against a new libssh2.so library but the php was loadng an old libssh2.so library.

    Copied the newer library over the old one (in /usr/lib), restarted apache and it works.

    Spent 2 days doing this and 5 minutes after asking the question …

  • Comment by
    Jacob
    18 Jun 2012

    Hi Arlo,

    Great to hear that you figured it out! Could you give me some exact steps that you performed to make that fix? I’d like to update my post to include it.

    Thanks!

  • Comment by
    Jacob
    21 Jun 2012

    Hi J. Storm,

    You’ll need the EPEL repo -> http://fedoraproject.org/wiki/About_EPEL. I have updated the post as well.

  • Comment by
    Jacob
    22 Jun 2012

    Hi Vhan,

    Can I get you to paste the output of netstat -nat | grep “:80” ?

  • Comment by
    Jacob
    16 Aug 2012

    Hi Kyle,

    Make sure that you have the EPEL yum repository installed and that you run yum install gcc php-devel php-pear libssh2 libssh2-devel.

    Looks like you haven’t installed libssh2.

  • Comment by
    Jacob
    22 Nov 2012

    cvetic, do you have any CentOS repositories enabled? Can you paste the output of the following commands?

    1) cat /etc/redhat-release
    2) yum repolist

  • Comment by
    Jacob
    9 Sep 2013

    Hi Mike,

    Would you mind posting your fix with some more detail in case anyone else runs into the same issue?

  • Comment by
    Jacob
    23 Oct 2013

    Hi Kamrul,

    Could you provide the output of ‘yum info libssh2-devel’?

    Thanks

Sorry, comments are closed.