Quick Tip: Install Recki-CT into a Vagrant Ubuntu Box

Bruno Skvorc
Share

Recki what?

If you don’t know what Recki-CT is, see @ircmaxell’s original post or the repo, we won’t go into depth here. This quick tip will merely show you how to install it on a Homestead Improved box, much like we did with other software before.

Step 1 – Homestead Improved

First and foremost, get a Homestead Improved instance up and running.

Give it a new virtual host like so:

- map: test.app
  to: /home/vagrant/Code/recki

Boot the VM and vagrant ssh into it.

Step 2 – JitFu

Recki-CT needs JitFu to be installed.

As per instructions, run the following commands in order while you’re in the VM.

sudo apt-get install bison flex texinfo

git clone git://git.sv.gnu.org/libjit.git libijt-fu
cd libjit-fu
./auto_gen.sh
./configure --prefix=/opt
make
sudo make install

git clone https://github.com/krakjoe/jitfu
cd jitfu
phpize
./configure --with-jitfu=/opt
make
sudo make install

sudo su
echo "extension=jitfu.so" >> /etc/php5/fpm/conf.d/20-jitfu.ini
echo "extension=jitfu.so" >> /etc/php5/cli/conf.d/20-jitfu.ini
exit

sudo service nginx restart
sudo service php5-fpm restart

To see if we installed it successfully:

cd ~/Code
git clone https://github.com/Swader/publicinfo recki
mv recki/public/index.php recki/
touch recki/recki.php

Open recki.php and paste the following content inside it:

<?php
use JITFU\Context;
use JITFU\Type;
use JITFU\Signature;
use JITFU\Func;
use JITFU\Value;

$context = new Context();

$integer   = Type::of(Type::int);

$function = new Func($context, new Signature($integer, [$integer]), function($args) use($integer) {
    $zero     = new Value($this, 0, $integer);
    $one      = new Value($this, 1, $integer);
    $two      = new Value($this, 2, $integer);

    /* if ($arg == 0) return 0; */
    $this->doIf(
        $this->doEq($args[0], $zero),
        function() use ($zero) {
            $this->doReturn($zero);
        }
    );

    /* if ($arg == 1) return 1; */
    $this->doIf(
        $this->doEq($args[0], $one),
        function() use($one) {
            $this->doReturn($one);
        }
    );

    /* return $function($arg-1) + $function($arg-2); */
    $this->doReturn(
        $this->doAdd(
            $this->doCall($this, [$this->doSub($args[0], $one)]),
            $this->doCall($this, [$this->doSub($args[0], $two)]))); 
});

$function->dump("Fibonacci");

var_dump($function(40)); /* __invoke with magicalness */
?>

If you go to test.app:8000 now, you should see JitFu support enabled in the PHPInfo screen. If you go to test.app:8000/recki.php, you should get int 102334155 as output rather quickly.

Step 3 – Clone and Compose

Next up, we’ll need to clone the Recki repo, and download dependencies with Composer.

cd ~/Code
rm -rf recki
git clone https://github.com/google/recki-ct recki
cd recki
composer install

Step 4 – Test

To see if it works, just run the examples via the command line:

php examples/01-basic-usage.php

or through the browser:

test.app:8000/examples/01-basic-usage.php

That’s all there is to it. Now you can focus on brutal optimizations of your PHP code in certain parts without having to replace the entire PHP engine your app is spinning on.