Building SentencePiece with Bazel

August 30, 2026 ยท View on GitHub

In addition to the CMake build, SentencePiece can be built with Bazel (using Bzlmod).

Prerequisites

  • Bazel 7.2.1 or later (Bazel 7, 8, 9 supported; installing via Bazelisk is recommended)
  • A C++20 compatible compiler (GCC, Clang, or MSVC)

Building

Build all libraries, tools, and tests:

bazel build //...

Main Targets

TargetDescription
//:sentencepiece (or //src:sentencepiece_processor)Core runtime library (encode/decode/normalize)
//:sentencepiece_train (or //src:sentencepiece_trainer)Model training library
//src:libsentencepiece.soStandalone shared library (.so / .dylib / .dll)
//src:libsentencepiece_train.soStandalone trainer shared library
//:spm_trainModel trainer CLI
//:spm_encodeEncoder CLI
//:spm_decodeDecoder CLI
//:spm_normalizeText normalizer CLI
//:spm_export_vocabVocabulary exporter CLI
//:spm_evalEvaluation CLI
//:compile_charsmapUnicode normalization charsmap compiler CLI

Running CLIs Directly with Bazel

You can train and encode directly using bazel run:

# Train a model
bazel run //src:spm_train -- \
  --input=$(pwd)/data/botchan.txt \
  --model_prefix=$(pwd)/m \
  --vocab_size=1000

# Encode text
echo "Hello world." | bazel-bin/src/spm_encode --model=m.model

Running Unit Tests

Run all unit tests in parallel with caching:

# Run all 22 test suites in parallel
bazel test //...

# Or run the consolidated test suite matching CMake's spm_test
bazel test //src:spm_test --test_output=errors

Using SentencePiece in Another Bazel Project (Bzlmod)

Add SentencePiece to your MODULE.bazel:

bazel_dep(name = "sentencepiece", version = "0.2.3")

Then in your BUILD.bazel:

cc_binary(
    name = "my_app",
    srcs = ["main.cc"],
    deps = [
        "@sentencepiece//:sentencepiece",
        "@sentencepiece//:sentencepiece_train",  # if training support is needed
    ],
)