XUtils

Docker PHP Extension Installer

Easily install PHP extensions in Docker containers.


Downloading the script on the fly with ADD

FROM php:7.2-cli

ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/

RUN install-php-extensions gd xdebug

Downloading the script on the fly with curl

FROM php:7.2-cli

RUN curl -sSLf \
        -o /usr/local/bin/install-php-extensions \
        https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions && \
    chmod +x /usr/local/bin/install-php-extensions && \
    install-php-extensions gd xdebug

Direct execution with curl

FROM php:8.2-cli

RUN curl -sSL https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions -o - | sh -s \
      gd xdebug

Copying the script from a Docker image

FROM php:7.2-cli

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

RUN install-php-extensions gd xdebug

Warning: by using this method you may use an outdated version of the mlocati/php-extension-installer image. You may want to run docker pull mlocati/php-extension-installer in order to use an up-to-date version.

Using the script of a Docker image

RUN  --mount=type=bind,from=mlocati/php-extension-installer:latest,source=/usr/bin/install-php-extensions,target=/usr/local/bin/install-php-extensions \
      install-php-extensions gd xdebug

Warning: by using this method you may use an outdated version of the mlocati/php-extension-installer image. You may want to run docker pull mlocati/php-extension-installer in order to use an up-to-date version.

Install the most recent xdebug 2.x version (for example 2.9.8)

install-php-extensions xdebug-^2

Install the most recent xdebug 2.8.x version (for example 2.8.1)

install-php-extensions xdebug-^2.8


Please remark that with the syntax above you'll get the vary latest compatible version, which may be unstable. In order to install the most recent *stable* version, you can append `@stable`:

```sh
# Install the most recent STABLE xdebug 3.x version (for example 3.2.2)
install-php-extensions xdebug-^3@stable

(valid suffixes are: @snapshot, @devel, @alpha, @beta, and @stable)

Pre-release versions extensions available on PECL can be setup by suffixing the extension’s name with its state i.e. alpha, beta, rc, preview, devel or snapshot. For example:

install-php-extensions xdebug-beta

TIP: When the latest version available on PECL is not stable, and you want to keep the last stable version, force it by suffixing the extension’s name with the stable state. For example:

install-php-extensions mongodb-stable

Installing an extension from its source code

You can also install PHP extensions from source code (provided that it comes with a package.xml or a package2.xml file).

Accepted formats are:

  • A short version for repositories hosted on GitHub.
    For example, for the php-memcached-dev/php-memcached GitHub repository, you can simply write:

    # Install from a specific commit (full commit SHA-1)
    install-php-extensions php-memcached-dev/php-memcached@8f106564e6bb005ca6100b12ccc89000daafa9d8
    # Install from a specific commit (short commit SHA-1)
    install-php-extensions php-memcached-dev/php-memcached@8f106564e6bb
    # Install from tag v3.2.0RC2
    install-php-extensions php-memcached-dev/php-memcached@v3.2.0RC2
    install-php-extensions php-memcached-dev/php-memcached@refs/tags/v3.2.0RC2
    # Install from branch master
    install-php-extensions php-memcached-dev/php-memcached@master
    install-php-extensions php-memcached-dev/php-memcached@refs/heads/master
    
  • An URL providing an archive containing the source code.
    Examples:

    # tgz archive for commit 8f106564e6bb005ca6100b12ccc89000daafa9d8
    install-php-extensions https://codeload.github.com/php-memcached-dev/php-memcached/tar.gz/8f106564e6bb005ca6100b12ccc89000daafa9d8
    # tgz archive for tag v3.1.5
    install-php-extensions https://codeload.github.com/php-memcached-dev/php-memcached/tar.gz/refs/tags/v3.1.5
    # tgz archive for branch master
    install-php-extensions https://codeload.github.com/php-memcached-dev/php-memcached/tar.gz/refs/heads/master
    
  • The absolute path of a local directory.
    Examples: “`sh

    Download the source code

    curl -o /tmp/source.tgz https://codeload.github.com/php-memcached-dev/php-memcached/tar.gz/refs/tags/v3.1.5 tar xzf /tmp/source.tgz -C /tmp install-php-extensions /tmp/php-memcached-3.1.5

Installing composer

You can also install composer, and you also can specify a major version of it, or a full version.

Examples:

# Install the latest version
install-php-extensions @composer
# Install the latest 1.x version
install-php-extensions @composer-1
# Install a specific version
install-php-extensions @composer-2.0.2

Issue with Let’s Encrypt certificates

The root CA certificate of Let’s Encrypt changes (more details here).
That breaks old linux distributions, namely:

  • Debian Jessie (8)
  • Debian Stretch (9)
  • Alpine Linux 3.7
  • Alpine Linux 3.8

This script can fix this issue: simply pass @fix_letsencrypt as an argument:

install-php-extensions @fix_letsencrypt

Special requirements

Some extensions have special requirements:

Extension Requirements
cassandra • Not available in jessie docker images
• Not available in stretch docker images
• Not available in buster docker images
• Not available in bullseye docker images
• Not available in bookworm docker images
ddtrace Not available in jessie docker images
ecma_intl • Not available in buster docker images
• Not available in bullseye docker images
geos • Not available in alpine3.9 docker images
• Not available in alpine3.10 docker images
lz4 Not available in jessie docker images
memprof • Not available in alpine3.9 docker images
• Not available in alpine3.10 docker images
• Not available in alpine3.11 docker images
• Not available in alpine3.12 docker images
• Not available in alpine3.13 docker images
• Not available in alpine3.14 docker images
• Not available in alpine3.15 docker images
parallel Requires images with PHP compiled with thread-safety enabled (zts)
parle Not available in jessie docker images
pdo_sqlsrv Not available in ARM architectures
pthreads Requires images with PHP compiled with thread-safety enabled (zts)
saxon Not available in alpine docker images
simdjson • Not available in jessie docker images
• Not available in stretch docker images
sodium Not available in jessie docker images
sqlsrv • Not available in 7.1-alpine3.9 docker images
• Not available in 7.1-alpine3.10 docker images
• Not available in ARM architectures
vips • Not available in alpine3.9 docker images
• Not available in jessie docker images
wikidiff2 • Not available in jessie docker images
• Not available in stretch docker images

How do I know which Linux distribution I am using?

You can run this command:

cat /etc/os-release

For example:

  • for Debian 11 (Bullseye) you’ll see:
    
    PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
    
  • for Alpine Linux 3.14 you’ll see:
    
    PRETTY_NAME="Alpine Linux v3.14"
    

Tests

When submitting a pull request, a GitHub Action is executed to check if affected PHP extensions actually work (see below).

Furthermore, we also check that new versions of extensions in the PECL repository will still work. This is done on a scheduled basis with another GitHub Action.
In case of failure, a message is sent to a Telegram Channel.
Feel free to subscribe to it to receive failure notifications.

How to contribute

Formatting code

Before submitting any pull request, you should execute the lint script in the scripts directory (or lint.bat on Windows).

If you don’t do that, and if there’s a coding style error, you’ll see that the Check shell coding style and/or the Check PHP coding style GitHub Actions will fail.

The error will be something like this:

--- filename.orig
+++ filename
@@ -line number,7 +line number,7 @@
     good line of code #1
     good line of code #2
     good line of code #3
-    the original line with a wrong coding style
+    the line wrong coding style that has been corrected
     good line of code #4
     good line of code #5
     good line of code #6

So, you should fix highlighted line (the one(s) at line number) by replacing what you see after the - with what you see after the +

For the maintainers

See the MAINTAINERS.md file.

Do you want to really say thank you?

You can offer me a monthly coffee or a one-time coffee :wink:


Articles

  • coming soon...