When I try to add the imagick
php-extension to my Docker image, I get an error by using PHP.
FROM php:8.2-fpm-alpine
RUN apk add --update --no-cache --virtual .build-deps $PHPIZE_DEPS imagemagick imagemagick-dev libgomp \
&& pecl install imagick \
&& echo 'extension=imagick.so;' >> $PHP_INI_DIR/conf.d/imagick.ini \
&& rm -rf /tmp/pear \
&& apk del .build-deps
Warning: PHP Startup: Unable to load dynamic library 'imagick.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20220829/imagick.so (Error loading shared library libgomp.so.1: No such file or directory (needed by /usr/local/lib/php/extensions/no-debug-non-zts-20220829/imagick.so)), /usr/local/lib/php/extensions/no-debug-non-zts-20220829/imagick.so.so (Error loading shared library /usr/local/lib/php/extensions/no-debug-non-zts-20220829/imagick.so.so: No such file or directory)) in Unknown on line 0
How can I fix this?
You're installing the library as a build dependency and then you're deleting it:
apk add --update --no-cache --virtual .build-deps ... imagemagick imagemagick-dev libgomp
apk del .build-deps
The libraries shouldn't be part of the .build-deps
step, so you'll want to pull those packages out into a separate line. And note that imagemagick-dev
implicitly includes imagemagick
and libgomp
. You want something like:
RUN apk update \
&& apk add --no-cache imagemagick-dev \
&& apk add --no-cache --virtual .build-deps $PHPIZE_DEPS \
&& pecl install imagick \
&& echo 'extension=imagick.so;' >> $PHP_INI_DIR/conf.d/imagick.ini \
&& rm -rf /tmp/pear \
&& apk del .build-deps
The build tools (like gcc) are a dev dependency, but the library itself is a run-time dependency, so it needs to stay in the image.