code.g
code.g

Reputation: 193

Docker php—install php extensions ssh2

I am using the official php docker image as base for my application container, so the Dockerfile starts like so:

FROM php:5.6-fpm-jessie

Later in the file I would like to have something like that:

 RUN apt-get update \
    && apt-get install -y libssh2-1-dev libssh2-1 \
    && docker-php-ext-install ssh2

But that tells me:

/usr/src/php/ext/ssh2 does not exist

So since that is a debian (yessie) based image, only the old php5 packages are available and php7 is installed by some tricky script in the php:fpm dockerfile and it seams that all extensions are compiled within the used php executable.

How can I install more extensions in this scenario?

Upvotes: 7

Views: 18455

Answers (10)

Wadim Sewostjanow
Wadim Sewostjanow

Reputation: 41

I have added the following for laravel sail:

php8.2-ssh2 \

Upvotes: 0

Ramas Win
Ramas Win

Reputation: 17

Working solution with PHP 7.4:

FROM php:7.4-fpm
RUN apt-get install -y libssh2-1 libssh2-1-dev
RUN pecl install ssh2-1.2 && docker-php-ext-enable ssh2

Upvotes: 0

morakabi
morakabi

Reputation: 1

I had some error on an other solutions In my running container on richarvey/nginx-php-fpm image worked with two command.

apk add --no-cache libssh2-dev autoconf build-base
pecl install https://pecl.php.net/get/ssh2-1.3.tgz
docker-php-ext-enable ssh2

Upvotes: 0

Michele Locati
Michele Locati

Reputation: 1844

The easiest solution is to use install-php-extensions. It's a script that can install the required system libraries automatically, on any Docker PHP image.

COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/

RUN install-php-extensions ssh2

install-php-extensions can install a ton of other PHP extensions as well, not only ssh2: more details at https://github.com/mlocati/docker-php-extension-installer

Upvotes: 2

Mantas Vaitkūnas
Mantas Vaitkūnas

Reputation: 744

In case you are using PHP >=8.0 image:

FROM php:8.0.2-fpm

RUN apt-get install -y libssh2-1-dev libssh2-1 \
    && pecl install ssh2-1.3.1 \
    && docker-php-ext-enable ssh2

Upvotes: 13

Sergii Shymko
Sergii Shymko

Reputation: 124

The solution that worked for me on the CLI image of PHP 7.2:

FROM php:7.2-cli

RUN apt-get install -y libssh2-1 libssh2-1-dev
RUN pecl install ssh2-1.2 && docker-php-ext-enable ssh2

Build the Docker image and confirm the SSH2 extension is installed:

docker build -t example/php-build .
docker run --rm example/php-build php --ri ssh2

ssh2

SSH2 support => enabled
extension version => 1.2
libssh2 version => 1.8.0
banner => SSH-2.0-libssh2_1.8.0

Upvotes: 1

marcogmonteiro
marcogmonteiro

Reputation: 2162

For someone looking for a newer version of this for php 7.4 you'll need the following:

install dependencies

RUN apt-get update && apt-get install -y \
    ssh \
    libssh2-1 \
    libssh2-1-dev \
    wget \
    libssl-dev \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Install ssh2 extension

RUN wget -O libssh2.tar.gz https://www.libssh2.org/download/libssh2-1.9.0.tar.gz \
    && wget -O ssh2.tgz https://pecl.php.net/get/ssh2-1.2.tgz \
    && mkdir libssh2 && tar vxzf libssh2.tar.gz -C libssh2 --strip-components 1 \
    && mkdir ssh2 && tar vxzf ssh2.tgz -C ssh2 --strip-components 1 \
    && cd libssh2 && ./configure \
    && make && make install \
    && cd ../ssh2 && phpize && ./configure --with-ssh2 \
    && make && make install \
    && echo "extension=ssh2.so" >> /usr/local/etc/php/conf.d/ssh2.ini \
    && cd ../ && rm -rf libssh2.tar.gz ssh2.tgz ssh2 libssh2

The links posted here will only work up to php 7.2.

Upvotes: 0

Léo Benoist
Léo Benoist

Reputation: 2541

For Alpine and php > 7

RUN apk add --no-cache libssh2-dev autoconf build-base
RUN pecl install ssh2-1.2 && docker-php-ext-enable ssh2

Upvotes: 7

lightswitch05
lightswitch05

Reputation: 9428

It looks like the pecl option isn't available for anything newer then PHP 6. In order to get it installed, I had to build from source. From php:7.1.27-cli I was able to get it setup with these commands - the php:7.1.27-cli image is based on debian stretch:

# install dependencies 
RUN apt-get update && apt-get install -y \
    ssh \
    libssh2-1 \
    libssh2-1-dev \
    wget \
    libssl-dev \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Install ssh2 extension
RUN wget -O libssh2.tar.gz https://www.libssh2.org/download/libssh2-1.8.1.tar.gz \
    && wget -O ssh2.tgz https://pecl.php.net/get/ssh2-1.1.2.tgz \
    && mkdir libssh2 && tar vxzf libssh2.tar.gz -C libssh2 --strip-components 1 \
    && mkdir ssh2 && tar vxzf ssh2.tgz -C ssh2 --strip-components 1 \
    && cd libssh2 && ./configure \
    && make && make install \
    && cd ../ssh2 && phpize && ./configure --with-ssh2 \
    && make && make install \
    && echo "extension=ssh2.so" >> /usr/local/etc/php/conf.d/ssh2.ini \
    && cd ../ && rm -rf libssh2.tar.gz ssh2.tgz ssh2 libssh2

Upvotes: 4

code.g
code.g

Reputation: 193

Finally, this is successful.

&& apt-get install -y libssh2-1-dev libssh2-1 \
&& pecl install ssh2 \
&& docker-php-ext-enable ssh2 

Upvotes: 5

Related Questions