Ubuntu: How to build PHP from source code


Example we use Ubuntu 22.04, and we want to build PHP 7.4

Step 1: download source code:

git clone https://github.com/php/php-src.git --depth 1 --branch php-7.4.8

result:

root@tutorialspots ~ # git clone https://github.com/php/php-src.git --depth 1 --branch php-7.4.8
Cloning into 'php-src'...
remote: Enumerating objects: 19090, done.
remote: Counting objects: 100% (19090/19090), done.
remote: Compressing objects: 100% (16962/16962), done.
remote: Total 19090 (delta 2679), reused 13701 (delta 1956), pack-reused 0
Receiving objects: 100% (19090/19090), 17.77 MiB | 11.15 MiB/s, done.
Resolving deltas: 100% (2679/2679), done.
Note: switching to 'da4a40646aa897a5ba49edd2d64121ba34f1aeb9'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

Step 2: change dir to php-src

cd php-src

Step 3: install requirements:

apt install build-essential autoconf automake libtool
apt install libbison-dev
apt install re2c
apt install libxml2-dev
apt install libsqlite3-dev
apt install zlib1g-dev

Step 4: Build

./buildconf --force
./configure --with-mysqli
make
make install

result:

...
checking whether to include debugging symbols... no
checking whether to dlopen extensions with RTLD_NOW instead of RTLD_LAZY... no
checking layout of installed files... PHP
checking path to configuration file... DEFAULT
checking where to scan for configuration files...
checking whether to enable PHP's own SIGCHLD handler... no
checking whether to explicitly link against libgcc... no
checking whether to enable short tags by default... yes
checking whether to enable dmalloc... no
checking whether to enable IPv6 support... yes
checking whether to enable DTrace support... no
checking how big to make fd sets... using system default

Configuring extensions
checking for io.h... no
checking for strtoll... yes
checking for atoll... yes
checking whether to build with LIBXML support... yes
checking for libxml-2.0 >= 2.7.6... yes
checking for OpenSSL support... no
checking for Kerberos support... no
checking whether to use system default cipher list instead of hardcoded value... no
checking for PCRE library to use... bundled
checking whether to enable PCRE JIT functionality... yes
checking whether to enable the SQLite3 extension... yes
checking for sqlite3 > 3.7.4... yes
checking for sqlite3_errstr in -lsqlite3... yes
checking for sqlite3_load_extension in -lsqlite3... yes
checking for ZLIB support... no
checking whether to enable bc style precision math functions... no
checking for BZip2 support... no
checking whether to enable calendar conversion support... no
checking whether to enable ctype functions... yes
checking for cURL support... no
checking for QDBM support... no
checking for GDBM support... no
checking for NDBM support... no
checking for TCADB support... no
checking for LMDB support... no
checking for Berkeley DB4 support... no
checking for Berkeley DB3 support... no
checking for Berkeley DB2 support... no
checking for DB1 support... no
checking for DBM support... no
checking for CDB support... no
checking for INI File support... no
checking for FlatFile support... no
checking whether to enable DBA interface... no
checking whether to enable DOM support... yes
checking for libxml-2.0 >= 2.7.6... yes
checking whether to build with Enchant support... no
checking whether to enable EXIF (metadata from images) support... no
checking for FFI support... no
checking for fileinfo support... yes
checking for strcasestr... yes
checking for utimes... yes
checking for strndup... yes
checking whether to enable input filter support... yes
checking whether to enable FTP support... no
checking whether to explicitly enable FTP SSL support... no
checking for GD support... no
checking for external libgd... no
checking for libwebp... no
checking for libjpeg... no
checking for libXpm... no
checking for FreeType 2... no
checking whether to enable JIS-mapped Japanese font support in GD... no
checking for GNU gettext support... no
checking for GNU MP support... no
checking for mhash support... no
checking size of long... (cached) 8
checking if we're at 64-bit platform... yes
checking for iconv support... yes
checking for iconv... yes
checking if iconv is glibc's... yes
checking if iconv supports errno... yes
checking if iconv supports //IGNORE... no
checking if your cpp allows macro usage in include lines... yes
checking for IMAP support... no
checking for IMAP Kerberos support... no
checking for IMAP SSL support... no
checking whether to enable internationalization support... no
checking whether to enable JavaScript Object Serialization support... yes
checking for LDAP support... no
checking whether to build with LDAP Cyrus SASL support... no
checking whether to enable multibyte string support... no
checking whether to enable multibyte regex support (requires oniguruma)... yes
checking for MySQLi support... yes
checking for specified location of the MySQL UNIX socket... no
checking for MySQL UNIX socket location... no
checking for Oracle Database OCI8 support... no
checking for Adabas support... no
checking for SAP DB support... no
checking for Solid support... no
checking for IBM DB2 support... no
checking for Empress support... no
checking for Empress local access support... no
checking for a custom ODBC support... no
checking whether to build with iODBC support... no
checking for Easysoft ODBC-ODBC Bridge support... no
checking whether to build with unixODBC support... no
checking for DBMaker support... no
checking for any ODBC driver support... no
checking whether to enable Zend OPcache support... yes
checking whether to enable copying PHP CODE pages into HUGE PAGES... yes
checking for mprotect... yes
checking for sysvipc shared memory support... yes
checking for mmap() using MAP_ANON shared memory support... yes
checking for shm_open in -lrt... yes
checking for mmap() using shm_open() shared memory support... yes
checking for shm_unlink in -lrt... yes
checking whether to enable pcntl support... no
checking whether to enable PDO support... yes
checking for PDO_DBLIB support via FreeTDS... no
checking for Firebird support for PDO... no
checking for MySQL support for PDO... no
checking for the location of libz... no
checking Oracle OCI support for PDO... no
checking for ODBC v3 support for PDO... no
checking for PostgreSQL support for PDO... no
checking for sqlite 3 support for PDO... yes
checking for PDO includes... /root/php-src/ext
checking for sqlite3 > 3.7.4... yes
checking for sqlite3_close_v2 in -lsqlite3... yes
checking for sqlite3_column_table_name in -lsqlite3... yes
checking for PostgreSQL support... no
checking for phar archive support... yes
checking for phar openssl support... no
checking whether to enable POSIX-like functions... yes
checking for sys/mkdev.h... no
checking for sys/sysmacros.h... yes
checking for seteuid... yes
checking for setegid... yes
checking for setsid... yes
checking for getsid... yes
checking for getpgid... yes
checking for ctermid... yes
checking for mkfifo... yes
checking for mknod... yes
checking for setrlimit... yes
checking for getrlimit... yes
checking for getgroups... yes
checking for makedev... no
checking for initgroups... yes
checking for getgrgid_r... yes
checking for working ttyname_r() implementation... yes
checking for utsname.domainname... yes
checking for PSPELL support... no
checking for libedit readline replacement... no
checking for readline support... no
checking whether to enable PHP sessions... yes
checking for mm support... no
checking whether pwrite works... yes
checking whether pread works... yes
checking whether to enable shmop support... no
checking whether to enable SimpleXML support... yes
checking for libxml-2.0 >= 2.7.6... yes
checking for SNMP support... no
checking whether to enable SOAP support... no
checking whether to enable sockets support... no
checking for sodium support... no
checking whether flush should be called explicitly after a buffered io... no
checking for crypt in -lcrypt... yes
checking for standard DES crypt... yes
checking for extended DES crypt... yes
checking for MD5 crypt... yes
checking for Blowfish crypt... yes
checking for SHA512 crypt... yes
checking for SHA256 crypt... yes
checking whether the compiler supports __alignof__... yes
checking whether the compiler supports aligned attribute... yes
checking for asinh... yes
checking for acosh... yes
checking for atanh... yes
checking for log1p... yes
checking for hypot... yes
checking for working POSIX fnmatch... yes
checking for fork... yes
checking if your OS can spawn processes with inherited handles... yes
checking for res_nsearch... yes
checking for res_ndestroy... no
checking for __res_ndestroy... no
checking for res_ndestroy in -lresolv... no
checking for __res_ndestroy in -lresolv... no
checking for res_ndestroy in -lbind... no
checking for __res_ndestroy in -lbind... no
checking for res_ndestroy in -lsocket... no
checking for __res_ndestroy in -lsocket... no
checking for dns_search... no
checking for __dns_search... no
checking for dns_search in -lresolv... no
checking for __dns_search in -lresolv... no
checking for dns_search in -lbind... no
checking for __dns_search in -lbind... no
checking for dns_search in -lsocket... no
checking for __dns_search in -lsocket... no
checking for dn_expand... yes
checking for dn_skipname... yes
checking for res_search... yes
checking whether strptime() declaration fails... yes
checking for wchar.h... (cached) yes
checking for mblen... yes
checking for mbstate_t... yes
checking for atomic.h... no
checking whether arc4random_buf is declared... no
checking for Argon2 support... no
checking for net/if.h... yes
checking for usable getifaddrs... yes
checking whether to enable System V IPC support... no
checking whether to enable System V semaphore support... no
checking whether to enable System V shared memory support... no
checking for TIDY support... no
checking whether to enable tokenizer support... yes
checking whether to enable XML support... yes
checking whether to build with expat support... no
checking for libxml-2.0 >= 2.7.6... yes
checking whether to enable XMLReader support... yes
checking for libxml-2.0 >= 2.7.6... yes
checking whether to build with XMLRPC-EPI support... no
checking whether to build with expat support... no
checking iconv dir for XMLRPC-EPI... no
checking whether to enable XMLWriter support... yes
checking for libxml-2.0 >= 2.7.6... yes
checking whether to build with XSL support... no
checking whether to enable zend-test extension... no
checking for zip archive read/write support... no
checking whether to enable mysqlnd... no
checking whether to disable compressed protocol support in mysqlnd... yes
checking for zlib... yes
checking linker support for -zcommon-page-size=2097152... yes

Configuring PEAR
checking whether to install PEAR... no

Configuring Zend
checking for cpuid.h... yes
checking for getpid... yes
checking for kill... yes
checking for finite... yes
checking for sigsetjmp... no
checking whether isfinite is declared... yes
checking whether isnan is declared... yes
checking whether isinf is declared... yes
checking for usable _FPU_SETCW... yes
checking for usable fpsetprec... no
checking for usable _controlfp... no
checking for usable _controlfp_s... no
checking whether FPU control word can be manipulated by inline assembler... yes
checking whether double cast to long preserves least significant bits... no
checking for dlfcn.h... (cached) yes
checking whether dlsym() requires a leading underscore in symbol names... no
checking whether to enable thread-safety... no
checking whether to enable inline optimization for GCC... yes
checking whether to enable Zend debugging... no
checking for inline... inline
checking target system is Darwin... no
checking for MM alignment and log values... done
checking for mremap... yes
checking for sigaction... yes
checking whether to enable zend signal handling... yes

Configuring TSRM

Configuring libtool
checking for a sed that does not truncate output... /usr/bin/sed
checking for ld used by cc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for /usr/bin/ld option to reload object files... -r
checking for BSD-compatible nm... /usr/bin/nm -B
checking how to recognize dependent libraries... pass_all
checking the maximum length of command line arguments... 1572864
checking command to parse /usr/bin/nm -B output from cc object... ok
checking for objdir... .libs
checking for ar... ar
checking for ranlib... ranlib
checking for strip... strip
checking if cc supports -fno-rtti -fno-exceptions... no
checking for cc option to produce PIC... -fPIC
checking if cc PIC flag -fPIC works... yes
checking if cc static flag -static works... yes
checking if cc supports -c -o file.o... yes
checking whether the cc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... yes

creating libtool
appending configuration tag "CXX" to libtool

Generating files
configure: patching main/php_config.h.in
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/phpdbg/phpdbg.1
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE. By continuing this installation  |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.
...
Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
clicommand.inc
invertedregexiterator.inc
pharcommand.inc
directorytreeiterator.inc
directorygraphiterator.inc
phar.inc

Build complete.
Don't forget to run 'make test'.
...
Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
clicommand.inc
invertedregexiterator.inc
pharcommand.inc
directorytreeiterator.inc
directorygraphiterator.inc
phar.inc
Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20190902/
Installing PHP CLI binary:        /usr/local/bin/
Installing PHP CLI man page:      /usr/local/php/man/man1/
Installing phpdbg binary:         /usr/local/bin/
Installing phpdbg man page:       /usr/local/php/man/man1/
Installing PHP CGI binary:        /usr/local/bin/
Installing PHP CGI man page:      /usr/local/php/man/man1/
Installing build environment:     /usr/local/lib/php/build/
Installing header files:          /usr/local/include/php/
Installing helper programs:       /usr/local/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php/man/man1/
  page: phpize.1
  page: php-config.1
/root/php-src/build/shtool install -c ext/phar/phar.phar /usr/local/bin/phar.phar
ln -s -f phar.phar /usr/local/bin/phar
Installing PDO headers:           /usr/local/include/php/ext/pdo/

done ! check :

root@tutorialspots ~/php-src # sapi/cli/php -v
PHP 7.4.8 (cli) (built: Jun 20 2024 05:17:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

Now, you can copy some file to your directory like: /usr/local/php/7.4/

Leave a Reply