How to Compile LiteRT from Source: Docker, CMake, and Bazel Guide
Compile LiteRT using Docker for reproducible containerized builds, CMake for direct host and Android cross-compilation via presets, or Bazel for Google's canonical internal workflow.
LiteRT is Google’s high-performance on-device runtime for machine-learning and generative-AI workloads. The google-ai-edge/LiteRT repository organizes source code into logical layers—runtime core in litert/runtime/, compiler tools in litert/compiler/, and language bindings in litert/cc/ and litert/python/—that you can build using three supported build systems. This guide provides the exact commands, file paths, and prerequisites to compile LiteRT from source on Linux, macOS, Windows, and Android.
Prerequisites by Platform
Install the following toolchains before attempting to compile LiteRT. The repository requires specific compiler versions to match the internal Google toolchain.
-
Linux:
build-essential,clang-18,llvm-18,libc++-dev,libc++abi-dev,openjdk-17-jdk, Python 3, andbazelisk(or Bazel 7.x+). -
macOS: Xcode command-line tools, Homebrew, Python 3.11, and optionally
bazelisk. -
Windows: Visual Studio 2022 (C++ workload), Git-Bash, Python 3.13, and Bazelisk.
-
Android cross-compilation: Android SDK + NDK r21+, the
ANDROID_NDK_HOMEenvironment variable, and CMake 4.0.1 or newer.
Detailed package lists are available in g3doc/instructions/BUILD_INSTRUCTIONS.md for Bazel builds and g3doc/instructions/CMAKE_BUILD_INSTRUCTIONS.md for CMake workflows.
Building with Docker (Simplest Method)
The Docker workflow provides the fastest path to reproducible artifacts for both host and Android targets. The script at docker_build/build_with_docker.sh creates a Linux container, installs all dependencies, and executes the full CMake build automatically.
Ensure Docker Desktop or a Docker daemon is running, then execute:
cd /path/to/LiteRT
./docker_build/build_with_docker.sh
This command builds a litert image, configures CMake using the default preset defined in litert/CMakePresets.json, and outputs release artifacts to cmake_build/ (host) and cmake_build_android_arm64/ (Android). No manual toolchain configuration is required.
Building with CMake (Direct and Flexible)
CMake builds offer granular control over compiler flags, accelerator delegates, and output directories. The repository provides preset configurations in litert/CMakePresets.json and litert/cc_sdk/CMakePresets.json that encapsulate common build flavors.
Host Builds for Linux and macOS
Configure and compile the runtime for your local machine using the default preset:
# Release build
cmake --preset default
cmake --build cmake_build -j
# Debug build
cmake --preset default-debug
cmake --build cmake_build_debug -j
The default preset selects the appropriate generator and compiler flags for your host platform, placing static libraries like libLiteRtRuntime.a in the cmake_build/ directory.
Android Cross-Compilation
Cross-compile for Android arm64 by exporting your NDK path and selecting the android-arm64 preset. You must also specify TFLITE_HOST_TOOLS_DIR so the build can locate host-native flatbuffer compilers.
export ANDROID_NDK_HOME=$HOME/Android/Sdk/ndk/27.0.12096336
# Release build
cmake --preset android-arm64 \
-DTFLITE_HOST_TOOLS_DIR="$(cd host_flatc_build/_deps/flatbuffers-build && pwd)"
cmake --build cmake_build_android_arm64 -j
# Debug build
cmake --preset android-arm64-debug \
-DTFLITE_HOST_TOOLS_DIR="$(cd host_flatc_build/_deps/flatbuffers-build && pwd)"
cmake --build cmake_build_android_arm64_debug -j
Enabling GPU and NPU Accelerators
Toggle vendor delegate support by passing CMake definitions when configuring a custom build directory:
cmake -S . -B build-custom \
-DCMAKE_BUILD_TYPE=Release \
-DLITERT_ENABLE_GPU=ON \
-DLITERT_ENABLE_NPU=OFF
cmake --build build-custom -j
Valid options include LITERT_ENABLE_GPU, LITERT_ENABLE_NPU, and toolchain overrides detailed in lines 100-110 of CMAKE_BUILD_INSTRUCTIONS.md.
Building with Bazel (Google's Canonical Build System)
Bazel is the primary build system used internally at Google. It manages the runtime library, compiler tools, and benchmark utilities in a hermetic environment.
After installing Bazelisk (recommended) or Bazel 7.x or newer, run these commands from the repository root:
Build the C++ compiled-model API:
bazel build //litert/cc:litert_compiled_model
Build the benchmark utility:
bazel build //litert/tools:benchmark_model
Execute the benchmark to verify functionality:
./bazel-bin/litert/tools/benchmark_model \
--model=path/to/model.tflite \
--num_threads=4
Bazel outputs static libraries to bazel-bin/litert/cc/ and binaries to bazel-bin/litert/tools/, mirroring the source tree structure.
Verifying the Build
Confirm successful compilation by checking for these artifacts:
| Build Method | Release Library | Benchmark Binary |
|---|---|---|
| CMake | cmake_build/libLiteRtRuntime.a |
cmake_build/benchmark_model |
| Bazel | bazel-bin/litert/cc/liblitert_compiled_model.a |
bazel-bin/litert/tools/benchmark_model |
Run the benchmark binary against a known .tflite model (such as the MobileNet sample from the LiteRT samples repository) to ensure the runtime initializes correctly and executes inference without errors.
Summary
- Docker (
docker_build/build_with_docker.sh) provides the fastest, reproducible path for Linux and Android builds without local toolchain setup. - CMake (
litert/CMakePresets.json) supports flexible host builds and Android cross-compilation via presets likedefaultandandroid-arm64. - Bazel targets
//litert/cc:litert_compiled_modeland//litert/tools:benchmark_modelfor workflows aligned with Google's internal repository structure. - Set
ANDROID_NDK_HOMEandTFLITE_HOST_TOOLS_DIRwhen cross-compiling for Android with CMake. - Toggle hardware acceleration by setting
-DLITERT_ENABLE_GPU=ONorOFFduring CMake configuration.
Frequently Asked Questions
What is the fastest way to compile LiteRT for Android?
The Docker-based build is the fastest method. Running ./docker_build/build_with_docker.sh from the repository root automatically handles the Android NDK setup, cross-compilation toolchain, and produces both host and Android artifacts in cmake_build_android_arm64/ without requiring manual environment configuration.
Do I need Bazel to contribute to LiteRT?
No. While Bazel is Google's internal build system and required for certain upstream integrations, the CMake and Docker builds are fully supported first-class workflows. The litert/CMakePresets.json file contains officially maintained presets for all supported platforms.
How do I enable GPU acceleration when compiling from source?
Pass -DLITERT_ENABLE_GPU=ON to your CMake configuration command before building. For Bazel, GPU delegate support is typically enabled via build configuration flags in the .bazelrc file or command-line options specific to the target platform. Disable NPU support similarly using -DLITERT_ENABLE_NPU=OFF if your target device lacks neural processing hardware.
Where does the build output place the compiled static libraries?
CMake places libLiteRtRuntime.a in cmake_build/ (Release) or cmake_build_debug/ (Debug) depending on your preset. Bazel places liblitert_compiled_model.a in bazel-bin/litert/cc/ following the target path //litert/cc:litert_compiled_model.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →