blob: efeab5829aa225cf3398dfb69c723e457de17c06 [file] [log] [blame] [view]
fweacae1cd2022-02-17 09:45:38 -08001Project: /_project.yaml
2Book: /_book.yaml
3
4# Compiling Bazel from Source
5
6
7This page describes how to install Bazel from source and provides
8troubleshooting tips for common issues.
9
10To build Bazel from source, you can do one of the following:
11
12* Build it [using an existing Bazel binary](#build-bazel-using-bazel)
13
14* Build it [without an existing Bazel binary](#bootstrap-bazel) which is known
15 as _bootstrapping_.
16
17## Build Bazel using Bazel {:#build-bazel-using-bazel}
18
19### Summary {:#summary}
20
211. Get the latest Bazel release from the
22 [GitHub release page](https://github.com/bazelbuild/bazel/releases){: .external} or with
23 [Bazelisk](https://github.com/bazelbuild/bazelisk){: .external}.
24
252. [Download Bazel's sources from GitHub](https://github.com/bazelbuild/bazel/archive/master.zip){: .external}
26 and extract somewhere.
27 Alternatively you can git clone the source tree from https://github.com/bazelbuild/bazel
28
293. Install the same prerequisites as for bootstrapping (see
30 [for Unix-like systems](#bootstrap-unix-prereq) or
31 [for Windows](#bootstrap-windows-prereq))
32
334. Build a development build of Bazel using Bazel:
34 `bazel build //src:bazel-dev` (or `bazel build //src:bazel-dev.exe` on
35 Windows)
36
375. The resulting binary is at `bazel-bin/src/bazel-dev`
38 (or `bazel-bin\src\bazel-dev.exe` on Windows). You can copy it wherever you
39 like and use immediately without further installation.
40
41Detailed instructions follow below.
42
43### Step 1: Get the latest Bazel release {:#build-bazel-install-bazel}
44
45**Goal**: Install or download a release version of Bazel. Make sure you can run
46it by typing `bazel` in a terminal.
47
48**Reason**: To build Bazel from a GitHub source tree, you need a pre-existing
49Bazel binary. You can install one from a package manager or download one from
fwe6c2bd4a2022-02-18 10:47:46 -080050GitHub. See [Installing Bazel](/install). (Or you can [build from
fweacae1cd2022-02-17 09:45:38 -080051scratch (bootstrap)](#bootstrap-bazel).)
52
53**Troubleshooting**:
54
55* If you cannot run Bazel by typing `bazel` in a terminal:
56
57 * Maybe your Bazel binary's directory is not on the PATH.
58
59 This is not a big problem. Instead of typing `bazel`, you will need to
60 type the full path.
61
62 * Maybe the Bazel binary itself is not called `bazel` (on Unixes) or
63 `bazel.exe` (on Windows).
64
65 This is not a big problem. You can either rename the binary, or type the
66 binary's name instead of `bazel`.
67
68 * Maybe the binary is not executable (on Unixes).
69
70 You must make the binary executable by running `chmod +x /path/to/bazel`.
71
72### Step 2: Download Bazel's sources from GitHub {:#build-bazel-git}
73
74If you are familiar with Git, then just git clone https://github.com/bazelbuild/bazel
75
76Otherwise:
77
781. Download the
79 [latest sources as a zip file](https://github.com/bazelbuild/bazel/archive/master.zip){: .external}.
80
812. Extract the contents somewhere.
82
83 For example create a `bazel-src` directory under your home directory and
84 extract there.
85
86### Step 3: Install prerequisites {:#build-bazel-prerequisites}
87
88Install the same prerequisites as for bootstrapping (see below) -- JDK, C++
89compiler, MSYS2 (if you are building on Windows), etc.
90
91### Step 4a: Build Bazel on Ubuntu Linux, macOS, and other Unix-like systems {:#build-bazel-on-unixes}
92
93For instructions for Windows, see [Build Bazel on Windows](#build-bazel-on-windows).
94
95**Goal**: Run Bazel to build a custom Bazel binary (`bazel-bin/src/bazel-dev`).
96
97**Instructions**:
98
991. Start a Bash terminal
100
1012. `cd` into the directory where you extracted (or cloned) Bazel's sources.
102
103 For example if you extracted the sources under your home directory, run:
104
105 cd ~/bazel-src
106
1073. Build Bazel from source:
108
109 bazel build //src:bazel-dev
110
111 Alternatively you can run `bazel build //src:bazel --compilation_mode=opt`
112 to yield a smaller binary but it's slower to build.
113
1144. The output will be at `bazel-bin/src/bazel-dev` (or `bazel-bin/src/bazel`).
115
116### Step 4b: Build Bazel on Windows {:#build-bazel-on-windows}
117
118For instructions for Unix-like systems, see
119[Ubuntu Linux, macOS, and other Unix-like systems](#build-bazel-on-unixes).
120
121**Goal**: Run Bazel to build a custom Bazel binary
122(`bazel-bin\src\bazel-dev.exe`).
123
124**Instructions**:
125
1261. Start Command Prompt (Start Menu > Run > "cmd.exe")
127
1282. `cd` into the directory where you extracted (or cloned) Bazel's sources.
129
130 For example if you extracted the sources under your home directory, run:
131
132 cd %USERPROFILE%\bazel-src
133
1343. Build Bazel from source:
135
136 bazel build //src:bazel-dev.exe
137
138 Alternatively you can run `bazel build //src:bazel.exe
139 --compilation_mode=opt` to yield a smaller binary but it's slower to build.
140
1414. The output will be at `bazel-bin\src\bazel-dev.exe` (or
142 `bazel-bin\src\bazel.exe`).
143
144### Step 5: Install the built binary {:#build-bazel-install}
145
146Actually, there's nothing to install.
147
148The output of the previous step is a self-contained Bazel binary. You can copy
149it to any directory and use immediately. (It's useful if that directory is on
150your PATH so that you can run "bazel" everywhere.)
151
152---
153
154## Build Bazel from scratch (bootstrapping) {:#bootstrap-bazel}
155
156You can also build Bazel from scratch, without using an existing Bazel binary.
157
158### Step 1: Download Bazel's sources (distribution archive) {:#download-distfile}
159
160(This step is the same for all platforms.)
161
1621. Download `bazel-<version>-dist.zip` from
163 [GitHub](https://github.com/bazelbuild/bazel/releases){: .external}, for example
164 `bazel-0.28.1-dist.zip`.
165
166 **Attention**:
167
168 - There is a **single, architecture-independent** distribution archive.
169 There are no architecture-specific or OS-specific distribution archives.
170 - These sources are **not the same as the GitHub source tree**. You
171 have to use the distribution archive to bootstrap Bazel. You cannot
172 use a source tree cloned from GitHub. (The distribution archive contains
173 generated source files that are required for bootstrapping and are not part
174 of the normal Git source tree.)
175
1762. Unpack the distribution archive somewhere on disk.
177
178 You should verify the signature made by Bazel's
179 [release key](https://bazel.build/bazel-release.pub.gpg) 3D5919B448457EE0.
180
181### Step 2a: Bootstrap Bazel on Ubuntu Linux, macOS, and other Unix-like systems {:#bootstrap-unix-overview}
182
183For instructions for Windows, see [Bootstrap Bazel on Windows](#bootstrap-windows).
184
185#### 2.1. Install the prerequisites {:#bootstrap-unix-prereq}
186
187* **Bash**
188
189* **zip, unzip**
190
191* **C++ build toolchain**
192
193* **JDK.** Version 11 is required.
194
195* **Python**. Versions 2 and 3 are supported, installing one of them is
196 enough.
197
198For example on Ubuntu Linux you can install these requirements using the
199following command:
200
201```sh
202sudo apt-get install build-essential openjdk-11-jdk python zip unzip
203```
204
205#### 2.2. Bootstrap Bazel on Unix {:#bootstrap-unix}
206
2071. Open a shell or Terminal window.
208
2093. `cd` to the directory where you unpacked the distribution archive.
210
Ivo List2f9ed1c2022-03-16 04:41:53 -07002113. Run the compilation script: `env EXTRA_BAZEL_ARGS="--tool_java_runtime_version=local_jdk" bash ./compile.sh`.
fweacae1cd2022-02-17 09:45:38 -0800212
213The compiled output is placed into `output/bazel`. This is a self-contained
214Bazel binary, without an embedded JDK. You can copy it anywhere or use it
215in-place. For convenience, copy this binary to a directory that's on your
216`PATH` (such as `/usr/local/bin` on Linux).
217
218To build the `bazel` binary in a reproducible way, also set
219[`SOURCE_DATE_EPOCH`](https://reproducible-builds.org/specs/source-date-epoch/)
220in the "Run the compilation script" step.
221
222### Step 2b: Bootstrap Bazel on Windows {:#bootstrap-windows-overview}
223
224For instructions for Unix-like systems, see
225[Bootstrap Bazel on Ubuntu Linux, macOS, and other Unix-like systems](#bootstrap-unix).
226
227#### 2.1. Install the prerequisites {:#bootstrap-windows-prereq}
228
229* [MSYS2 shell](https://msys2.github.io/)
230
231* **The MSYS2 packages for zip and unzip.** Run the following command in the MSYS2 shell:
232
233 ```
234 pacman -S zip unzip patch
235 ```
236
237* **The Visual C++ compiler.** Install the Visual C++ compiler either as part
238 of Visual Studio 2015 or newer, or by installing the latest [Build Tools
239 for Visual Studio 2017](https://aka.ms/BuildTools).
240
241* **JDK 8.** You must install version 8 of the JDK. Versions other than 8 are
242 *not* supported.
243
244* **Python**. Versions 2 and 3 are supported, installing one of them is
245 enough. You need the Windows-native version (downloadable from
246 [https://www.python.org](https://www.python.org)). Versions installed via
247 pacman in MSYS2 will not work.
248
249#### 2.2. Bootstrap Bazel on Windows {:#bootstrap-windows}
250
2511. Open the MSYS2 shell.
252
2532. Set the following environment variables:
254 * Either `BAZEL_VS` or `BAZEL_VC` (they are *not* the same): Set to the
255 path to the Visual Studio directory (BAZEL\_V<b>S</b>) or to the Visual
256 C++ directory (BAZEL\_V<b>C</b>). Setting one of them is enough.
257 * `BAZEL_SH`: Path of the MSYS2 `bash.exe`. See the command in the
258 examples below.
259
260 Do not set this to `C:\Windows\System32\bash.exe`. (You have that file
261 if you installed Windows Subsystem for Linux.) Bazel does not support
262 this version of `bash.exe`.
263 * `PATH`: Add the Python directory.
264 * `JAVA_HOME`: Set to the JDK directory.
265
266 **Example** (using BAZEL\_V<b>S</b>):
267
268 export BAZEL_VS="C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools"
269 export BAZEL_SH="$(cygpath -m $(realpath $(which bash)))"
270 export PATH="/c/python27:$PATH"
271 export JAVA_HOME="C:/Program Files/Java/jdk1.8.0_112"
272
273 or (using BAZEL\_V<b>C</b>):
274
275 export BAZEL_VC="C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/VC"
276 export BAZEL_SH="$(cygpath -m $(realpath $(which bash)))"
277 export PATH="/c/python27:$PATH"
278 export JAVA_HOME="C:/Program Files/Java/jdk1.8.0_112"
279
2803. `cd` to the directory where you unpacked the distribution archive.
281
Ivo List2f9ed1c2022-03-16 04:41:53 -07002824. Run the compilation script: `env EXTRA_BAZEL_ARGS="--tool_java_runtime_version=local_jdk" ./compile.sh`
fweacae1cd2022-02-17 09:45:38 -0800283
284The compiled output is placed into `output/bazel.exe`. This is a self-contained
285Bazel binary, without an embedded JDK. You can copy it anywhere or use it
286in-place. For convenience, copy this binary to a directory that's on
287your `PATH`.
288
289To build the `bazel.exe` binary in a reproducible way, also set
290[`SOURCE_DATE_EPOCH`](https://reproducible-builds.org/specs/source-date-epoch/)
291in the "Run the compilation script" step.
292
293You don't need to run Bazel from the MSYS2 shell. You can run Bazel from the
294Command Prompt (`cmd.exe`) or PowerShell.