This is an old revision of the document!

Build your own PHP on Windows

This tutorial concerns the new PHP SDK and PHP 7.2 and later, while the described PHP SDK is compatible with at least PHP 7.0. To build PHP prior to 7.0, or if you experience issues building PHP lower than 7.2 with the new PHP SDK, please refer to older documentation.

Before you Begin

Building PHP on Windows will require three things

  1. A properly set up build environment, including the compiler and the PHP SDK
  2. Prebuilt libraries and headers for third party libraries that PHP uses in the correct location
  3. The PHP source



PHP officially supports building with Microsoft's Visual C++ compilers. MinGW and Cygwin are NOT supported. ICC and clang can be used for experimental purposes. Though, be aware, the production quality binaries are guaranteed only by using Visual C++. The freely available Community VC++ editions are fully supported and can be used. For more information and how to get the compiler see the supported versions. The following VC++ versions are supported:

  • Visual C++ 14.0 (Visual Studio 2015) for PHP 7.0 or PHP 7.1.
  • Visual C++ 15.0 (Visual Studio 2017) for PHP 7.2 or PHP 7.3.


  • Fetch the latest stable SDK tag from
    • The new PHP SDK is required, when building PHP 7.2+
    • Read the PHP SDK specific notes on the Github repository page
  • If compiling PHP 7.0+:
    • Install Visual Studio 2015
  • If compiling PHP 7.2+:
    • Install Visual Studio 2017
  • Optional tools
    • Git
    • Cppcheck
    • Clang

Command prompt

The PHP SDK 2.0.0+ provides starter scripts, that automatically setup the compiler environment. The starter scripts also provide functionality to inject custom setup pieces. To create a customized setup script, put your code into the file phpsdk-local.bat into the PHP SDK root dir. The following variables are automatically available within the PHP SDK shell

  • PHP_SDK_ARCH - the target build architecture used for environment setup
  • PHP_SDK_OS_ARCH - the operating system architecture
  • PHP_SDK_PHP_CMD - integrated PHP binary wrapper script
  • PHP_SDK_ROOT_PATH - the path to the PHP SDK root directory
  • PHP_SDK_VC - Visual C++ version used for the environment
  • several other less relevant vars are available, that can be viewed by the set command

The full list of the tools included with the PHP SDK can be found on the repository readme page.

Each version of Visual Studio (or Windows SDK) provides a command prompt environment for compiling (available from the Start Menu group). It is usable, for example, if you want to omit the starter scripts usage or want to test a pre release Visual Studio version.

  • If compiling PHP 7.0+ open either the “VS2015 x64 Native Tools Command Prompt” or the “VS2015 x86 Native Tools Command Prompt”.

All commands in the rest of this document should be run in the appropriate command prompt.

Download prerequisites

  1. Get the PHP source, there are two alternatives:
  2. Get the binary tools:
  3. Get the libraries on which PHP depends:
    • use the PHP SDK tools to fetch the suitable dependencies automatically by calling phpsdk_deps -u
    • alternatively, download the packaged deps from choosing the matching VC++ version and architecture directory. Note the .7z packages are not provided anymore for PHP 7.2 and up.
    • Note: This should include the libraries needed to build most the core extensions. However, some other extensions may need additional libraries, header files and helper apps. See libs, fetch the version you need and extract the archive into the deps directory (see below). Another set of libraries used for the PECL extension builds is available under PECL deps directory. If required, they need to be fetched manually.

Setup the build directory

This should be done only once before building PHP. The reference binary SDK directory used here is c:\php-sdk, an arbitrary location can be used depending on concrete circumstances. Consequently, we use the path C:\php-sdk\phpdev\vc14\x64\php-7.1.0-src as a reference path for the Visual C++ 64-bit build of PHP 7.1.0.

  1. Setup the PHP SDK
    1. Create the build directory c:\php-sdk
    2. Unpack the binary tools archive into this directory, it should contain three sub-directories: bin, lib and msys2
  2. Open the command prompt and enter the build directory:
    cd c:\php-sdk\
  3. Invoke the starter script, for example for Visual Studio 2015 64-bit build, invoke phpsdk-vc14-x64.bat
  4. Run the phpsdk_buildtree batch script which will create the desired directory structure:
    phpsdk_buildtree phpdev
  5. The phpsdk_buildtree script will create the path according to the currently VC++ version used and switch into the newly created directory
  6. Extract the PHP source code to C:\php-sdk\phpdev\vc##\x##, where:
    • vc## is the compiler version you are using (eq vc14)
    • x## is your architecture (x86 or x64)
    • For example: C:\php-sdk\phpdev\vc14\x64\php-7.1.0-src
  7. In the same directory where you extracted the PHP source there is a deps directory. Do one of the following
    • switch into the php source directory and run phpsdk_deps -u
    • If you downloaded the dependencies manually in the previous step, extract them into deps


Note, that no cross builds are currently supported by the PHP SDK starter scripts.

  1. Invoke the starter script to automatically setup the environment for the desired build config, fe
  2. Change directory to the location of your PHP source code, e.g.
    cd C:\php-sdk\phpdev\vc14\x64\php-7.1.0-src
  3. Run:
  4. To get an overview of the compiling flags:
    configure --help
  5. Create your configure command:
    configure --disable-all --enable-cli --enable-$remains
  6. To build PHP, run:
  7. If you want the resulting PHP builds and extensions to be zipped, after 'nmake' also run:
    nmake snap
  8. The compiled PHP is now under “C:\php-sdk\phpdev\vcXX\x##\php-source-directory\Release_TS”. If you ran 'nmake snap' the zip file will also be here.
    • If you compiled with “--disable-zts” the compiled PHP will be under “C:\php-sdk\phpdev\vcXX\x##\php-source-directory\Release”

Recompile after you have done some changes

  1. Clean up old compiled binaries
    nmake clean
  2. If you need to update the 'configure' script
    buildconf --force
  3. Create your makefile: see release
    configure --disable-all --enable-cli --enable-$remains
  4. Compile

Building PECL extensions

Many PECL extensions already have prebuilt DLLs which can be downloaded from the extension page on the PECL web site. For example, on the APCu extension page, there are *DLL* links next to the release archive link. They lead you to a page where you can download the compiled extension for your desired PHP version and architecture.

To build a PECL extension (for example APCu):

  1. Open the extension's page on PECL ( APCu)
  2. Download the extension source either by:
    • downloading a source archive
    • fetching the source from the extension's repository (link can be found under *Browse Source*)
  3. Create a directory named pecl on the same level as your PHP source directory, e.g. C:\php-sdk\phpdev\vc14\x64\pecl
  4. Extract or clone the extension source code to the pecl directory
    • if cloning, clone to a subdirectory, e.g. C:\php-sdk\phpdev\vc14\x64\pecl\apcu
    • source code archive should already contain a subdirectory named e.g. apcu-4.0.7
  5. Invoke the starter script to automatically setup the environment for the desired build config, fe
  6. Enter your PHP source directory
  7. Rebuild the configure script by running:
  8. Executing configure --help should now contain an option to enable APCu
    --enable-apcu    Whether to enable APCu support
  9. Configure and build:
    configure --disable-all --enable-cli --enable-apcu
  10. Test the binary with a php -m command, to make sure APCu is loaded

At this point, your compiled extension will be located in the release directory.


  1. For possible PHP SDK issues, please consult the PHP SDK readme
  2. As of PHP 7.3 the switch --enable-native-intrinsics is available. The resulting binary will then require a processor with the corresponding feature set available. For example, if you set --enable-native-intrinsics=avx2, be sure the target machine has processor with AVX2 support.
internals/windows/stepbystepbuild_sdk_2.1546109819.txt.gz · Last modified: 2018/12/29 18:56 by ab