blob: 6b834da2ad75b60d2e963880a37e69ed264624c9 [file] [log] [blame] [view]
Googler1fbef392017-03-22 09:05:09 +00001---
2layout: documentation
Laszlo Csomorebb902e2018-10-22 00:56:43 -07003title: Compiling Bazel from source
daroberts223aebd2021-02-18 17:53:30 -08004category: getting-started
Googler1fbef392017-03-22 09:05:09 +00005---
6
Googlerde981772021-01-08 18:41:01 -08007<h1 id="compiling-from-source">Compiling Bazel from Source</h1>
Googler1fbef392017-03-22 09:05:09 +00008
Googler10eddc32020-12-21 18:46:12 -08009This page describes how to install Bazel from source and provides
10troubleshooting tips for common issues.
11
laszlocsomor810d2362018-09-17 09:35:09 -070012To build Bazel from source, you can do one of the following:
13
Laszlo Csomorebb902e2018-10-22 00:56:43 -070014* Build it [using an existing Bazel binary](#build-bazel-using-bazel)
laszlocsomor810d2362018-09-17 09:35:09 -070015
Laszlo Csomorebb902e2018-10-22 00:56:43 -070016* Build it [without an existing Bazel binary](#bootstrap-bazel) which is known
Zackary Lowery81183892019-10-30 08:14:39 -070017 as _bootstrapping_.
laszlocsomor810d2362018-09-17 09:35:09 -070018
Laszlo Csomorebb902e2018-10-22 00:56:43 -070019<h2 id="build-bazel-using-bazel">Build Bazel using Bazel</h2>
steren30c35a22017-06-30 15:49:47 +020020
Googler7732c4c2021-01-15 17:13:49 -080021<h3 id="summary">Summary</h3>
Googler1fbef392017-03-22 09:05:09 +000022
Jingwen Chen7c15c032020-01-16 11:54:44 -0800231. Get the latest Bazel release from the
24 [GitHub release page](https://github.com/bazelbuild/bazel/releases) or with
25 [Bazelisk](https://github.com/bazelbuild/bazelisk).
Laszlo Csomor6532ecc2018-05-04 01:21:54 -070026
Laszlo Csomor7d479872019-08-16 07:00:04 -0700272. [Download Bazel's sources from GitHub](https://github.com/bazelbuild/bazel/archive/master.zip)
28 and extract somewhere.
29 Alternatively you can git clone the source tree from https://github.com/bazelbuild/bazel
Laszlo Csomorebb902e2018-10-22 00:56:43 -070030
Laszlo Csomor7d479872019-08-16 07:00:04 -0700313. Install the same prerequisites as for bootstrapping (see
32 [for Unix-like systems](#bootstrap-unix-prereq) or
33 [for Windows](#bootstrap-windows-prereq))
Laszlo Csomorebb902e2018-10-22 00:56:43 -070034
Jingwen Chen7c15c032020-01-16 11:54:44 -0800354. Build a development build of Bazel using Bazel:
36 `bazel build //src:bazel-dev` (or `bazel build //src:bazel-dev.exe` on
37 Windows)
Laszlo Csomorebb902e2018-10-22 00:56:43 -070038
Jingwen Chen7c15c032020-01-16 11:54:44 -0800395. The resulting binary is at `bazel-bin/src/bazel-dev`
Laszlo Csomor7d479872019-08-16 07:00:04 -070040 (or `bazel-bin\src\bazel-dev.exe` on Windows). You can copy it wherever you
41 like and use immediately without further installation.
42
43Detailed instructions follow below.
44
Greg Estrend2b063e2020-02-20 12:33:22 -080045<h3 id="build-bazel-install-bazel">Step 1: Get the latest Bazel release</h3>
Laszlo Csomor7d479872019-08-16 07:00:04 -070046
47**Goal**: Install or download a release version of Bazel. Make sure you can run
48it by typing `bazel` in a terminal.
49
50**Reason**: To build Bazel from a GitHub source tree, you need a pre-existing
51Bazel binary. You can install one from a package manager or download one from
52GitHub. See <a href="install.html">Installing Bazel</a>. (Or you can [build from
53scratch (bootstrap)](#bootstrap-bazel).)
54
55**Troubleshooting**:
56
57* If you cannot run Bazel by typing `bazel` in a terminal:
58
59 * Maybe your Bazel binary's directory is not on the PATH.
60
61 This is not a big problem. Instead of typing `bazel`, you will need to
62 type the full path.
63
64 * Maybe the Bazel binary itself is not called `bazel` (on Unixes) or
65 `bazel.exe` (on Windows).
66
67 This is not a big problem. You can either rename the binary, or type the
68 binary's name instead of `bazel`.
69
70 * Maybe the binary is not executable (on Unixes).
71
72 You must make the binary executable by running `chmod +x /path/to/bazel`.
73
Greg Estrend2b063e2020-02-20 12:33:22 -080074<h3 id="build-bazel-git">Step 2: Download Bazel's sources from GitHub</h3>
Laszlo Csomor7d479872019-08-16 07:00:04 -070075
76If you are familiar with Git, then just git clone https://github.com/bazelbuild/bazel
77
78Otherwise:
79
801. Download the
81 [latest sources as a zip file](https://github.com/bazelbuild/bazel/archive/master.zip).
82
832. Extract the contents somewhere.
84
85 For example create a `bazel-src` directory under your home directory and
86 extract there.
87
Greg Estrend2b063e2020-02-20 12:33:22 -080088<h3 id="build-bazel-prerequisites">Step 3: Install prerequisites</h3>
Laszlo Csomor7d479872019-08-16 07:00:04 -070089
90Install the same prerequisites as for bootstrapping (see below) -- JDK, C++
91compiler, MSYS2 (if you are building on Windows), etc.
92
Greg Estrend2b063e2020-02-20 12:33:22 -080093<h3 id="build-bazel-on-unixes">Step 4: Build Bazel</h3>
94
95<h4 id="build-bazel-on-unixes">Ubuntu Linux, macOS, and other Unix-like systems</h4>
Laszlo Csomor7d479872019-08-16 07:00:04 -070096
97([Scroll down](#build-bazel-on-windows) for instructions for Windows.)
98
Jingwen Chen7c15c032020-01-16 11:54:44 -080099**Goal**: Run Bazel to build a custom Bazel binary (`bazel-bin/src/bazel-dev`).
Laszlo Csomor7d479872019-08-16 07:00:04 -0700100
101**Instructions**:
102
1031. Start a Bash terminal
104
1052. `cd` into the directory where you extracted (or cloned) Bazel's sources.
106
107 For example if you extracted the sources under your home directory, run:
108
109 cd ~/bazel-src
110
1113. Build Bazel from source:
112
Jingwen Chen7c15c032020-01-16 11:54:44 -0800113 bazel build //src:bazel-dev
Laszlo Csomor7d479872019-08-16 07:00:04 -0700114
Jingwen Chen7c15c032020-01-16 11:54:44 -0800115 Alternatively you can run `bazel build //src:bazel --compilation_mode=opt`
116 to yield a smaller binary but it's slower to build.
117
1184. The output will be at `bazel-bin/src/bazel-dev` (or `bazel-bin/src/bazel`).
Laszlo Csomor7d479872019-08-16 07:00:04 -0700119
Greg Estrend2b063e2020-02-20 12:33:22 -0800120<h4 id="build-bazel-on-windows">Windows</h4>
Laszlo Csomor7d479872019-08-16 07:00:04 -0700121
122([Scroll up](#build-bazel-on-unixes) for instructions for Linux, macOS, and other
123Unix-like systems.)
124
125**Goal**: Run Bazel to build a custom Bazel binary
126(`bazel-bin\src\bazel-dev.exe`).
127
128**Instructions**:
129
1301. Start Command Prompt (Start Menu &gt; Run &gt; "cmd.exe")
131
1322. `cd` into the directory where you extracted (or cloned) Bazel's sources.
133
134 For example if you extracted the sources under your home directory, run:
135
136 cd %USERPROFILE%\bazel-src
137
1383. Build Bazel from source:
139
140 bazel build //src:bazel-dev.exe
141
Jingwen Chen7c15c032020-01-16 11:54:44 -0800142 Alternatively you can run `bazel build //src:bazel.exe
143 --compilation_mode=opt` to yield a smaller binary but it's slower to build.
Laszlo Csomor7d479872019-08-16 07:00:04 -0700144
Jingwen Chen7c15c032020-01-16 11:54:44 -08001454. The output will be at `bazel-bin\src\bazel-dev.exe` (or
146 `bazel-bin\src\bazel.exe`).
Laszlo Csomor7d479872019-08-16 07:00:04 -0700147
Greg Estrend2b063e2020-02-20 12:33:22 -0800148<h3 id="build-bazel-install">Step 5: Install the built binary</h3>
Laszlo Csomor7d479872019-08-16 07:00:04 -0700149
150Actually, there's nothing to install.
151
152The output of the previous step is a self-contained Bazel binary. You can copy
153it to any directory and use immediately. (It's useful if that directory is on
154your PATH so that you can run "bazel" everywhere.)
155
156---
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700157
158<h2 id="bootstrap-bazel">Build Bazel from scratch (bootstrapping)</h2>
159
160You can also build Bazel from scratch, without using an existing Bazel binary.
161
Greg Estrend2b063e2020-02-20 12:33:22 -0800162<h3 id="download-distfile">Step 1: Download Bazel's sources (distribution archive)</h3>
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700163
164(This step is the same for all platforms.)
165
1661. Download `bazel-<version>-dist.zip` from
167 [GitHub](https://github.com/bazelbuild/bazel/releases), for example
Laszlo Csomor7d479872019-08-16 07:00:04 -0700168 `bazel-0.28.1-dist.zip`.
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700169
Laszlo Csomor7d479872019-08-16 07:00:04 -0700170 **Attention**:
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700171
Laszlo Csomor7d479872019-08-16 07:00:04 -0700172 - There is a **single, architecture-independent** distribution archive.
173 There are no architecture-specific or OS-specific distribution archives.
174 - These sources are **not the same as the GitHub source tree**. You
175 have to use the distribution archive to bootstrap Bazel. You cannot
176 use a source tree cloned from GitHub. (The distribution archive contains
177 generated source files that are required for bootstrapping and are not part
178 of the normal Git source tree.)
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700179
Laszlo Csomor7d479872019-08-16 07:00:04 -07001802. Unpack the distribution archive somewhere on disk.
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700181
Googler480d83e2021-02-26 17:24:10 -0800182 You should verify the signature made by Bazel's
Klaus Aehlig552cf562019-10-11 04:20:41 -0700183 [release key](https://bazel.build/bazel-release.pub.gpg) 3D5919B448457EE0.
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700184
Greg Estrend2b063e2020-02-20 12:33:22 -0800185<h3 id="bootstrap-unix">Step 2a: Bootstrap Bazel on Ubuntu Linux, macOS, and other Unix-like systems</h3>
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700186
187([Scroll down](#bootstrap-windows) for instructions for Windows.)
188
189<h4 id="bootstrap-unix-prereq">2.1. Install the prerequisites</h4>
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700190
Laszlo Csomor4691ad92018-05-14 01:58:00 -0700191* **Bash**
192
193* **zip, unzip**
194
195* **C++ build toolchain**
196
wyva9de41c2020-05-11 07:23:47 -0700197* **JDK.** Versions 8 and 11 are supported.
Laszlo Csomor4691ad92018-05-14 01:58:00 -0700198
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700199* **Python**. Versions 2 and 3 are supported, installing one of them is
200 enough.
Laszlo Csomor4691ad92018-05-14 01:58:00 -0700201
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700202For example on Ubuntu Linux you can install these requirements using the
203following command:
Googler1fbef392017-03-22 09:05:09 +0000204
Serge70e84f82018-04-18 10:04:22 -0700205```sh
wyva9de41c2020-05-11 07:23:47 -0700206sudo apt-get install build-essential openjdk-11-jdk python zip unzip
Serge70e84f82018-04-18 10:04:22 -0700207```
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700208
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700209<h4 id="bootstrap-unix-bootstrap">2.2. Bootstrap Bazel</h4>
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700210
Laszlo Csomorebb902e2018-10-22 00:56:43 -07002111. Open a shell or Terminal window.
212
2133. `cd` to the directory where you unpacked the distribution archive.
214
Klaus Aehlig004bcb42019-01-24 03:53:32 -08002153. Run the compilation script: `env EXTRA_BAZEL_ARGS="--host_javabase=@local_jdk//:jdk" bash ./compile.sh`.
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700216
217The compiled output is placed into `output/bazel`. This is a self-contained
218Bazel binary, without an embedded JDK. You can copy it anywhere or use it
Googler480d83e2021-02-26 17:24:10 -0800219in-place. For convenience, copy this binary to a directory that's on your
220`PATH` (such as `/usr/local/bin` on Linux).
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700221
Klaus Aehligfc996af2019-02-19 07:21:28 -0800222To build the `bazel` binary in a reproducible way, also set
223[`SOURCE_DATE_EPOCH`](https://reproducible-builds.org/specs/source-date-epoch/)
Laszlo Csomor7d479872019-08-16 07:00:04 -0700224in the "Run the compilation script" step.
Klaus Aehligfc996af2019-02-19 07:21:28 -0800225
Greg Estrend2b063e2020-02-20 12:33:22 -0800226<h3 id="bootstrap-windows">Step 2b: Bootstrap Bazel on Windows</h3>
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700227
228([Scroll up](#bootstrap-unix) for instructions for Linux, macOS, and other
229Unix-like systems.)
230
231<h4 id="bootstrap-windows-prereq">2.1. Install the prerequisites</h4>
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700232
233* [MSYS2 shell](https://msys2.github.io/)
234
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700235* **The MSYS2 packages for zip and unzip.** Run the following command in the MSYS2 shell:
Laszlo Csomorab2431e2018-05-29 05:15:42 -0700236
237 ```
laszlocsomorf6696b82019-04-29 01:02:51 -0700238 pacman -S zip unzip patch
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700239 ```
240
241* **The Visual C++ compiler.** Install the Visual C++ compiler either as part
242 of Visual Studio 2015 or newer, or by installing the latest [Build Tools
243 for Visual Studio 2017](https://aka.ms/BuildTools).
244
245* **JDK 8.** You must install version 8 of the JDK. Versions other than 8 are
246 *not* supported.
247
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700248* **Python**. Versions 2 and 3 are supported, installing one of them is
249 enough. You need the Windows-native version (downloadable from
250 [https://www.python.org](https://www.python.org)). Versions installed via
251 pacman in MSYS2 will not work.
Googler1fbef392017-03-22 09:05:09 +0000252
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700253<h4 id="bootstrap-windows-bootstrap">2.2. Bootstrap Bazel</h4>
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700254
2551. Open the MSYS2 shell.
256
2572. Set the following environment variables:
Laszlo Csomora7e0c752018-06-10 23:24:26 -0700258 * Either `BAZEL_VS` or `BAZEL_VC` (they are *not* the same): Set to the
259 path to the Visual Studio directory (BAZEL\_V<b>S</b>) or to the Visual
260 C++ directory (BAZEL\_V<b>C</b>). Setting one of them is enough.
Laszlo Csomor75748652018-07-26 07:38:14 -0700261 * `BAZEL_SH`: Path of the MSYS2 `bash.exe`. See the command in the
262 examples below.
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700263
264 Do not set this to `C:\Windows\System32\bash.exe`. (You have that file
265 if you installed Windows Subsystem for Linux.) Bazel does not support
266 this version of `bash.exe`.
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700267 * `PATH`: Add the Python directory.
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700268 * `JAVA_HOME`: Set to the JDK directory.
269
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700270 **Example** (using BAZEL\_V<b>S</b>):
Laszlo Csomor4691ad92018-05-14 01:58:00 -0700271
272 export BAZEL_VS="C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools"
Laszlo Csomor75748652018-07-26 07:38:14 -0700273 export BAZEL_SH="$(cygpath -m $(realpath $(which bash)))"
Laszlo Csomor4691ad92018-05-14 01:58:00 -0700274 export PATH="/c/python27:$PATH"
275 export JAVA_HOME="C:/Program Files/Java/jdk1.8.0_112"
276
Laszlo Csomora7e0c752018-06-10 23:24:26 -0700277 or (using BAZEL\_V<b>C</b>):
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700278
Laszlo Csomor4691ad92018-05-14 01:58:00 -0700279 export BAZEL_VC="C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/VC"
Laszlo Csomor75748652018-07-26 07:38:14 -0700280 export BAZEL_SH="$(cygpath -m $(realpath $(which bash)))"
Laszlo Csomor4691ad92018-05-14 01:58:00 -0700281 export PATH="/c/python27:$PATH"
282 export JAVA_HOME="C:/Program Files/Java/jdk1.8.0_112"
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700283
Laszlo Csomorebb902e2018-10-22 00:56:43 -07002843. `cd` to the directory where you unpacked the distribution archive.
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700285
Laszlo Csomor7d479872019-08-16 07:00:04 -07002864. Run the compilation script: `env EXTRA_BAZEL_ARGS="--host_javabase=@local_jdk//:jdk" ./compile.sh`
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700287
288The compiled output is placed into `output/bazel.exe`. This is a self-contained
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700289Bazel binary, without an embedded JDK. You can copy it anywhere or use it
Googler480d83e2021-02-26 17:24:10 -0800290in-place. For convenience, copy this binary to a directory that's on
291your `PATH`.
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700292
Laszlo Csomor7d479872019-08-16 07:00:04 -0700293To build the `bazel.exe` binary in a reproducible way, also set
294[`SOURCE_DATE_EPOCH`](https://reproducible-builds.org/specs/source-date-epoch/)
295in the "Run the compilation script" step.
296
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700297You don't need to run Bazel from the MSYS2 shell. You can run Bazel from the
298Command Prompt (`cmd.exe`) or PowerShell.