Install Bazel on Ubuntu

Install Bazel on Ubuntu

Link: https://github.com/bazelbuild/bazel
This page describes options for installing Bazel on Ubuntu. Additionally, it provides links to Bazel completion scripts and binary installers if required, for example if you don't have administrator access.
Supported Ubuntu Linux platforms:

18.04 (LTS)
16.04 (LTS)
20.04 (LTS)
Bazel should be compatible with other Ubuntu releases and Debian "stretch" and later, but has not been tested and is not guaranteed to work.

Install Bazel on Ubuntu using one of the following methods:

Recommendations: Use Bazelisk
to use our custom APT repository Compile Bazel from source
using the binary installer

Note : For ARM-based systems, the APT repository does not contain an arm64 version, and there is no binary installer available. Please use Bazelisk, or compile it from source.

Bazel ships with two completion scripts. After installing Bazel, you can do the following:

Access bash completion script
Install zsh completion script

Step 1: Add the Bazel distribution URI as a package source

Using Bazel's apt repository
Note : This setting is a one-time use.

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

The component name "jdk1.8" is reserved for legacy reasons only and has nothing to do with supported or included JDK versions. Bazel version has nothing to do with Java version. Changing the "jdk1.8" component name would break existing users of the codebase.

Step 2: Install and update Bazel

sudo apt update && sudo apt install bazel

Once installed, you can upgrade to a newer version of Bazel during the normal system update process:

sudo apt update && sudo apt full-upgrade

The bazel package always installs the latest stable version of Bazel. In addition to the latest version of Bazel, you can also install specific older versions, for example:

sudo apt install bazel-1.0.0

This will install Bazel 1.0.0 on your system as /usr/bin/bazel-1.0.0. This is useful if you need to build your project with a specific version of Bazel (for example, because the project uses a .bazelversion file to explicitly state which Bazel version it should build with).

Alternatively, you can set bazel to a specific version by creating a symlink:

sudo ln -s /usr/bin/bazel-1.0.0 /usr/bin/bazel
bazel --version  # 1.0.0

Step 3: Install JDK (optional)

Bazel includes a bundled private JRE as its runtime and does not require you to install any specific version of Java.

However, if you want to use Bazel to build Java code, you must have a JDK installed.

# Ubuntu 16.04 (LTS) uses OpenJDK 8 by default:
sudo apt install openjdk-8-jdk
# Ubuntu 18.04 (LTS) uses OpenJDK 11 by default:
sudo apt install openjdk-11-jdk

Using the binary installer

In general, you should use the apt repository, however, if your machine does not have administrator privileges or cannot add custom repositories, binary installers may be useful.

You can download binary installers from Bazel's GitHub releases page.

The installer includes and extracts the Bazel binaries into the $HOME/bin folder. You must manually install some additional libraries for Bazel to function properly.

Step 1: Install required packages

Bazel requires a C++ compiler and unzip/zip to function properly:

sudo apt install g++ unzip zip

If you want to build Java code with Bazel, install the JDK:

# Ubuntu 16.04 (LTS) uses OpenJDK 8 by default:
sudo apt-get install openjdk-8-jdk
# Ubuntu 18.04 (LTS) uses OpenJDK 11 by default:
sudo apt-get install openjdk-11-jdk

Step 2: Run the installer

Next, download the Bazel binary installer named bazel-version-installer-linux-x86_64.sh from the Bazel releases page on GitHub.

Please run it as follows:

chmod +x bazel-version-installer-linux-x86_64.sh
./bazel-version-installer-linux-x86_64.sh --user

The --user flag will install Bazel into the system's $HOME/bin directory and set the .bazelrc path to $HOME/.bazelrc. Use the --help command to see other installation options.

Step 3: Set up your environment

If you have run the Bazel installer with the above --user flag, the Bazel executable will be installed into the $HOME/bin directory. It is recommended that you add this directory to the default path as follows:

export PATH="$PATH:$HOME/bin"

You can also add this command to your ~/.bashrc or ~/.zshrc file to make it permanent.

Guess you like

Origin blog.csdn.net/qq_37464479/article/details/126385913