Phpredis as Redis client in Ubuntu 12.04 LTS

Redis can be run with either Predis or phpredis. This post will explain how you can install the phpredis client on Ubuntu 12.04 LTS. This post assumes you run the standard LAMP stack. We have talked about running Redis in Symfony2 before, but then we assumed you would be running Predis.

Redis is an open source, BSD licensed, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets. — source:

The main advantage of running phpredis over running Predis is that it is faster, because phpredis is written in C, while Predis is written in PHP. So, if you want to run the fastest client for your server than follow these simple instructions:

Instructions for installing phpredis (bleeding edge)

1) Make sure you have the dependencies installed:

sudo apt-get install redis-server php5-dev build-essential xsltproc

2) Now clone the phpredis repo, configure and make:

git clone
cd phpredis/

3a) Install the compiled files directly:

sudo cp modules/ `php-config --extension-dir`
sudo cp rpm/redis.ini /etc/php5/conf.d/
sudo service apache2 restart

3b) or create, view and install the Debian package:

dpkg -c phpredis-x86_64.deb
sudo dpkg -i phpredis-x86_64.deb

Instructions for installing phpredis (stable)

1) Make sure you have the dependencies installed:

sudo apt-get install redis-server dh-make-php php5-dev build-essential

2) Now get Redis from PECL and make a package:

mkdir redis
cd redis
export DEBFULLNAME="Maurits van der Schee"
export DEBEMAIL=""
dh-make-pecl redis

3) or create, view and install the Debian package:

cd php-redis-2.2.3/
dpkg -c ../php5-redis_2.2.3-1_amd64.deb
sudo dpkg -i ../php5-redis_2.2.3-1_amd64.deb

Test to see whether it works or not

To test the loaded module create a PHP file that you run from the command-line or via Apache2:

        $redis = new Redis() or die("Cannot load Redis module.");
        $redis->set('random', rand(5000,6000));
        echo $redis->get('random');

That’s it! Easy. And it performs way better than Predis.

Reliable queues in Redis

You can run Redis, for instance, if you need a fast and scalable queuing system in a distributed platform. You can implement such a queue using the LPUSH and BRPOP commands. With LPUSH you add elements to the start of the list, while the BRPOP removes elements from the list, blocking when no elements are available. But if you want that queue to be reliable, you can consider using the BRPOPLPUSH command, that can add the returned element to an ‘in progress’ queue before returning it to the client. This way you can remove the element from the ‘in progress’ queue when you successfully handled them, or put them back on the original queue if you failed.


4 thoughts on “Phpredis as Redis client in Ubuntu 12.04 LTS”

  1. @Misja: That sounds like a good idea! I’ll try it and update the blog post.

Leave a Reply

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