13

The official php7 docker image has the following example:

FROM php:7.0-fpm
RUN apt-get update && apt-get install -y libmemcached-dev \
    && pecl install memcached \
    && docker-php-ext-enable memcached

I'm trying to use FROM php:7.0-fpm-alpine:

RUN apk add --update --no-cache libmemcached-dev
RUN      pecl install memcached && docker-php-ext-enable memcached

PECL gives this error:

pecl/memcached requires PHP (version >= 5.2.0, version <= 6.0.0, excluded versions: 6.0.0), installed version is 7.0.13

How can I install the memcached php extension on alpine?

1
  • I believe I figured it out, will post instructions when I confirm it's working and have time to update here.
    – timetofly
    Commented Dec 1, 2016 at 18:05

3 Answers 3

30

Currently the php-memcached-dev:php7 branch contains the source for this extension.

To install it you can still use the docker-php-ext-* commands but you need to checkout the source yourself.

Base Installation

Assuming everything required to install the extension is already installed you can do:

RUN git clone -b php7 https://github.com/php-memcached-dev/php-memcached /usr/src/php/ext/memcached \
    && docker-php-ext-configure /usr/src/php/ext/memcached \
        --disable-memcached-sasl \
    && docker-php-ext-install /usr/src/php/ext/memcached \
    && rm -rf /usr/src/php/ext/memcached

This block will clone the repository, configure and install the extension then clean up after it self.

Pre-requisites

It is most likely that you need to install to packages to build the extension, we can add and remove them by doing:

ENV MEMCACHED_DEPS zlib-dev libmemcached-dev cyrus-sasl-dev git
RUN set -xe \
    && apk add --no-cache libmemcached-libs zlib \
    && apk add --no-cache \
        --virtual .memcached-deps \
        $MEMCACHED_DEPS \
    && git clone -b php7 https://github.com/php-memcached-dev/php-memcached /usr/src/php/ext/memcached \
    && docker-php-ext-configure /usr/src/php/ext/memcached \
        --disable-memcached-sasl \
    && docker-php-ext-install /usr/src/php/ext/memcached \
    && rm -rf /usr/src/php/ext/memcached \
    && apk del .memcached-deps

Update 17 May 2017

memcached has been added to the official pecl libraries for php7 now (v3 -> php7/7.1, v2 -> php5)

This makes installation a bit different

FROM php:7.0-alpine

ENV MEMCACHED_DEPS zlib-dev libmemcached-dev cyrus-sasl-dev
RUN apk add --no-cache --update libmemcached-libs zlib
RUN set -xe \
    && apk add --no-cache --update --virtual .phpize-deps $PHPIZE_DEPS \
    && apk add --no-cache --update --virtual .memcached-deps $MEMCACHED_DEPS \
    && pecl install memcached \
    && echo "extension=memcached.so" > /usr/local/etc/php/conf.d/20_memcached.ini \
    && rm -rf /usr/share/php7 \
    && rm -rf /tmp/* \
    && apk del .memcached-deps .phpize-deps
6
  • I've been looking for hours now to install php memcached. Your steps do not throw any errors but after installing I get the folowing error; PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-non-zts-20160303/memcached.so' - Error loading shared library libmemcached.so.11: No su ch file or directory (needed by /usr/local/lib/php/extensions/no-debug-non-zts-20160303/memcached.so) in Unknown on line 0 How can I fix this?
    – Odyssee
    Commented Feb 21, 2017 at 9:22
  • 3
    The issue is that libmemcached-dev is added as virtual dependency and removed afterwards. You have to add libmemcached library separately. So just add apk add --update libmemcached just below set -xe. @harry-bragg please update your answer. It was very helpful for me
    – Ololo
    Commented Mar 7, 2017 at 17:27
  • A few things have changed since I made this post, I will update it with how I am current doing things Commented May 14, 2017 at 13:50
  • @HarryBragg, what is the set -xe for?
    – Sebastian
    Commented Oct 16, 2017 at 12:54
  • 1
    @Sebastian Bit late maybe but... "-x Print a trace of simple commands. -e Exit immediately if a command returns a non-zero status." See also the manpage. You can just google 'man <command you want to learn more about>' for most of these things. Or even just in shell/cli on a unix system do 'man <command>' to get relevant info.
    – sg3s
    Commented Jul 9, 2018 at 7:42
25

Try it.

FROM php:7.2.10-fpm-alpine3.7


# Install PHP Extensions (igbinary & memcached)
RUN apk add --no-cache --update libmemcached-libs zlib
RUN set -xe && \
    cd /tmp/ && \
    apk add --no-cache --update --virtual .phpize-deps $PHPIZE_DEPS && \
    apk add --no-cache --update --virtual .memcached-deps zlib-dev libmemcached-dev cyrus-sasl-dev && \
# Install igbinary (memcached's deps)
    pecl install igbinary && \
# Install memcached
    ( \
        pecl install --nobuild memcached && \
        cd "$(pecl config-get temp_dir)/memcached" && \
        phpize && \
        ./configure --enable-memcached-igbinary && \
        make -j$(nproc) && \
        make install && \
        cd /tmp/ \
    ) && \
# Enable PHP extensions
    docker-php-ext-enable igbinary memcached && \
    rm -rf /tmp/* && \
    apk del .memcached-deps .phpize-deps
1
  • 1
    Top! Still working in 2021, solved my problem. Thanksalot! Commented Sep 7, 2021 at 8:12
2

This works for me on Debian 11

FROM php:7.4.28-apache
RUN apt-get install -y libz-dev libmemcached-dev && \
    apt-get install -y memcached libmemcached-tools && \
    pecl install memcached && \
    docker-php-ext-enable memcached

And you can start the daemon using something like this:

/etc/init.d/memcached start

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.