How to install v8js for PHP 5 on Linux

    I didn’t find too many documentation online on how to install v8js for PHP 5 on Linux, so I decided to write my own tutorial for it. I’m using Debian 8 with php5-fpm, but if you tweak it around you should be able to easily get it working on other distro’s and setups.

    If you want to install v8js for PHP on Windows, you can follow this tutorial:
    https://blog.xenokore.com/how-to-install-v8js-for-php-on-windows/

    And if you want to install v8js for PHP 7 on Linux, you can follow this documentation:
    https://github.com/phpv8/v8js/blob/php7/README.Linux.md

    Let’s get started.

    Login to your server and start by installing the required packages:

    apt-get install php-pear php5-dev libv8-dev g++ cpp

    We will now use PECL to install the v8js package:

    pecl install v8js-0.1.3

    0.1.3 is the last version that’s supported by default PHP 5.x installations. You can find more PECL packages here: http://pecl.php.net/package/v8js

    After installing the package, you have to add extension=v8js.so to the end of your php.ini file. You can do this easily by running the following command, if your configuration is the same:

    echo "extension=v8js.so" >> /etc/php5/fpm/php.ini

    After you have added that line you can now simply restart your webserver and php processor:

    service nginx restart && service php5-fpm restart

    Now you’re all set and should be able to use v8js in your PHP scripts. Let’s test it out with a simple script:

    $v8 = new V8Js();
    $v8->executeString("print('hello world');");

    Output: hello world

    There we go, it’s as simple as that! Remember that it does not have any sort of DOM, so running commands on window. and document. will not work without first creating the DOM. There are some ways to do this by loading a javascript implementation of the DOM, but that’s for another time.

    Have fun!

    Leave a Reply

    Your email address will not be published. Required fields are marked *