Build with Go and xk6
Building locally gives you direct control over the build process and is ideal for development.Before you start
Install Go
Set up Go (version 1.19 or higher) and ensure
go version returns the correct version.Install Git
Install Git for downloading extension sources.
If the command doesn’t return a path, ensure
$GOPATH/bin is in your $PATH. See the Go documentation for details.Build your first extension
Once installed, build a k6 binary with extensions using thexk6 build command:
k6 binary in the current directory with the xk6-sql and xk6-output-prometheus-remote extensions.
Output:
Command breakdown
The xk6 command syntax:Understanding the build command
From the example above:- Version:
latestbuilds using the latest k6 source code. Consider using a stable release version (e.g.,v0.45.1) as a best practice. - Extension modules: Each
--withflag specifies a full GitHub URI. Without a version, it defaults tolatest. Lock versions like[email protected]for stability. - Output location: Without
--output, the binary is namedk6in the current directory. Use--output k6-extendedfor a custom name or--output /tmp/k6for a different location.
Build from a local repository
If you’ve cloned an extension repository locally and want to use that version:. tells xk6 to use the current directory as the replacement for the module.
Run your extended binary
Now run test scripts using the bundled extensions:Be sure to specify
./k6 on Linux/Mac to use the local binary, otherwise the system may execute a different k6 on your PATH. Windows shells search the current directory by default.Build with Docker
Using the xk6 Docker image simplifies the process by avoiding local Go setup.This guide covers creating a custom k6 binary using Docker. To create a Docker image with a custom k6 binary, refer to the Using modules with Docker documentation.
Build your first extension
Build a custom k6 binary with the latest versions of k6 and extensions:k6 (or k6.exe) binary in the current working directory.
Build with specific versions
To build with concrete versions (k6v0.45.1, xk6-kafka v0.19.1, xk6-output-influxdb v0.4.1):
Docker command breakdown
The Docker-specific part of the command:--rm- Container is destroyed after the build completes-u- Specifies user and group IDs so the binary has the same permissions as the host user-v- Mounts the current directory inside the container so the binary can be written to it
Verify the build
Run the version command to check your build:Run your extended binary
On Linux/Mac, specify
./k6 to use the local binary. Windows shells search the current directory by default.Common build scenarios
Multiple extensions
Bundle multiple extensions in one binary:Output to specific location
Latest development version
Troubleshooting
If you encounter issues:- Go environment: Ensure
go versionworks and$GOPATH/binis in your$PATH - xk6 not found: Verify installation with
which xk6orwhere xk6 - Build errors: Check extension compatibility with your k6 version
- Docker permissions: On Linux, use
-u "$(id -u):$(id -g)"to avoid permission issues
Next steps
Create Extensions
Build your own k6 extensions
Explore Extensions
Find extensions for your use case
Protocol Extensions
Use MQTT, Redis, and SQL extensions
xk6 Repository
View xk6 source and documentation