How to compile PHP from source on Fedora/RHEL/etc
Compiling PHP from source on Fedora and Enterprise Linux (RHEL, CentOS, etc.) is quite straight forward, and is on par with how PHP is compiled on Ubuntu/Debian.
Compiled PHP binaries are readily available for Fedora and Fedora-alike operating systems in their default software repositories. For PHP setups that do not require compile-step customizations, it might be easier to install PHP from the software repositories without compiling PHP on the target server.
dnf install php
The PHP versions available at the default software repositories may not be the latest version. For example, Fedora 34 ships with PHP 7.4, and not PHP 8.0, which is the latest version.
Once the repository is added, PHP can be installed from the Remi repository:
sudo dnf module install php:remi-8.0
Compiling PHP from its source files entails fetching the PHP source code, installing the compilers and its tools on the server, configuring the build, and finally compiling it.
- The Git repository of PHP source code, when cloned, is around 450 MB. PHP source, and its required dependencies will require about 1 GB of empty disk space.
- The initial setup will also require downloading ~ 600 MB of data from archives.
sudo dnf install git make gcc gcc-c++ binutils glibc-devel autoconf libtool bison re2c automake libxml2-devel
The command above install the C compiler, and other tools required to compile PHP. This is similar to installing
build-essential package in Debian/Ubuntu systems.
gcc-c++are C/C++ compilers.
makeis a utility to direct the compilation scripts.
autoconfis used to generate the
configurescript that is used later in the compilation.
libtoolis a tool that helps to manage and locate shared libraries.
bisonis YACC-compatible parser generator
re2cis a tool that is used to generate the PHP's lexer.
automakeis used to create
binutilsis a collection of binaries for creating/modifying/extracting archives, displaying binary sizes, information, etc.
libxml2-develis one of the dependencies that PHP uses for its XML support. While it is possible to completely disable it, it is more common to compile PHP with XML support.
PHP source code is available at php/php-src repository on GitHub.
git clone https://github.com/php/php-src.git --branch=master
This clones the PHP source code to the current working directory, and checkout the
master branch. The
master branch contains the latest PHP source code.
To fetch a specific PHP version, specify its branch. For example, to checkout PHP 7.4.6:
git clone https://github.com/php/php-src.git --branch=PHP-7.4.6
php-src repository will be cloned to a
php-src directory in the current working directory. Once done,
cd into the new directory:
buildconf command creates a configuration script in
php-src/configure, which can then be used to configure the build.
On subsequent compilations, it might be necessary to rebuild the
./configure script by running the
buildconf command with the
--force option. This will ensure the
./configure script is up to date.
./configure script is used to enable/disable build configurations. This includes enabling/disabling certain PHP modules, enabling specific features of PHP (such as image type toggles for the GD extension, toggle IPv6 support, etc).
To view all the configuration flags and options available, run
The flags shown in the
./configure --help follow a pattern of
--with-XYZ. It accepts multiple flags, and is often a very long one in most PHP setups primed for production use.
./configure --enable-ftp --with-openssl --disable-cgi
If the flag is passed, the extension/SAPI/feature with name
XXX will be enabled.
For extensions, using
--enable-XXX=shared pattern makes the extension compiled to a separate file so it can be enabled/disabled from PHP INI files.
For example, running
./configure --enable-ftp enables the FTP extension; Running
./configure --enable-ftp=shared enables the extension to be compiled as a shared extension; the extension will be compiled to a separate
.so file, so it can be enabled/disabled using a PHP configuration file.
Not all extensions support compiling to a shared extension.
In addition to extensions, the
--enable-XYZ options are available for Server APIs (SAPIs) and specific features as well. Notably, the
--enable-zts enables thread-safety feature in the build.
This option is similar to
--enable-XYZ pattern that they enable various PHP extensions and features. Note these extensions/features often require additional dependencies.
For example, the OpenSSL extension, enabled with
./configure --with-openssl depends on the development files of the OpenSSL library. In Fedora and alike, they can be easily installed with the
openssl-devel package. The
-devel suffix to the package name indicates that it is a development package. To fulfill the requirements for OpenSSL extension, install
sudo dnf install openssl-devel
The opposite of
--enable-XYZ flags. The presence of this flag means PHP is configured to include that extension/feature/SAPI by default, unless the
--disable-XYZ option is passed.
--disable-all flag disables all extensions, which allows a clean slate for individual extensions to be enabled with
By default, PHP compiles with SQLite support built-in. Disabling the SQLite3 extensions makes it possible to compile PHP without having to install SQLite3 dependencies.
./configure --without-sqlite3 --without-pdo-sqlite
./configure command again and again with several options is cumbersome. When the
./configure script is run, it saves the command to a
./config.nice file, that executes the exact same command as before, and optionally append additional options.
After the first
./configure run has completed, using the
./config.nice file helps to avoid typing the same
./configure options again and again.
./config.nice script has completed, it is now time to run the compiler.
Depending on the CPU cores and threads available, this can take anywhere in the range from 2 minutes to 15-20 minutes.
make -j $(nproc)
make command is used to run the compilation using the C compiler. It accepts a
-j parameter, that can be used to configure parallel processing. Output of the
nproc command, which returns the number of available CPU threads in the system is then set for the
make -j parameter.
-j option is not present, it will use a single CPU thread by default. To set a specific number of threads, simply specify the number for the
Install/try it out
The compiled binaries will be available in the
./sapi directory. For example, to immediately run the PHP CLI, call the
Alternately, the compiled PHP version can be installed on the system, so other tools can easily use the
php binary in PATH.
sudo make install
Following are command-line arguments can be passed to the
./configure script to enable/disable/configure PHP extensions and features.
The following extensions are PHP core extensions, and cannot be disabled. Older PHP versions might have had a flag to toggle this extension, but they are not valid anymore for these extensions.
|Hash||Core extension since PHP 7.4|
|JSON||Core extension since PHP 8.0|
|SPL||Core extension since PHP 5.3|
The following extensions are enabled by default, but can be disabled if necessary. The
--disable-all flag also disables all of them.
Compiling additional extensions often require its dependencies in place. Here is a list of PHP extensions and their dependencies, and the
./configure flag to enable it.
The Dependencies column lists the package names in Ubuntu/Debian repositories. Install them using the package manager:
sudo dnf install <package-name>
- GD extensions Avif image support (
libavif-devel) is only supported on PHP 8.1 and later.
The entire list of commands above are shortened below:
sudo dnf install git make gcc gcc-c++ binutils glibc-devel autoconf libtool bison re2c automake libxml2-devel git clone https://github.com/php/php-src.git --branch=master cd php-src ./buildconf ./configure make -j $(nproc) sudo make install
cd php-src git pull --rebase ./buildconf --force ./config.nice make -j $(nproc) sudo make install
./config.nice is also available.