Recently, I wanted to improve our CI build pipelines with some custom-built Docker images for our PHP projects. To make sure I can reuse the image in as many of our projects as possible, I wanted to compile and install as many PHP extensions as possible.
Installing PHP extensions from scratch can be a bit tedious as you need to figure out which libraries are required for compiling the PHP extensions. Luckily, there exists a tool to help automate this for you if you are using Alpine or Debian/Ubuntu as base image.
In your Dockerfile, install the script via the following approach:
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
Now, you can invoke the
install-php-extensions script to install all sorts of 3rd party PHP extensions needed for your
specific setup. For example, to install xdebug, run the following command in your Dockerfile:
RUN install-php-extensions xdebug
If you need to install a specific module version, you can specify the exact version number as well:
RUN install-php-extensions xdebug-2.9.7
Or, in case you want to install the latest patch release of the 2.9 release, install xdebug like this:
RUN install-php-extensions xdebug-^2.9
Additionally, it even allows you to install Composer which also saves a few lines of code, especially if you need a specific version of Composer running in your container: