© P.J. McNerney 2020
P. McNerneyBeginning Bazelhttps://doi.org/10.1007/978-1-4842-5194-2_2

2. Setup and Installation

P. J. McNerney1 
(1)
Blackhawk, CO, USA
 

Before we start building, we need to install Bazel and any other tools and frameworks required (e.g., compilers). In this chapter, we will demonstrate the necessary steps among several operating systems.

Note

Throughout the course of this book, we will be using Bazel version 1.0.0. Bazel is evolving rapidly, with new capabilities and configurations coming all the time. In this evolution, there may be changes to the dependencies which require tweaks to the build code. For the sake of the examples here, it is best for you to normalize against version 1.0.0. Once you have gotten the hang of Bazel, you can upgrade to later versions and tweak the examples as necessary.

We will cover the installation instructions for the following operating systems: Windows, MacOS, and Ubuntu Linux. Additional installation instructions may be found at https://docs.bazel.build/versions/master/install.html.

Since we are focused on version 1.0.0 of Bazel, we can find the installation binaries required for all of our platforms at https://github.com/bazelbuild/bazel/releases/.

For all operating systems, we will need some basic tools in order to bootstrap Bazel. These include the tools and frameworks for building and running Python, Java, and C++. Although we won’t be explicitly building any C++ projects in the course of this book, we will be depending upon projects which do build C++ (e.g., Protocol Buffers).

Note

Java, Python, and C++ are “special” with regard to Bazel because they largely comprise the built-in languages whose rules come out of the box with Bazel. That is, the rules for building libraries and binary using these languages come as a part of Bazel itself.

Other languages that we will build over the course of this book are Go and Swift; however, we don’t need to explicitly download tools for them. Instead, we will see that by virtue of depending upon external projects and registering the appropriate toolchains, we will get the components to build these languages for “free.” There will be more on this in later chapters.

MacOS

Installing Xcode

For installation on MacOS, we will first need to install Xcode for performing the basic build actions. The simplest way to retrieve Xcode is to open the App Store application on MacOS and download the application.

../images/481224_1_En_2_Chapter/481224_1_En_2_Fig1_HTML.jpg
Figure 2-1

Retrieving Xcode from the App Store

Once you have downloaded the application, you will need to open the application and accept the license agreement.

Alternatively, you can accept the license agreement on the command line. To do so, open a terminal window and execute the following:
~$ sudo xcodebuild -license accept

Installing Bazel

Once you have set up Xcode, you are now ready to install Bazel onto your machine. Download the Bazel binary installer for 1.0.0. This can be found at https://github.com/bazelbuild/bazel/releases/download/1.0.0/bazel-1.0.0-installer-darwin-x86_64.sh.

After this has downloaded, navigate to the directory (e.g., Downloads) to which you have downloaded the installation script. As a precaution, you may need to first ensure that you can execute the installation script by changing the file’s permissions. Once done, you can then run installation.
~$ cd Downloads
~/Downloads$ chmod +x bazel-1.0.0-installer-darwin-x86_64.sh
~/Downloads$ ./bazel-1.0.0-installer-darwin-x86_64.sh –user
The –-user flag installs Bazel to ~/bin (i.e., your user’s bin directory). To ensure that you can run Bazel, make sure that ~/bin is in your default paths. Add the following to your ~/.zshrc (or .bashrc if you are using a version of MacOs earlier than Catalina) file.
export PATH="$PATH:$HOME/bin"
Once you have added this in, source your ~/.zshrc (or ~/.bashrc) file to make sure the new path is picked up.
~$ source ~/.zshrc
Now you are all set to run Bazel on MacOS. You can easily verify this on the command line using the version directive, which will output what version of Bazel you are using.
~$ bazel --version
bazel 1.0.0

Installing Java

You will need at least Java 8 for the examples presented in this book. For MacOS 10.7 and above, Java is no longer installed by default; instead, we need to explicitly download and install it.

Head over to https://java.com/en/download/mac_download.jsp to download Java. Once you have downloaded the file to your computer, follow the instructions to install Java on your computer.

Once you are done, open a terminal and verify that you have successfully installed Java on your computer by running the following:
~$ java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)java

Verifying Your Python Version

By default, Python comes packaged with your MacOS machine. You will need at least Python 2.7.15 in order to run the examples in this book. To verify that you have a sufficient version of Python on your computer, open a terminal to verify your version.
~$ python --version
Python 2.7.15

Note

At the time of this writing, both Python 2 and Python 3 work with Bazel, with a migration path currently in place to get to the latter. You can set your default to be one or the other; however, this is considered outside the scope of this book for the sake of the examples presented therein; currently they should work with either version.

Ubuntu Linux

Installing Required Packages

Installation on Ubuntu is very similar to MacOS. However, in this case, instead of downloading Xcode, we will be installing a set of required packages (i.e., pkg-config, zip, g++, zlib1g-dev, unzip, python3).

Open a terminal window and execute the following command:
~$ sudo apt-get install pkg-config zip g++ zlib1g-dev unzip python3

This may require you to install additional packages as well. Press “Y” when asked if additional packages should be installed.

Installing Bazel

Having retrieved the prerequisites, you are ready to download Bazel. Retrieve the installation script from https://github.com/bazelbuild/bazel/releases/download/1.0.0/bazel-1.0.0-installer-linux-x86_64.sh.

Open a terminal and navigate to the location where you downloaded the file (e.g., ~/.Downloads). It may be necessary to set the permissions to execute the file. After that is taken care of, you can run the execution.
~$ cd Downloads
~/Downloads$ chmod +x bazel-1.0.0-installer-linux-x86_64.sh
~/Downloads$ ./bazel-1.0.0-installer-linux-x86_64.sh --user
The –-user flag installs Bazel to ~/bin (i.e., your user’s bin directory). To ensure that you can run Bazel, make sure that ~/bin is in your default paths. Add the following to your ~/.bashrc file:
export PATH="$PATH:$HOME/bin"
Once you have added this in, source your ~/.bashrc file to make sure the new path is picked up.
~$ source ~/.bashrc
Now you are all set to run Bazel on Ubuntu. You can easily verify this on the command line using the version directive, which will output what version of Bazel you are using.
~$ bazel –-version
bazel 1.0.0

Installing Java

To ensure that we are installing the correct version of Java (via OpenJDK), we first need to check your version of Ubuntu. Open a terminal and execute the following:
~$ lsb_release -a
No LSB modules area available
Distributor ID: Ubuntu
Description:      Ubuntu 16.04.5 LTS
Release:          16.04
Codename:         xenial
If you are using Ubuntu 16.04, then you will need to use OpenJDK 8. Run the following command line:
~$ sudo apt-get install openjdk-8-jdk
If you are using Ubuntu 18.04, then you will need to use Open JDK 11. Run the following command line instead:
~$ sudo apt-get install openjdk-11-jdk

In each case, it may be necessary to install additional packages in order to complete the Java installation.

After you are done with installation, verify the version of Java you have installed by running the following:
~$ java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-8u222-b10-1ubuntu1~16.04.1-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)

Windows

Setting Up Your System

In order to use Bazel, it is recommended that you have 64-bit Windows 10, version 1703 or above. To check your Windows version, open Settings.
../images/481224_1_En_2_Chapter/481224_1_En_2_Fig2_HTML.jpg
Figure 2-2

Windows Settings

Select SystemAbout. The information you need is under Windows Specification .
../images/481224_1_En_2_Chapter/481224_1_En_2_Fig3_HTML.jpg
Figure 2-3

Verifying the OS build of Windows

Additionally, you will need to enable Developer Mode in order to develop on your machine. Go to SettingsUpdate & SecurityFor developers.
../images/481224_1_En_2_Chapter/481224_1_En_2_Fig4_HTML.jpg
Figure 2-4

Enabling Developer Mode

Under Use developer features, select Developer mode. It may be necessary to wait for the Developer package to be downloaded.

Installing Required Applications

You will need several applications prior to actually retrieving Bazel itself.

Visual C++ Redistributable for Visual Studio 2015

This package contains the runtime components required for executing C++ applications built using Visual Studio 2015. Navigate to www.microsoft.com/en-us/download/developer-tools.aspx and search for Visual C++ Redistributable for Visual Studio 2015.
../images/481224_1_En_2_Chapter/481224_1_En_2_Fig5_HTML.jpg
Figure 2-5

Retrieving the Visual C++ Redistributable Packages

Download the package and install it on your computer.

MSYS2

MSYS2 is a platform that provides some basic tools for software distribution and building; in this context, we will be most interested in the fact that it provides a bash shell for Windows. It also provides a package management system to make it easy to install software, similar to what might be seen in Linux (e.g., through apt-get) or MacOS (e.g., through something like Homebrew).

Navigate to www.msys2.org/ and download MSYS2 for x86_64. Install the software on your computer; for simplicity, use the default installation path.
../images/481224_1_En_2_Chapter/481224_1_En_2_Fig6_HTML.jpg
Figure 2-6

Installing MSYS2 to your computer

Once you have completed installation, open an MSYS2 terminal. You will need to install several packages (i.e., zip, unzip, patch, diffutils, and git). Execute the following command within the terminal:
pjmcn@WINDOWS-HOME MSYS~
$ pacman -S zip unzip patch diffutils git

You may need to install additional packages in order to complete the installations.

Bazel Installation

Having taken care of the necessary components, you are now ready to download and install Bazel itself. Retrieve the executable from https://github.com/bazelbuild/bazel/releases/download/1.0.0/bazel-1.0.0-windows-x86_64.exe.

Unlike Linux and MacOS, the downloaded executable is the Bazel executable; there is no separate installation script. Once you have downloaded the application, move the application to a directory (e.g., C:Users<user name>in) and rename it to bazel.exe.

Add the path to your MSYS2 .bashrc file.
export PATH="$PATH:/c/Users/<username>/bin"
Once you have added this in, source your ~/.bashrc file to make sure the new path is picked up.
~$ source ~/.bashrc
Now you are all set to run Bazel on Windows. You can easily verify this on the command line using the version directive, which will output what version of Bazel you are using.
pjmcn@WINDOWS-HOME MSYS~
$ bazel –-version
bazel 1.0.0

Installing Language Support

In order to work with several languages (C++, Java, and Python), you will need to install the appropriate supporting frameworks and applications.

C++
Although we will not be directly building C++ applications within this book, there are several libraries upon which we will depend which require C++ support. Navigate to https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019. Download the Build Tools Installer and run the installation.
../images/481224_1_En_2_Chapter/481224_1_En_2_Fig7_HTML.jpg
Figure 2-7

Retrieving the Build Tools for Visual Studio 2019

During the course of installation, make sure you select the C++ build tools.
../images/481224_1_En_2_Chapter/481224_1_En_2_Fig8_HTML.jpg
Figure 2-8

Installing the C++ build tools

Java

Many examples in this book do use Java. Navigate to www.oracle.com/technetwork/java/javase/downloads/index.html. You will need to download at least Java SE Development Kit 10 for Windows x64. Download an appropriate installation executable and install it on your computer.

Note

MSYS2 has difficulty with spaces within paths. The default path places Java into Program Files. In order to avoid any issues, you should change your installation path to someplace without spaces (e.g., C:Users<user name>inJava<jdk-version>).

As you did with Bazel, make sure to add in the path to your Java installation into the PATH of your bash shell within your .bashrc file.
export PATH="$PATH:/c/Users/<user name>/bin/Java/<jdk-version>/bin"
Additionally, you will need to also set your JAVA_HOME variable.
export JAVA_HOME="/c/Users/<user name>/bin/Java/<jdk-version>"
Source the .bashrc and confirm that Java is all set within MSYS2.
pjmcn@WINDOWS-HOME MSYS~
$ source .bashrc
pjmcn@WINDOWS-HOME MSYS~
$ java --version
java version "11.0.4" 2019-07-16 LTS
Java™ SE Runtime Environment 18.9 (build 11.0.4+10-LTS)
Java Hotspot™ 64-Bit Server VM 18.9 (build 11.0.4+10-LTS, mixed mode)
Python

Finally, in order to build for Python, you will need to download either Python 2.7 or 3 for Windows. Navigate to www.python.org/downloads/release/python-2716 and download the Windows x86-64 MSI Installer. After you have downloaded the installer, execute it in order to install Python.

Once again, make sure you add the path to Python into the PATH of your bash shell.
export PATH="$PATH:/c/Python27"
Source the .bashrc and confirm that python is all set within MSYS2.
pjmcn@WINDOWS-HOME MSYS~
$ source .bashrc
pjmcn@WINDOWS-HOME MSYS~
$ python –-version
Python 2.7.16

Final Word

At this stage, you should be able to execute the Bazel examples within this book. For additional operating systems that you may want to install Bazel on, navigate to https://docs.bazel.build/versions/master/install.html.

Having taken care of the scaffolding necessary to run Bazel, you are now ready to jump in and start having some fun.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.4.181