Compiling Bazel from source (original) (raw)

This page describes how to install Bazel from source and provides troubleshooting tips for common issues.

To build Bazel from source, you can do one of the following:

Build Bazel using Bazel

Summary

  1. Get the latest Bazel release from theGitHub release page or withBazelisk.
  2. Download Bazel’s sources from GitHuband extract somewhere. Alternatively you can git clone the source tree from https://github.com/bazelbuild/bazel
  3. Install the same prerequisites as for bootstrapping (seefor Unix-like systems orfor Windows)
  4. Build a development build of Bazel using Bazel:bazel build //src:bazel-dev (or bazel build //src:bazel-dev.exe on Windows)
  5. The resulting binary is at bazel-bin/src/bazel-dev(or bazel-bin\src\bazel-dev.exe on Windows). You can copy it wherever you like and use immediately without further installation.

Detailed instructions follow below.

Step 1: Get the latest Bazel release

Goal: Install or download a release version of Bazel. Make sure you can run it by typing bazel in a terminal.

Reason: To build Bazel from a GitHub source tree, you need a pre-existing Bazel binary. You can install one from a package manager or download one from GitHub. See Installing Bazel. (Or you can build from scratch (bootstrap).)

Troubleshooting:

Step 2: Download Bazel's sources from GitHub

If you are familiar with Git, then just git clone https://github.com/bazelbuild/bazel

Otherwise:

  1. Download thelatest sources as a zip file.
  2. Extract the contents somewhere.
    For example create a bazel-src directory under your home directory and extract there.

Step 3: Install prerequisites

Install the same prerequisites as for bootstrapping (see below) – JDK, C++ compiler, MSYS2 (if you are building on Windows), etc.

Step 4: Build Bazel

Ubuntu Linux, macOS, and other Unix-like systems

(Scroll down for instructions for Windows.)

Goal: Run Bazel to build a custom Bazel binary (bazel-bin/src/bazel-dev).

Instructions:

  1. Start a Bash terminal
  2. cd into the directory where you extracted (or cloned) Bazel’s sources.
    For example if you extracted the sources under your home directory, run:
  3. Build Bazel from source:
bazel build //src:bazel-dev  

Alternatively you can run bazel build //src:bazel --compilation_mode=optto yield a smaller binary but it’s slower to build. 4. The output will be at bazel-bin/src/bazel-dev (or bazel-bin/src/bazel).

Windows

(Scroll up for instructions for Linux, macOS, and other Unix-like systems.)

Goal: Run Bazel to build a custom Bazel binary (bazel-bin\src\bazel-dev.exe).

Instructions:

  1. Start Command Prompt (Start Menu > Run > “cmd.exe”)
  2. cd into the directory where you extracted (or cloned) Bazel’s sources.
    For example if you extracted the sources under your home directory, run:
cd %USERPROFILE%\bazel-src  
  1. Build Bazel from source:
bazel build //src:bazel-dev.exe  

Alternatively you can run bazel build //src:bazel.exe --compilation_mode=opt to yield a smaller binary but it’s slower to build. 4. The output will be at bazel-bin\src\bazel-dev.exe (orbazel-bin\src\bazel.exe).

Step 5: Install the built binary

Actually, there’s nothing to install.

The output of the previous step is a self-contained Bazel binary. You can copy it to any directory and use immediately. (It’s useful if that directory is on your PATH so that you can run “bazel” everywhere.)


Build Bazel from scratch (bootstrapping)

You can also build Bazel from scratch, without using an existing Bazel binary.

Step 1: Download Bazel's sources (distribution archive)

(This step is the same for all platforms.)

  1. Download bazel-<version>-dist.zip fromGitHub, for examplebazel-0.28.1-dist.zip.
    Attention:
    • There is a single, architecture-independent distribution archive. There are no architecture-specific or OS-specific distribution archives.
    • These sources are not the same as the GitHub source tree. You have to use the distribution archive to bootstrap Bazel. You cannot use a source tree cloned from GitHub. (The distribution archive contains generated source files that are required for bootstrapping and are not part of the normal Git source tree.)
  2. Unpack the distribution archive somewhere on disk.
    You should verify the signature made by Bazel’srelease key 3D5919B448457EE0.

Step 2a: Bootstrap Bazel on Ubuntu Linux, macOS, and other Unix-like systems

(Scroll down for instructions for Windows.)

2.1. Install the prerequisites

For example on Ubuntu Linux you can install these requirements using the following command:

sudo apt-get install build-essential openjdk-11-jdk python zip unzip

2.2. Bootstrap Bazel

  1. Open a shell or Terminal window.
  2. cd to the directory where you unpacked the distribution archive.
  3. Run the compilation script: env EXTRA_BAZEL_ARGS="--host_javabase=@local_jdk//:jdk" bash ./compile.sh.

The compiled output is placed into output/bazel. This is a self-contained Bazel binary, without an embedded JDK. You can copy it anywhere or use it in-place. For convenience, copy this binary to a directory that’s on yourPATH (such as /usr/local/bin on Linux).

To build the bazel binary in a reproducible way, also setSOURCE_DATE_EPOCHin the “Run the compilation script” step.

Step 2b: Bootstrap Bazel on Windows

(Scroll up for instructions for Linux, macOS, and other Unix-like systems.)

2.1. Install the prerequisites

pacman -S zip unzip patch  

2.2. Bootstrap Bazel

  1. Open the MSYS2 shell.
  2. Set the following environment variables:
    • Either BAZEL_VS or BAZEL_VC (they are not the same): Set to the path to the Visual Studio directory (BAZEL_VS) or to the Visual C++ directory (BAZEL_VC). Setting one of them is enough.
    • BAZEL_SH: Path of the MSYS2 bash.exe. See the command in the examples below.
      Do not set this to C:\Windows\System32\bash.exe. (You have that file if you installed Windows Subsystem for Linux.) Bazel does not support this version of bash.exe.
    • PATH: Add the Python directory.
    • JAVA_HOME: Set to the JDK directory.
      Example (using BAZEL_VS):
export BAZEL_VS="C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools"  
export BAZEL_SH="$(cygpath -m <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>r</mi><mi>e</mi><mi>a</mi><mi>l</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>h</mi></mrow><annotation encoding="application/x-tex">(realpath </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">re</span><span class="mord mathnormal">a</span><span class="mord mathnormal">lp</span><span class="mord mathnormal">a</span><span class="mord mathnormal">t</span><span class="mord mathnormal">h</span></span></span></span>(which bash)))"  
export PATH="/c/python27:$PATH"  
export JAVA_HOME="C:/Program Files/Java/jdk1.8.0_112"  

or (using BAZEL_VC):

export BAZEL_VC="C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/VC"  
export BAZEL_SH="$(cygpath -m <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>r</mi><mi>e</mi><mi>a</mi><mi>l</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>h</mi></mrow><annotation encoding="application/x-tex">(realpath </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">re</span><span class="mord mathnormal">a</span><span class="mord mathnormal">lp</span><span class="mord mathnormal">a</span><span class="mord mathnormal">t</span><span class="mord mathnormal">h</span></span></span></span>(which bash)))"  
export PATH="/c/python27:$PATH"  
export JAVA_HOME="C:/Program Files/Java/jdk1.8.0_112"  
  1. cd to the directory where you unpacked the distribution archive.
  2. Run the compilation script: env EXTRA_BAZEL_ARGS="--host_javabase=@local_jdk//:jdk" ./compile.sh

The compiled output is placed into output/bazel.exe. This is a self-contained Bazel binary, without an embedded JDK. You can copy it anywhere or use it in-place. For convenience, copy this binary to a directory that’s on your PATH.

To build the bazel.exe binary in a reproducible way, also setSOURCE_DATE_EPOCHin the “Run the compilation script” step.

You don’t need to run Bazel from the MSYS2 shell. You can run Bazel from the Command Prompt (cmd.exe) or PowerShell.