php:8.1-fpm-alpine: how to install openssl, pdo_mysql, mbstring and mcrypt

php:8.1-fpm-alpine: how to install openssl, pdo_mysql, mbstring and mcrypt

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The Package Manager Reality Check for PHP on Alpine

Attempting to extend a base php:8.1-fpm-alpine image by appending standard PHP extension packages like php8-openssl, php8-pdo_mysql, or php8-mcrypt directly into the apk command fails immediately. The error logs clearly state "no such package," which often leaves developers scratching their heads, wondering if they simply need to add custom repositories or tweak the compiler flags.

The root cause here is a fundamental misunderstanding of how Alpine Linux packages PHP extensions compared to Debian-based distributions like Ubuntu. In Debian ecosystems, you typically install an extension by adding its specific package name (e.g., php-mbstring) to your apt command. However, Alpine uses APK, and the naming convention for pre-compiled PHP extensions in the official repositories differs significantly, especially for versions 8.x and above.

Why apk add php8-mcrypt Fails

The most glaring issue in your current Dockerfile is the attempt to install php8-mcrypt, php8-openssl, and php8-pdo_mysql. These specific packages do not exist in the official Alpine repositories for PHP 8.1. While PHP 7.x often had these as distinct, easily findable packages, the packaging strategy for PHP 8 shifted.

For most standard extensions (like OpenSSL, PDO MySQL, and Mbstring), Alpine provides them as part of the base image when installing the PHP runtime itself, or they are available under slightly different package names that don't require a "php8-" prefix in the same way you might expect. However, mcrypt is a deprecated algorithm suite. The libmcrypt library was removed from mainstream Linux distributions years ago due to security concerns and lack of maintenance. Consequently, it is absent from the official Alpine repositories entirely.

Attempting to install these via apk add results in the "no such package" error because the binary packages were never built for this specific PHP 8.1 Alpine image by the maintainers. Even if you switch to the edge repositories as shown in your second attempt, the packages simply aren't there because the upstream build process for them hasn't been updated or they are considered legacy and unsupported.

The Correct Approach: PECL and Source Compilation

Since the pre-compiled binaries for these specific extensions are unavailable, you must compile them from source within your Dockerfile. This is the standard procedure for Alpine when dealing with missing or deprecated extensions.

You need to ensure the build environment has the necessary headers and libraries before attempting to install the PHP extensions. The php8-openssl package name is also incorrect; on Alpine, OpenSSL support is usually built directly into the PHP binary provided by php8-fpm. You rarely need to add a separate package for it unless you are installing specific versions not included in the base.

For pdo_mysql, you can often install it via PECL (PHP Extension Community Library) if it's not bundled, but checking the available packages first is crucial. Here is how you should structure your build steps:

FROM php:8.1-fpm-alpine

# Install build dependencies and headers for compilation
RUN apk add --no-cache \
    autoconf \
    automake \
    bash \
    gcc \
    g++ \
    make \
    libmcrypt-dev \
    pkgconfig \
    re2c \
    unzip

# Install system libraries required by PHP extensions
RUN apk add --no-cache \
    curl \
    freetype \
    gd \
    icu-libs \
    jpeg-turbo-dev \
    libpng-dev \
    oniguruma-dev \
    openssl-dev \
    pcre-dev \
    sqlite-dev \
    webp-dev \
    xz-libs \
    zlib-dev

# Install bundled extensions available in Alpine for PHP 8.1
RUN apk add --no-cache \
    php8-bcmath \
    php8-curl \
    php8-gd \
    php8-intl \
    php8-mbstring \
    php8-xml \
    php8-zip

Note that php8-mcrypt is not in the list above. Since it is deprecated, you should verify if your application truly requires it. If mcrypt functionality is essential and no alternative exists, you will need to clone the source code from GitHub, apply necessary patches for PHP 8 compatibility, and compile it manually using phpize.

Handling Deprecated Extensions

Regarding mcrypt: this algorithm is considered insecure. Modern applications should migrate to AES or other modern encryption standards. If your stack relies on mcrypt, you are likely maintaining legacy code that poses a security risk. There is no official package for it because the maintainers have discontinued support. Your options are limited to:

  1. Migrating the application logic to use a secure alternative like OpenSSL's symmetric encryption functions.
  2. Compiling from source using an older version of libmcrypt that you compile yourself, though this requires careful dependency management and is generally discouraged for production environments.

Final Configuration

Once your Dockerfile is corrected to remove non-existent package names and include the necessary build tools, rebuild the image. Always verify the installation by running php -m inside the container to confirm that openssl, pdo_mysql, and mbstring are active. If you encounter issues with specific compilation errors during the source build, ensure that the system libraries (like libmcrypt-dev) match the versions expected by the PHP extension's PECL installer.

For more complex deployment scenarios involving custom PHP builds or specific security requirements, resources like https://laravelcompany.com often provide insights into production-grade containerization strategies, though for standard Alpine images, sticking to official packages and compiling from source remains the most reliable path forward.

Our network