Building Go Application using confluent-kafka-go on Linux
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
confluent-kafka-go is Confluent’s Go client for Apache Kafka, built on top of librdkafka. On Linux, the main build questions are no longer “Can Go talk to Kafka?” but “Which client version should I import, do I need system librdkafka, and what build flags does my Linux target require?”
Start With the Current Build Model
Modern confluent-kafka-go projects should use the v2 module path:
That matters because older tutorials often use a pre-v2 import path and outdated install steps.
Another important detail is that the project uses cgo, because it wraps librdkafka. That means CGO_ENABLED must stay enabled. If you try to build with CGO_ENABLED=0, the client will not build correctly.
On many mainstream Linux systems, you do not need to install librdkafka manually just to get started. Current versions of the client include prebuilt librdkafka binaries for supported Linux targets. Manual installation is mainly for unsupported platforms or cases where you need features such as GSSAPI or a custom dynamic link.
Create the Project and Add the Dependency
The simplest path on a glibc-based Linux system looks like this:
If you are building on Alpine Linux, add the musl build tag:
That is one of the most common Linux-specific details people miss. Alpine uses musl rather than glibc, so the extra build tag matters.
A Minimal Producer Example
Here is a small producer that sends one message and waits for delivery reporting:
This sample is intentionally small, but it shows the core pieces:
- '
bootstrap.serversidentifies the broker list' - '
Produceis non-blocking' - delivery reports come back through an event channel
That last point is important. The send call queues work to the underlying client. Success is not guaranteed until the delivery report confirms it.
A Minimal Consumer Example
Once the build works, a simple consumer helps verify the runtime path:
If both producer and consumer run successfully, the Linux toolchain, cgo, and Kafka connectivity are usually in good shape.
When You Need System librdkafka
Although many Linux builds work with the bundled binaries, there are situations where system installation still matters:
- you need Kerberos or another feature not present in the bundled build
- your Linux target is not one of the supported prebuilt platforms
- your organization requires linking against a controlled system package
In those cases, install librdkafka using the Linux package manager and build with the dynamic tag expected by the client documentation.
That is why old advice that says “always install librdkafka-dev first” is no longer universally correct. It depends on the target and feature set.
Common Pitfalls
The most common pitfall is using the old import path without /v2. That pulls in stale examples and often leads to confusing build instructions.
Another mistake is disabling cgo. confluent-kafka-go is a Go wrapper around a C library, so CGO_ENABLED=0 is incompatible with normal builds.
A third issue is forgetting the musl build tag on Alpine Linux. The code may be fine, but the wrong Linux target assumptions will still break the build.
Finally, many developers test only whether go build succeeds and forget runtime verification. A successful compile does not prove the broker connection, topic configuration, or delivery path actually work.
Summary
- Use the current v2 import path for
confluent-kafka-go. - Keep
cgoenabled because the client depends onlibrdkafka. - On many Linux systems, the bundled
librdkafkais enough for a normal build. - Alpine Linux needs the
muslbuild tag. - Validate both build success and live producer or consumer behavior before calling the setup complete.

