PHP: Install protobuf on windows

Google's Protocol Buffer's project provides both a PHP implementation and a PHP extension for processing data. The PHP implementation provides a workable solution anywhere that PHP can be used, but is not as efficient. The extension requires compiling the C source and installing the extension, but provides much better performance in data parsing. I noticed this difference in performance while working on parsing some Open Street Map dump files.

I do my primary development work on windows, and wanted to get the extension installed for the performance boost. There does not appear to be an official pre-compiled DLL available anywhere for this extension, which means I needed to compile it myself. After a few hours of figuring out how to compile PHP and it's extensions on windows, I was able to get the DLL built and loaded. I'm documenting the process here for anyone else that needs this (or any other PECL extension) that is not officially provided.

This guide is for PHP 8.x, other versions may require different steps or requirements.

Getting started

The PHP documentation has a Wiki page documenting how to build PHP and extension on windows. The documentation is pretty straight forward, but I found some of the items seemed to be a little out of order to me, so here I will attempt to provide a more step-by-step process.

First, obtain the software and sources required to perform the compilation.

  1. The PHP Source code for the version you are targeting. For this write-up, that will be 8.2.11.
  2. Visual studio community 2019. New versions are not currently supported by the PHP build system.
  3. The PHP binary tools SDK. Version 2.2.0 as of this writing.
  4. The protobuf PECL extension. Version 3.24.3 as of this writing.
  5. Any other PECL extensions you'd like to build.

I opted to perform this task in a clean Virtual Machine environment rather than in my daily OS. If you'd like to do this as well, you'll also need

  1. Virtual Box (or other virtual machine software)
  2. Windows installation ISO

Create a new virtual machine and install windows on it. If using virtual box, I recommend installing the Guest additions and setting up a shared folder for the virtual machine to easily transfer files between the virtual machine and the host.

Installing Visual Studio

Launch the visual studio installer. Continue through the installer until you reach the prompt for which development tools you would like to install. Select the option for Desktop development with C++ and continue.

Screenshot showing ".NET desktop development" and "Desktop development with C++" selected

When the installation is complete, close visual studio and it's installer.

Set up the PHP build environment

  1. Extract the PHP binary tools SDK to C:\php-sdk.
  2. Open the x64 Native tools Command Prompt provided by visual studio.
  3. In the command prompt, type cd c:\php-sdk to enter the SDK directory
  4. Type phpsdk-vs16-x64 to set up the SDK environment and enter the SDK shell.
  5. Type bin\phpsdk_buildtree phpdev to create the necessary directory structure.
  6. Extract the PHP source code into the newly created folder c:\php-sdk\phpdev\vs16\x64. The source should be in its own folder, for example c:\php-sdk\phpdev\vs16\x64\php-8.2.11
  7. In the command prompt, enter the PHP source directory. cd php-8.2.11
  8. Type mkdir pecl to create the pecl directory where the extensions will be placed.
  9. Extract the protobuf pecl source into the newly created pecl directory. The source should be in its own folder, e.g. protobuf-3.24.3.

Building the extension

  1. In your command prompt, ensure you are in the PHP source directory. cd c:\php-sdk\phpdev\vs16\x64\php-8.2.11.
  2. Type buildconf to generate the configuration files.
  3. Type configure --help to view all the configuration options available to you. You should see the --enable-protobuf option in the list.
  4. Type configure --disable-all --disable-zts --enable-cli --with-all-shared --enable-protobuf to configure the build PHP with the protobuf extension as a shared library. If you have other PECL extensions, add their --enable flag as well. If you need a thread safe build, omit the --disable-zts flag.
  5. If the configuration is successful, you should see a summary of the configuration, including a list of the extensions to be built. Ensure that protobuf is in that list and is marked as shared.

    Screenshot showing successful configuration

  6. Type nmake to build PHP and the extensions.

The nmake build process will take a few minutes to complete, and may show some warning messages in the process. When the build is complete, you will see a message SAPI sapi\cli build complete.

Screenshot showing successful build.

The build output files wll be located in the c:\php-sdk\phpdev\vs16\x64\php-8.2.11\x64\Release directory. Find the php_protobuf.dll file in this folder. Copy the php_protobuf.dll file to your PHP installation's ext directory and edit the php.ini file to load the extension.

Download

While I recommend building your own DLL, for those who cannot, you may download the DLLs I built. These DLLs are for the VC16 x64 Non Thread Safe versions of PHP.

Support free Wi-Fi

openwireless.org

Support free maps

openstreetmap.org