skip to Main Content

I tried to install V8JS with this Dockerfile

FROM php:7.3-cli-buster

RUN apt-get update -y --fix-missing && apt-get upgrade -y;

# Install v8js
RUN apt-get install -y libv8-dev
RUN pecl install v8js
RUN docker-php-ext-enable v8js

but I got a configuration error:

checking for V8 files in default path… not found

configure: error: Please reinstall the v8 distribution ERROR:
`/tmp/pear/temp/v8js/configure
–with-php-config=/usr/local/bin/php-config –with-v8js’ failed The command ‘/bin/sh -c pecl install v8js’ returned a non-zero code: 1

full cli output:

Sending build context to Docker daemon   35.6MB
Step 1/5 : FROM php:7.3-cli-buster
 ---> c7ff0bf4f6fb
Step 2/5 : RUN apt-get update -y --fix-missing && apt-get upgrade -y;
 ---> Using cache
 ---> e151d6e061d2
Step 3/5 : RUN apt-get install -y libv8-dev
 ---> Using cache
 ---> fe35f48dd8cf
Step 4/5 : RUN pecl install v8js
 ---> Running in d9f4ba184d81
downloading v8js-2.1.1.tgz ...
Starting to download v8js-2.1.1.tgz (101,888 bytes)
.......................done: 101,888 bytes
28 source files, building
running: phpize
Configuring for:
PHP Api Version:         20180731
Zend Module Api No:      20180731
Zend Extension Api No:   320180731
Please provide the installation prefix of libv8 [autodetect] : building in /tmp/pear/temp/pear-build-defaultuserEVh9Nq/v8js-2.1.1
running: /tmp/pear/temp/v8js/configure --with-php-config=/usr/local/bin/php-config --with-v8js
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 cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... none needed
checking how to run the C preprocessor... cc -E
checking for icc... no
checking for suncc... no
checking whether cc understands -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... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for PHP prefix... /usr/local
checking for PHP includes... -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib
checking for PHP extension directory... /usr/local/lib/php/extensions/no-debug-non-zts-20180731
checking for PHP installed headers prefix... /usr/local/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for re2c... re2c
checking for re2c version... 1.1.1 (ok)
checking for gawk... no
checking for nawk... nawk
checking if nawk is broken... no
checking for V8 Javascript Engine... yes, shared
checking for V8 files in default path... not found
configure: error: Please reinstall the v8 distribution
ERROR: `/tmp/pear/temp/v8js/configure --with-php-config=/usr/local/bin/php-config --with-v8js' failed
The command '/bin/sh -c pecl install v8js' returned a non-zero code: 1

How to resolve configuration error (due to lack of a path to the V8 files I guess) and install V8JS on PHP Docker FROM Debian Buster?

EDIT

Dockerfile from @saulotoledo answer works 🙂

Building image takes approx 60-90 min and the image size is 5.47GB vs base image (FROM) 7.3-cli-buster 367MB

to build Dockerfile php with V8JS copy the code from @saulotoledo answer and paste it into the file named Dockerfile

Then run in the same directory this command:

docker build --tag "test-php-js" .

Then run container this way:

docker run -it --rm --entrypoint="" --name="php-v8js" test-php-js /bin/sh

you should be logged into terminal of php-cli, type:

php -m

and you should see a list of enabled extensions including v8js

type:

php --ri v8js

to see some details, something like:

V8 Javascript Engine => enabled
V8 Engine Compiled Version => 7.4.288.21
V8 Engine Linked Version => 7.4.288.21
Version => 2.1.1

and now the cherry part you all have been waiting for – type:

php -r "(new V8Js())->executeString("print('Hello' + ' from JS ' + 'World!')", 'basic.js');";

to see php running js code with V8JS support 😀

Hello from JS World!

Note that I needed to put extra backslash in front of every JS doublequote " for the php -r command. But that’s only due to running JS from php in cli mode as oneliner.

Normally you don’t need that
See an official PHP documentation example

Does anyone know if it is possible to install and enable V8JS extension without building it from its source but by using Debian Buster package and PECL as I tried at the beginning?

3

Answers


  1. Chosen as BEST ANSWER

    Thanks to saulotoledo I created example image that takes 950MB vs 398MB base image (php:7.3-cli-buster)

    It works also with PHP FPM, just change FROM php:7.3-cli-buster to FROM php:7.3-fpm-buster if you want FPM version.

    Dockerfile

    FROM php:7.3-cli-buster
    ENV V8_VERSION=7.4.288.21
    # php:7.3-cli-buster 398MB
    
    RUN apt-get update -y --fix-missing && apt-get upgrade -y;
    
    # Install required CLI tools
    RUN apt-get install -y --no-install-recommends 
        libtinfo5 libtinfo-dev 
        build-essential 
        curl 
        git 
        libglib2.0-dev 
        libxml2 
        python 
        patchelf
    
    # Install V8, PHP's V8Js
    RUN cd /tmp 
        
        && git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git --progress --verbose 
        && export PATH="$PATH:/tmp/depot_tools" 
        
        && fetch v8 
        && cd v8 
        && git checkout $V8_VERSION 
        && gclient sync 
        && tools/dev/v8gen.py -vv x64.release -- is_component_build=true use_custom_libcxx=false 
        
        && cd /tmp/v8 
        && ninja -C out.gn/x64.release/ 
        && mkdir -p /opt/v8/lib && mkdir -p /opt/v8/include 
        && cp out.gn/x64.release/lib*.so out.gn/x64.release/*_blob.bin out.gn/x64.release/icudtl.dat /opt/v8/lib/ 
        && cp -R include/* /opt/v8/include/ 
        && for A in /opt/v8/lib/*.so; do patchelf --set-rpath '$ORIGIN' $A;done 
        
        && cd /tmp 
        && git clone https://github.com/phpv8/v8js.git 
        && cd v8js 
        && phpize 
        && ./configure --with-v8js=/opt/v8 LDFLAGS="-lstdc++" 
        && make 
        && make test 
        && make install 
        
        && docker-php-ext-enable v8js 
        
        && rm -rf "/tmp/v8" 
        && rm -rf "/tmp/depot_tools"
    
    # Image size after removing source files 950MB
    

  2. Edit:
    That should solve your issue (note that I am compiling it manually):

    FROM php:7.3-cli-buster
    ENV V8_VERSION=7.4.288.21
    
    RUN apt-get update -y --fix-missing && apt-get upgrade -y;
    
    # Install v8js (see https://github.com/phpv8/v8js/blob/php7/README.Linux.md)
    RUN apt-get install -y --no-install-recommends 
        libtinfo5 libtinfo-dev 
        build-essential 
        curl 
        git 
        libglib2.0-dev 
        libxml2 
        python 
        patchelf 
        && cd /tmp 
        
        && git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git --progress --verbose 
        && export PATH="$PATH:/tmp/depot_tools" 
        
        && fetch v8 
        && cd v8 
        && git checkout $V8_VERSION 
        && gclient sync 
        
        && tools/dev/v8gen.py -vv x64.release -- is_component_build=true use_custom_libcxx=false
    
    RUN export PATH="$PATH:/tmp/depot_tools" 
        && cd /tmp/v8 
        && ninja -C out.gn/x64.release/ 
        && mkdir -p /opt/v8/lib && mkdir -p /opt/v8/include 
        && cp out.gn/x64.release/lib*.so out.gn/x64.release/*_blob.bin out.gn/x64.release/icudtl.dat /opt/v8/lib/ 
        && cp -R include/* /opt/v8/include/ 
        && apt-get install patchelf 
        && for A in /opt/v8/lib/*.so; do patchelf --set-rpath '$ORIGIN' $A;done
    
    # Install php-v8js
    RUN cd /tmp 
        && git clone https://github.com/phpv8/v8js.git 
        && cd v8js 
        && phpize 
        && ./configure --with-v8js=/opt/v8 LDFLAGS="-lstdc++" 
        && make 
        && make test 
        && make install
    
    RUN docker-php-ext-enable v8js
    

    Old answer:

    I believe you need to compile the V8 manually. I am not sure yet why the libv8-dev is not enough. Since that does not work, you need to compile it because it is a requirement.

    That said, this link may be helpful. But the official compilation instructions are presented here. After a few tests I noticed you need to add libtinfo5 (and maybe libtinfo-dev, to be safe, but you may try it without it) to your apt-get install, otherwise the ninja build in the instructions will fail.

    After using that compilation instructions in your Dockerfile, the V8 will compile properly. After that you will still have some trouble with the pecl installation. Unfortunately I had to stop investigating the problem for now, the compilation takes some time. However, the following links may help you to solve your problem:

    • This one if you still have trouble compiling with ninja
    • This one presents another Dockerfile for compiling V8, but the base image is different and needs some adaptation.

    I hope it helps. If you manage to have a working Dockerfile please share with us later. If I have some time later to investigate it and I have some progress I will edit my answer.

    Login or Signup to reply.
  3. This works without compiling v8 – it is very fast. Using distro packaged nodejs library. You can use current official php docker image

    RUN apt-get install -y libnode-dev 
        && cp -s /usr/lib/x86_64-linux-gnu/libv8* /usr/local/lib/ 
        && cp -rs /usr/include/node/* /usr/local/include/ 
        && pecl install v8js 
        && echo "extension=v8js.so" > /usr/local/etc/php/conf.d/v8js.so
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search