DEV Community

Peter Fox
Peter Fox

Posted on • Originally published at Medium on

Laravel Octane: Compiling RoadRunner from Source

Photo by Jean Gerber on Unsplash

RoadRunner is one of the two options available for use with Laravel Octane a new package released during 2021 as a way to speed up requests significantly.

RoadRunner itself is an open source PHP application server written in the language Go (or known as Golang should you wish to get decent search results from google). Much like Apache or Nginx & PHP-FPM, HTTP requests can be sent to RoadRunner to execute PHP code and then send a response back in returned. The difference in RoadRunner is that it can keep aspects of the PHP application in memory for far better speed after the booting of the application. For Laravel this is fairly beneficial.

Normally when you install Octane and pick RoadRunner, the rr binary will be downloaded for you which is executable for the server.

Why compile your own version?

RoadRunner itself is built in a reasonably modular way with it’s own service container and plugin mechanism that provides a lot of potential for expansion. The first example of this is in exposing Go services to the PHP code via a built in RPC server which can act similarly to a PHP extension. The second is the ability to apply middleware to Requests in much the same way Laravel can. With Go itself being a fairly easy language to pick up and write performant code in this all makes for a winning combination.

Getting Started

The first thing you’ll need to do is make sure you have Go installed and that it’s version is greater than 1.13. At the time of writing the latest version is 1.16. If you have Go installed but don’t know the version you have, you can check with go version. If it’s not installed already you can download it from the Golang site or as a Mac user I prefer to install it via brew. Also for good measure you should have a git client to use for cloning repos from GitHub.

For the sake of this article and testing something with the final end product you should set up a Laravel Octane project with RoadRunner and test it’s all working as you would expect before continuing. If you want a short cut, I’ve made a demo repo with Octane already install that you can clone.

Then to get starting with RoadRunner itself what we’ll need to do is clone the RoadRunner binary source code. The easier way to do this is with git but there’s no reason you can’t download the source code as a zip file.

git clone [https://github.com/spiral/roadrunner-binary](https://github.com/spiral/roadrunner-binary)
Enter fullscreen mode Exit fullscreen mode

Once we’ve done that we need to download all the dependencies required to compile the binary. This is fairly similar to how we use composer for PHP packages. We do this via the go mod commands.

go mod download
Enter fullscreen mode Exit fullscreen mode

This should then give you all the dependencies for doing a simple complication of RoadRunner.

The build command itself will be needed to run on the platform you intend to be using. For instance if you’re developing on a Mac and deploying to a Linux server the binary file generated won’t work. You’ll need to have a Linux environment to compile RoadRunner in.

The command to build RoadRunner is:

go build -trimpath -ldflags “-s” -o ./rr ./cmd/rr
Enter fullscreen mode Exit fullscreen mode

This should leave you with a rr file in the director that is the self contained application server binary. We can then copy this to our Laravel application (with Octane already installed and set up for RoadRunner of course) and then you can execute the artisan octane:start command and everything should run as normal. You will notice a remark at the start of the command though, “Unable to determine the current RoadRunner binary version. Please report this issue: https://github.com/laravel/octane/issues/new.. This is to be expected though as we’re no longer using a release version but our own custom binary.

Now obviously we haven’t achieved anything huge here, the binary compiled is practically the same one that Laravel Octane itself installs into your application for the purpose of running with Octane but now we have this process down we can start looking at customising the code ourselves and implementing our own plugins.

I hope to cover getting started with RoadRunner plugins very soon so please do follow me on social media for the next installments.

I’m Peter Fox, a software developer in the UK who works with Laravel among other things. Thank you for reading my article, I’ve got several more on both medium and dev.to. If you want to know more about me, head over to https://www.peterfox.me. Also feel free to follow me @SlyFireFox on twitter for more Laravel tips and tutorials in the future.

Top comments (0)