Compdigitec Labs

« | Home | »

Install bcompiler for PHP on Ubuntu Linux

By admin | August 10, 2008

The PHP bytecode compiler is a useful way to convert PHP to bytecode, which can then be converted to binaries. However, the Ubuntu packaging team has not yet made a binary package of bcompiler available. However, you can install this bytecode compiler yourself with these 9 easy to follow steps. A binary build is availible, but you should try to compile/install it yourself because the binary extension might not be suitable for your computer.

  1. Install the basic dependencies with sudo apt-get install automake m4 php5-dev
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    The following extra packages will be installed:
    autoconf automake1.4 autotools-dev libtool shtool
    Suggested packages:
    autoconf2.13 autobook autoconf-archive gnu-standards autoconf-doc gfortran fortran95-compiler libtool-doc
    Recommended packages:
    automaken libltdl3-dev
    The following NEW packages will be installed:
    autoconf automake automake1.4 autotools-dev libtool m4 php5-dev shtool
    0 upgraded, 8 newly installed, 0 to remove and 67 not upgraded.
    Need to get 2371kB of archives.
    After this operation, 9540kB of additional disk space will be used.
    Do you want to continue [Y/n]? y
    Get:1 http://us.archive.ubuntu.com hardy/main m4 1.4.10-1 [207kB]
    Get:2 http://us.archive.ubuntu.com hardy/main autoconf 2.61-4 [448kB]
    Get:3 http://us.archive.ubuntu.com hardy/main autotools-dev 20070725.1 [61.9kB]
    Get:4 http://us.archive.ubuntu.com hardy/main automake 1:1.10.1-2 [519kB]
    Get:5 http://us.archive.ubuntu.com hardy/main automake1.4 1:1.4-p6-12 [272kB]
    Get:6 http://us.archive.ubuntu.com hardy/main libtool 1.5.26-1ubuntu1 [340kB]
    Get:7 http://us.archive.ubuntu.com hardy/main shtool 2.0.7-1 [159kB]
    Get:8 http://us.archive.ubuntu.com hardy-updates/main php5-dev 5.2.4-2ubuntu5.3 [364kB]
    Fetched 2371kB in 32s (72.5kB/s)
    Selecting previously deselected package m4.
    (Reading database ... 123151 files and directories currently installed.)
    Unpacking m4 (from .../archives/m4_1.4.10-1_i386.deb) ...
    Selecting previously deselected package autoconf.
    Unpacking autoconf (from .../autoconf_2.61-4_all.deb) ...
    Selecting previously deselected package autotools-dev.
    Unpacking autotools-dev (from .../autotools-dev_20070725.1_all.deb) ...
    Selecting previously deselected package automake.
    Unpacking automake (from .../automake_1%3a1.10.1-2_all.deb) ...
    Selecting previously deselected package automake1.4.
    Unpacking automake1.4 (from .../automake1.4_1%3a1.4-p6-12_all.deb) ...
    Selecting previously deselected package libtool.
    Unpacking libtool (from .../libtool_1.5.26-1ubuntu1_i386.deb) ...
    Selecting previously deselected package shtool.
    Unpacking shtool (from .../shtool_2.0.7-1_all.deb) ...
    Selecting previously deselected package php5-dev.
    Unpacking php5-dev (from .../php5-dev_5.2.4-2ubuntu5.3_i386.deb) ...
    Setting up m4 (1.4.10-1) ...

    Setting up autoconf (2.61-4) …

    Setting up autotools-dev (20070725.1) …
    Setting up automake (1:1.10.1-2) …

    Setting up automake1.4 (1:1.4-p6-12) …

    Setting up libtool (1.5.26-1ubuntu1) …
    Setting up shtool (2.0.7-1) …
    Setting up php5-dev (5.2.4-2ubuntu5.3) …

  2. Then, install the secondary dependencies with sudo apt-get install re2c libbz2-dev:
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    The following NEW packages will be installed:
    libbz2-dev re2c
    0 upgraded, 2 newly installed, 0 to remove and 67 not upgraded.
    Need to get 219kB of archives.
    After this operation, 582kB of additional disk space will be used.
    Get:1 http://us.archive.ubuntu.com hardy/main libbz2-dev 1.0.4-2ubuntu4 [32.1kB]
    Get:2 http://us.archive.ubuntu.com hardy/main re2c 0.12.1-1build1 [187kB]
    Fetched 219kB in 1s (113kB/s)
    Selecting previously deselected package libbz2-dev.
    (Reading database ... 123871 files and directories currently installed.)
    Unpacking libbz2-dev (from .../libbz2-dev_1.0.4-2ubuntu4_i386.deb) ...
    Selecting previously deselected package re2c.
    Unpacking re2c (from .../re2c_0.12.1-1build1_i386.deb) ...
    Setting up libbz2-dev (1.0.4-2ubuntu4) ...
    Setting up re2c (0.12.1-1build1) ...
  3. Compile and install bcompiler with sudo pecl install bcompiler-0.8:
    downloading bcompiler-0.8.tgz ...
    Starting to download bcompiler-0.8.tgz (46,667 bytes)
    .............done: 46,667 bytes
    10 source files, building
    running: phpize
    Configuring for:
    PHP Api Version: 20041225
    Zend Module Api No: 20060613
    Zend Extension Api No: 220060519
    building in /var/tmp/pear-build-root/bcompiler-0.8
    running: /tmp/pear/cache/bcompiler-0.8/configure
    checking for grep that handles long lines and -e... /bin/grep
    checking for egrep... /bin/grep -E
    checking for a sed that does not truncate output... /bin/sed
    checking for gcc... gcc
    checking for C compiler default output file name... a.out
    checking whether the C compiler works... yes
    checking whether we are cross compiling... no
    checking for suffix of executables...
    checking for suffix of object files... o
    checking whether we are using the GNU C compiler... yes
    checking whether gcc accepts -g... yes
    checking for gcc option to accept ISO C89... none needed
    checking whether gcc and cc understand -c and -o together... yes
    checking for system library directory... lib
    checking if compiler supports -R... no
    checking if compiler supports -Wl,-rpath,... yes
    checking build system type... i686-pc-linux-gnu
    checking host system type... i686-pc-linux-gnu
    checking target system type... i686-pc-linux-gnu
    checking for PHP prefix... /usr
    checking for PHP includes... -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
    checking for PHP extension directory... /usr/lib/php5/20060613+lfs
    checking for PHP installed headers prefix... /usr/include/php5
    checking for re2c... re2c
    checking for re2c version... 0.12.1 (ok)
    checking for gawk... gawk
    checking whether to enable bcompiler support... yes, shared
    checking for BZip2 in default path... found in /usr
    checking for BZ2_bzerror in -lbz2... yes
    checking for ld used by gcc... /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 whether ln -s works... yes
    checking how to recognize dependent libraries... pass_all
    checking how to run the C preprocessor... gcc -E
    checking for ANSI C header files... yes
    checking for sys/types.h... yes
    checking for sys/stat.h... yes
    checking for stdlib.h... yes
    checking for string.h... yes
    checking for memory.h... yes
    checking for strings.h... yes
    checking for inttypes.h... yes
    checking for stdint.h... yes
    checking for unistd.h... yes
    checking dlfcn.h usability... yes
    checking dlfcn.h presence... yes
    checking for dlfcn.h... yes
    checking for g77... no
    checking for xlf... no
    checking for f77... no
    checking for frt... no
    checking for pgf77... no
    checking for cf77... no
    checking for fort77... no
    checking for fl32... no
    checking for af77... no
    checking for xlf90... no
    checking for f90... no
    checking for pgf90... no
    checking for pghpf... no
    checking for epcf90... no
    checking for gfortran... no
    checking for g95... no
    checking for xlf95... no
    checking for f95... no
    checking for fort... no
    checking for ifort... no
    checking for ifc... no
    checking for efc... no
    checking for pgf95... no
    checking for lf95... no
    checking for ftn... no
    checking whether we are using the GNU Fortran 77 compiler... no
    checking whether accepts -g... no
    checking the maximum length of command line arguments... 98304
    checking command to parse /usr/bin/nm -B output from gcc object... ok
    checking for objdir... .libs
    checking for ar... ar
    checking for ranlib... ranlib
    checking for strip... strip
    checking if gcc supports -fno-rtti -fno-exceptions... no
    checking for gcc option to produce PIC... -fPIC
    checking if gcc PIC flag -fPIC works... yes
    checking if gcc static flag -static works... yes
    checking if gcc supports -c -o file.o... yes
    checking whether the gcc linker (/usr/bin/ld) 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... no
    configure: creating libtool
    appending configuration tag "CXX" to libtool
    appending configuration tag "F77" to libtool
    configure: creating ./config.status
    config.status: creating config.h
    running: make
    /bin/bash /var/tmp/pear-build-root/bcompiler-0.8/libtool --mode=compile gcc -I. -I/tmp/pear/cache/bcompiler-0.8 -DPHP_ATOM_INC -I/var/tmp/pear-build-root/bcompiler-0.8/include -I/var/tmp/pear-build-root/bcompiler-0.8/main -I/tmp/pear/cache/bcompiler-0.8 -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DHAVE_CONFIG_H -g -O2 -c /tmp/pear/cache/bcompiler-0.8/bcompiler.c -o bcompiler.lo
    mkdir .libs
    gcc -I. -I/tmp/pear/cache/bcompiler-0.8 -DPHP_ATOM_INC -I/var/tmp/pear-build-root/bcompiler-0.8/include -I/var/tmp/pear-build-root/bcompiler-0.8/main -I/tmp/pear/cache/bcompiler-0.8 -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DHAVE_CONFIG_H -g -O2 -c /tmp/pear/cache/bcompiler-0.8/bcompiler.c -fPIC -DPIC -o .libs/bcompiler.o
    /tmp/pear/cache/bcompiler-0.8/bcompiler.c:1342:8: warning: extra tokens at end of #endif directive
    /bin/bash /var/tmp/pear-build-root/bcompiler-0.8/libtool --mode=link gcc -DPHP_ATOM_INC -I/var/tmp/pear-build-root/bcompiler-0.8/include -I/var/tmp/pear-build-root/bcompiler-0.8/main -I/tmp/pear/cache/bcompiler-0.8 -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DHAVE_CONFIG_H -g -O2 -o bcompiler.la -export-dynamic -avoid-version -prefer-pic -module -rpath /var/tmp/pear-build-root/bcompiler-0.8/modules bcompiler.lo -lbz2
    gcc -shared .libs/bcompiler.o -lbz2 -Wl,-soname -Wl,bcompiler.so -o .libs/bcompiler.so
    creating bcompiler.la
    (cd .libs && rm -f bcompiler.la && ln -s ../bcompiler.la bcompiler.la)
    /bin/bash /var/tmp/pear-build-root/bcompiler-0.8/libtool --mode=install cp ./bcompiler.la /var/tmp/pear-build-root/bcompiler-0.8/modules
    cp ./.libs/bcompiler.so /var/tmp/pear-build-root/bcompiler-0.8/modules/bcompiler.so
    cp ./.libs/bcompiler.lai /var/tmp/pear-build-root/bcompiler-0.8/modules/bcompiler.la
    PATH="$PATH:/sbin" ldconfig -n /var/tmp/pear-build-root/bcompiler-0.8/modules
    ----------------------------------------------------------------------
    Libraries have been installed in:
    /var/tmp/pear-build-root/bcompiler-0.8/modules

    If you ever happen to want to link against installed libraries
    in a given directory, LIBDIR, you must either use libtool, and
    specify the full pathname of the library, or use the `-LLIBDIR’
    flag during linking and do at least one of the following:
    – add LIBDIR to the `LD_LIBRARY_PATH’ environment variable
    during execution
    – add LIBDIR to the `LD_RUN_PATH’ environment variable
    during linking
    – use the `-Wl,–rpath -Wl,LIBDIR’ linker flag
    – have your system administrator add LIBDIR to `/etc/ld.so.conf’

    See any operating system documentation about shared libraries for
    more information, such as the ld(1) and ld.so(8) manual pages.
    ———————————————————————-

    Build complete.
    Don’t forget to run ‘make test’.

    running: make INSTALL_ROOT=”/var/tmp/pear-build-root/install-bcompiler-0.8″ install
    Installing shared extensions: /var/tmp/pear-build-root/install-bcompiler-0.8/usr/lib/php5/20060613+lfs/
    running: find “/var/tmp/pear-build-root/install-bcompiler-0.8” -ls
    191629 4 drwxr-xr-x 3 root root 4096 Aug 10 12:20 /var/tmp/pear-build-root/install-bcompiler-0.8
    191686 4 drwxr-xr-x 3 root root 4096 Aug 10 12:20 /var/tmp/pear-build-root/install-bcompiler-0.8/usr
    191687 4 drwxr-xr-x 3 root root 4096 Aug 10 12:20 /var/tmp/pear-build-root/install-bcompiler-0.8/usr/lib
    191688 4 drwxr-xr-x 3 root root 4096 Aug 10 12:20 /var/tmp/pear-build-root/install-bcompiler-0.8/usr/lib/php5
    191689 4 drwxr-xr-x 2 root root 4096 Aug 10 12:20 /var/tmp/pear-build-root/install-bcompiler-0.8/usr/lib/php5/20060613+lfs
    191685 124 -rwxr-xr-x 1 root root 119116 Aug 10 12:20 /var/tmp/pear-build-root/install-bcompiler-0.8/usr/lib/php5/20060613+lfs/bcompiler.so

    Build process completed successfully
    Installing ‘/usr/lib/php5/20060613+lfs/bcompiler.so’
    install ok: channel://pear.php.net/bcompiler-0.8

  4. If you encounter “ERROR: `phpize’ failed“, that means you have not yet installed php5-dev. Repeat step 1 then try again.
  5. If you encounter “configure: WARNING: You will need re2c 0.12.0 or later if you want to regenerate PHP parsers“, repeat step 2 to install re2c.
  6. If you encounter “checking for BZip2 in default path… not found” or “configure: error: Please reinstall the BZip2 distribution“, repeat step 2 to install libbz2-dev.
  7. Add “extension=bcompiler.so” to /etc/php5/cli/php.ini to activate the extension.
  8. You’re done! To test it, save the following file as example.php:
    <?php
    /**
    * A function to output hello world
    *
    * @function helloWorld
    * @return void
    */
    function helloWorld() {
    echo "Hello World!\n";
    }
    ?>

    Save the following as compile.php:

    <?php
    $fh = fopen("example.phb", "w");
    bcompiler_write_header($fh);
    bcompiler_write_file($fh, "example.php");
    bcompiler_write_footer($fh);
    fclose($fh);
    ?>

    And save this one as use_p.php:

    <?php
    include_once('example.phb');
    helloWorld();
    helloWorld();
    helloWorld();
    ?>

    Now run compile.php to compile it. Then run use_p.php – you should see “Hello World!” three times. If you do, you’re done!

  9. If for any reason your computer cannot compile bcompiler.so, here is a binary build just in case. Do not forget to test it using the step above!

If this guide or article was helpful, please help us spread the word!

If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!

Topics: Linux, PHP | No Comments »

Comments