Skip to content

Bazel

What is Bazel – Tutorial, Examples, and Advantages

Bazel is an open-source build tool developed by Google to automate build processes for large-scale software. Companies such as Pinterest, Adobe, SpaceX, Nvidia, and LinkedIn use it, amongst others. In this tutorial, you’ll understand what Bazel is, how it works, and its important benefits. You’ll also learn how you can generate Bazel builds for your monorepo project.

How to install Bazel on Ubuntu

Step 1: Add Bazel distribution URI as a package source

# Setup package source
$ sudo apt install apt-transport-https curl gnupg -y
$ curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor >bazel-archive-keyring.gpg
$ sudo mv bazel-archive-keyring.gpg /usr/share/keyrings
$ echo "deb [arch=amd64 signed-by=/usr/share/keyrings/bazel-archive-keyring.gpg] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list

Step 2: Install and update Bazel

$ sudo apt update && sudo apt install bazel

Once installed, you can upgrade to a newer version of Bazel as part of your normal system updates:

$ sudo apt update && sudo apt full-upgrade

How to use Bazel

1. Project Structure

Bazel organizes code into packages. Each package is a directory with a BUILD file describing how to build its code.

Example structure:

my_project/
├── WORKSPACE
├── main.cpp
└── BUILD

  • WORKSPACE: Marks the root of your Bazel project.
  • BUILD: Contains build rules for the files in the directory.

2. Writing a Simple BUILD File

Suppose you have a simple C++ program:

main.cpp

#include <iostream>
int main() {
    std::cout << "Hello, Bazel!" << std::endl;
    return 0;
}

BUILD

cc_binary(
    name = "hello_bazel",
    srcs = ["main.cpp"],
)

  • cc_binary: Rule to build a C++ binary.
  • name: The name of the output binary.
  • srcs: Source files.

3. Building and Running

Build the binary:

$ bazel build //:hello_bazel
- Output will be in bazel-bin/hello_bazel

Run the binary:

$ bazel-bin/hello_bazel

Or use Bazel to run directly:

$ bazel run //:hello_bazel


4. Adding Dependencies

Suppose you have another file, helper.cpp and helper.h:

helper.h

#pragma once
int add(int a, int b);

helper.cpp

#include "helper.h"
int add(int a, int b) { return a + b; }

Update your BUILD file:

cc_library(
    name = "helper",
    srcs = ["helper.cpp"],
    hdrs = ["helper.h"],
)

cc_binary(
    name = "hello_bazel",
    srcs = ["main.cpp"],
    deps = [":helper"],
)


5. Cleaning the Build

To clean up build outputs:

$ bazel clean


6. Advantages of Bazel

  • Speed: Incremental builds and caching make repeated builds fast.
  • Scalability: Handles very large codebases and monorepos.
  • Reproducibility: Builds are hermetic and consistent across environments.
  • Multi-language: Supports C++, Java, Python, Go, and more.
  • Extensibility: Custom rules for new languages or tools.

7. Further Resources


Summary

Bazel is a powerful, fast, and extensible build tool ideal for modern software projects. With simple configuration files and strong support for large codebases, it’s a great choice for both small and enterprise teams.