Skip to content

Setting up PHP 5 + Gearman + Java on Ubuntu Linux 9.10 (Karmic)

April 10, 2010

Bellow are stops to setup a running Gearman based job server which can be used to have various work-distribution clusters especially creating an interoperability between your web layer (esp PHP5) and backing business layer (esp. in Java).

Prepare Gearman job server

You may decide to get the source and compile or use Ubunut’s PPA repositories to get them pre-built. I go with the second and added this repository ( deb karmic main ) to my /etc/apt/source.list .

Install Job Server

sudo aptitude update
sudo aptitude install gearman-job-server

Prepare PHP environment

sudo aptitude install uuid-dev php5-dev
sudo pecl install channel://

And also add to both:

  • /etc/php5/apache2/php.ini
  • /etc/php5/cli/php.ini

Check Gearman PHP extension
Get phpinfo() and see if gearman exists there


Setup Java/Gearman Binding

Get the project from

Startup Gearmand

sudo /etc/init.d/gearman-job-server start

Sample Java based Worker

import org.gearman.client.*;
import org.gearman.common.GearmanJobServerConnection;
import org.gearman.common.GearmanNIOJobServerConnection;
import org.gearman.worker.AbstractGearmanFunction;
import org.gearman.worker.GearmanWorker;
import org.gearman.worker.GearmanWorkerImpl;

public class SampleGearmanWorker {

  public static void main(String[] args) {
    final GearmanJobServerConnection connection = new
    GearmanNIOJobServerConnection("localhost", 4730);

    GearmanWorker worker = new GearmanWorkerImpl();


  private static final byte[] EMPTY_BYTES = new byte[]{};

  public static class SampleFunction extends AbstractGearmanFunction {

  public String getName() {
    return "test";

  public GearmanJobResult executeFunction() {
    final byte[] hello = "hello ".getBytes();
    final byte[] name = (byte[]);
    final byte[] resp = new byte[hello.length + name.length];

    System.arraycopy(hello, 0, resp, 0, hello.length);
    System.arraycopy(name, 0, resp, hello.length, name.length);

    final GearmanJobResult jr = new
        GearmanJobResultImpl(this.jobHandle, true, resp, EMPTY_BYTES, EMPTY_BYTES, 0, 0);

    return jr;

Sample PHP Client

$client= new GearmanClient();
print $client->do("test", "amin")."\n";

Run the example

run Java code and also php gearman-client.php. It will print “hello amin” :)

Hope it helps!

  1. very informative..thanks a lot..james

  2. AlexT permalink

    For some reasons, Ubuntu installed libgearman2… And

    pecl install channel://

    fails with

    checking whether to enable gearman support… yes, shared
    not found
    configure: error: Please install libgearman
    ERROR: `/tmp/pear/temp/gearman/configure’ failed


  3. jose permalink

    you need
    sudo aptitude install php-pear
    otherwise sudo pecl does not work

    after that I am stock with the same error as Alex
    configure: error: Please install libgearman
    ERROR: `/tmp/pear/temp/gearman/configure’ failed

  4. If you are running Ubuntu 10.04, this should get you up and running:

    EDIT: /etc/apt/source.list and add the following:
    deb lucid main

    sudo apt-key adv –recv-keys –keyserver
    sudo apt-get update
    sudo apt-get install gearman-job-server libgearman2 libgearman-dev uuid-dev php5-dev libgearman-dev
    sudo pecl install channel://

  5. Luke permalink


    After the “apt-get install” I get the following error (I’m on 10.04):

    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    uuid-dev is already the newest version.
    php5-dev is already the newest version.
    Some packages could not be installed. This may mean that you have
    requested an impossible situation or if you are using the unstable
    distribution that some required packages have not yet been created
    or been moved out of Incoming.
    The following information may help to resolve the situation:

    The following packages have unmet dependencies:
    gearman-job-server: Depends: libdrizzle1 (>= 2010.10.01) but it is not installable
    E: Broken packages

  6. @Luke:

    Install Drizzle (Gearman Dependency)

    1. Add the signing key to your keyring:
    > sudo apt-key adv –keyserver –recv-keys 06899068
    or see 1a. (especially if dealing with an outbound proxy server)

    2. Add the PPA:
    > sudo sh -c ‘echo “deb lucid main” > /etc/apt/sources.list.d/drizzle.list’

    3. Update the APT cache
    > sudo apt-get update

    4. Install drizzle-dev (retrieves libdrizzle1)
    > sudo apt-get install -y drizzle

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

<span>%d</span> bloggers like this: