Skip to content

Self-contained executable#

To create a self-contained PHP executable, start with a console application, e.g.:

<?php
echo "Hello world!";
and a corresponding project file indicating that the output should be exe:
<Project Sdk="Peachpie.NET.Sdk/0.9.42">
  <PropertyGroup>
    <OutputType>exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <StartupObject>main.php</StartupObject>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="**/*.php" />
  </ItemGroup>
</Project>
Now compile your project into binaries using the following command: dotnet publish -c release

This will publish the resulting binaries into the /bin/release/netcoreapp2.1/publish folder.

As the final step, select an operating system to target, e.g.

dotnet publish -c release -r win-x64 for 64-bit Windows, or

dotnet publish -c release -r linux-x64 for Linux.

This creates a self-contained executable PHP application targeting the operating system of your choice.

Size considerations#

A few tips on how to minimize the size of the resulting application:

  • On Linux, you can get rid of culture-specific functionalities and keep just the “Invariant” culture features (cuts about 26MB of data). More detail here.
  • Use dotnet-warp to merge DLL files and remove unnecessary IL and methods. More info can be found here.

Note

Rename the project file so that it has the .csproj extension, it will work just fine.

Resources#