Imagick Docker Alpine Linux php8.2 – Php

by
Ali Hasan
alpine-linux boot2docker composer-php imagick php-8.2

Quick Fix: Install the necessary dependencies using apk add and then install the imagick extension using pecl install imagick. Finally, enable the extension using docker-php-ext-enable imagick.

The Problem:

I am facing issues installing Imagick for PHP 8.2 on Alpine Linux using Docker. I added a repository to the Dockerfile, but I encountered an error indicating that the "php82-common" package is missing. I cannot find any information about this package and need help resolving the installation.

The Solutions:

Solution 1: Using Additional Repositories

To install Imagick extension on Alpine Linux for PHP8.2, follow these steps:

  1. Add the testing repository to your Dockerfile:

    apk add --no-cache --virtual .build-Deps $PHPIZE_DEPS imagemagick-dev
    
  2. Install Imagick using PECL:

    && pecl install imagick
    
  3. Enable the Imagick extension:

    && docker-php-ext-enable imagick
    
  4. Remove the temporary repository:

    && apk del .build-Deps
    

This approach allows you to install Imagick without encountering the "php82-common (no such package)" error.

Solution 2: Custom Docker

Use a custom Docker image to install Imagick separately from your main application. Create a new Dockerfile with the following contents:

# Use the official PHP image as a base
FROM php:8.2

# Install the necessary system packages
# Alpine Linux
apk add --update imagemagick imagemagick-dev

# Install and enable the Imagick extension
apk add php8-pecl-Imagick
php8 -m | grep Imagick

# Add your application code and configuration
COPY . /var/www/html
  

Then, build and run the image:

# Build the Docker image
 docker build -t my-php-image .

# Run the image
 docker run -p 80:80 my-php-image
  

This method gives you more control over the installation process and allows you to install other extensions as needed.

Solution 3: Fixed Dockerfile

The original error was caused by specifying the testing repository for the php82-pecl-Imagick package, which has since been moved to the community repository. To fix this, the --repository option should be updated to point to the community repository:

apk add php82-pecl-Imagick --repository=https://dl-pkg.alpinelinux.org/alpine/edge/community

Additionally, the pecl command is required to install the Imagick PHP extension:

pecl install imagick

Here’s the fixed Dockerfile:

FROM laravelphp/vapor:php82

apk --update add postgresql14-client

# INSTALL COMPOSER
curl -s https://getcomposer.org/installer | php
mv composer.phar composer

# INSTALL PHP EXT
apk add php82-pecl-Imagick --repository=https://dl-pkg.alpinelinux.org/alpine/edge/community
apk --update add imagemagick imagemagick-dev
pecl install imagick
docker-php-ext-enable imagick

docker-php-ext-install pcntl
docker-php-ext-install exif

# Place application in application directory...
COPY . /var/task

Q&A

How to install imagick on Alpine linux?

RUN apk add –no-cache –virtual .build-deps $PHPIZE_DEPS imagemagick-dev && pecl install imagick && docker-php-ext-enable imagick && apk del .build-deps

Why php82-pecl-imagick package is not found?

The php82-pecl-imagick package is located in the community repository. You need to pass –repository=https://dl-cdn.alpinelinux.org/alpine/edge/community to apk’s –repository option.

Why need ‘pecl install imagick’ ?

You are missing the pecl command for installing the Imagick PHP extension.

Video Explanation:

The following video, titled "Install PHP Extensions Easily in Docker - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Install PHP extensions easily with mlocati/docker-php-extensions-installer https://github.com/mlocati/docker-php-extension-installer.