Building with Bazel
Jul 8 2022 · Starlark, Bazel 5.1, Visual Studo Code 1.66
Part 1: Learning Bazel
02. Install Bazel
Leave a rating/review
Notes: 02. Install Bazel
Episode 2 - Install Bazel
Every journey starts with a first step and Bazel is no exception.
We’re going to get Bazel up and running on our system. I will be showing how to install Bazel on Windows, macOS and Linux. For Windows, I’ll be using a Windows 10 system. I’ll be installing Bazel on macOS Monterey and for Linux, I’ll just be using Ubuntu. While Bazel works the same for all environments, you may run into some gotchas depending on the platform.
For example, in Bazel, if you want to build all targets in a workspace, you would type
bazel build ....
Unfortunately, this little shortcut confuses macOS since the
... is used in the zshell to navigate up a few directories.
This can be resolved by typing
bazel build //.... Windows also runs into some command line issues. If you are following any documentation and the commands don’t appear to work, you may be running into one of these command line issues.
Finally, Bazel has many different versions. Of course, different versions of Bazel may introduce complications. For instance, you may incorporate a new Bazel feature in your builds, but this project would no longer be able to compile on older versions of Bazel.
There’s a tool called Bazelisk that manages your Bazel installs. That way, you can switch between versions depending on the project. Bazelisk acts as a wrapper for Bazel so all the commands on Bazel work for Bazelisk. By adding a .bazelversion file, you actually define the bazel version used to compile your. That way, if you try to compile your project using Bazelisk, Bazelisk will automatically download the Bazel version defined in in the .bazelversion file and compile your app.
We’ll be using Bazel throughout this course, but if you do find yourself needing to switch between Bazel versions, then Bazelisk is the a must use tool.
To get started with Windows, I first need to install Visual C++ Redistributable for Visual Studio 2015. Open up a browser and head to the following link:
Now download then install dependency. Next, head over to Bazel’s releases on the official Bazel GitHub repo. Type in the following:
At the time of this course, Bazel is using 5.1 so scroll down to that release. Download the exe file. Then install. Okay, when installed, find the Bazel install directory. Name the Bazel binary to bazel.exe. At this point, we need to access Bazel on the command line. Open up control panel. Under system properties, advanced, click environment variables button. Now add a path to your bazel install.
Open a command line. Type the following:
You should have the running version. If not, either rewatch the this demo or refer to the official installation documentation. Also, make sure to read the Best Practices for Windows article as it covers issues such as long file names, symlink support and other important issues.
With Windows up and running, let’s get bazel up and running on Linux. Again, I’m going to using Ubuntu. If you are using a different distro, check the Bazel documentation as it covers a few other distros along with instructions on compiling from source.
With a fresh install of Ubuntu up and running, I’m going to write some commands to get access to the bazel.
sudo apt install apt-transport-https curl gnupg 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
Next, I’ll run apt-update and apt-install
sudo apt update && sudo apt install bazel
At this point, I can run bazel.
It’s super easy. Finally, let’s get this running on macOS. To install macOS, we’ll be using Homebrew. If you don’t have Homebrew installed, head over to brew.sh. You’ll see a simple installation script. You can copy it to the terminal. As you can see, I already have it installed.
Next, you need to install Xcode. If you don’t have Xcode installed on your system, open up the developer tools and download the latest version. Once done, type the following in the terminal to install the command line tools.
Then type the following to accept the license agreement:
sudo xcodebuild -license accept
Now for the install. Type the following:
brew install bazel
Finally, we check the version:
Now if you are planning on installing Bazelisk, head on over to the Bazelisk GitHub page. There are multiple ways to instal. In my case, I’ll use Homebrew.
brew install bazelisk
Now I can use bazelisk directly from the command line.
And we are up and running and ready to get busy. Okay, let’s get started.