How do I install an extension of Kafka for PHP?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a popular distributed event streaming platform capable of handling trillions of events a day. Integrating Kafka with different programming languages can expand its utility. For PHP developers, the php-rdkafka extension is a high-quality PHP extension that provides a stable and performant interface to the Apache Kafka cluster.
Introduction to php-rdkafka
The php-rdkafka extension is a Kafka client for PHP, based on the librdkafka C library, which provides low-level functionality necessary for Kafka communication. This extension supports the advanced features of Kafka (like message compression and custom partitioners) and can be a useful tool for PHP applications that require integration with Kafka for event-driven architectures.
Prerequisites
Before you install php-rdkafka, ensure you have the following prerequisites installed:
- PHP development environment (PHP 7.0 or greater)
php-pearandphp-devpackages to compile and install PHP extensionsbuild-essentialfor required tools likemakelibrdkafka(the C library that powers the extension)
The version of librdkafka should be compatible with the php-rdkafka version you intend to install.
Step-by-Step Installation Guide
1. Install librdkafka
First, you need to install librdkafka, as it is a required dependency for php-rdkafka. You can install it from source or from pre-built binaries. Here is how you can install it from source:
2. Install php-rdkafka
Once librdkafka is installed, you can proceed to install the php-rdkafka extension. This can also be done in several ways, including PECL, compiling from source, or using a package manager if available.
Installing via PECL
PECL is the simplest method if available:
If successful, add extension=rdkafka.so to your php.ini file.
Compiling from Source
If you prefer or need to compile manually from source, here are the steps:
Add extension=rdkafka.so to your php.ini file.
3. Verify the Installation
To verify that the installation was successful, run:
This command should display the version of rdkafka and the available configuration options.
Usage Example
With php-rdkafka installed, you can now produce and consume messages with Kafka. Here is a simple example of producing a message:
Summary
Here’s a table summarizing the key steps and components involved in setting up php-rdkafka with PHP:
| Step | Component | Purpose |
| Install prerequisites | PHP, php-pear, etc. | Prepare environment for php-rdkafka |
| Install librdkafka | librdkafka | C library dependency for php-rdkafka |
| Install php-rdkafka via PECL or source | php-rdkafka | PHP extension for Apache Kafka |
| Verify installation | php --ri rdkafka | Ensure correct installation |
| Test with a producer code snippet | Kafka producer | Test producing messages to Kafka |
Conclusion
Integrating Kafka with PHP using the php-rdkafka extension can significantly enhance an application's capabilities to interact with real-time data streams. With proper installation and configuration, PHP applications can both produce to and consume from a Kafka cluster, opening up a wide range of possibilities for real-time data processing and analytics.
Related reading
- How do I run celery status/flower without the -A option?
- How do I set a number of retry attempts in RabbitMQ?
- How do I set a number of retry attempts in RabbitMQ?
- How do I set the size of messages in Kafka?
- How do I set up a Kafka service on gitlab-ci.yml?
- How do I start a RabbitMQ node?
- How do I stop attempting to consume messages off of Kafka when at the end of the log?
- How do I stop the RabbitMQ server on localhost

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.