Install the Protocol Buffer Compiler

The first thing we need to do to get you compiling protobuf is—you guessed it—install the compiler. Go to the Protobuf release page on GitHub[4] and download the relevant release for your computer. If you’re on a Mac, for instance, you’d download protoc-3.9.0-osx-x86_64.zip. You can download and install in your terminal like so:

 $ wget https://github.com/protocolbuffers/protobuf/​
 releases/download/v3.9.0/protoc-3.9.0-osx-x86_64.zip
 $ unzip protoc-3.9.0-osx-x86_64.zip -d /usr/local/protobuf

Here’s what the layout and files in the extracted protobuf directory look like:

 ❯ tree /usr/local/protobuf
 /usr/local/protobuf
 ├── bin
 │   └── protoc
 ├── include
 │   └── google
 │   └── protobuf
 │   ├── any.proto
 │   ├── api.proto
 │   ├── compiler
 │   │   └── plugin.proto
 │   ├── descriptor.proto
 │   ├── duration.proto
 │   ├── empty.proto
 │   ├── field_mask.proto
 │   ├── source_context.proto
 │   ├── struct.proto
 │   ├── timestamp.proto
 │   ├── type.proto
 │   └── wrappers.proto
 └── readme.txt

As you can see, a protobuf installation consists of two directories. The bin directory contains the compiler binary named protoc, and the include directories contains a bunch of protobuf files that are like protobuf’s standard library. A mistake I’ve seen many people make when setting up their systems to work with protobuf is that they install the compiler binary without the include protobuf files. But without those files you can’t compile successfully, so just extract the whole release using the commands I just showed you and you’ll be just dandy.

Now that you’ve got the compiler binary installed, make sure your shell can find and run it. Add the binary to your PATH env var using your shell’s configuration file. If you’re using ZSH for instance, run something like the following to update your configuration:

 $ echo ​'export PATH="$PATH:/usr/local/protobuf/bin"'​ >> ~/.zshenv

At this point the protobuf compiler is installed on your machine. To test the installation, run protoc --version. If you don’t see any errors, you’re ready to handle the rest of this chapter. If you do see errors, don’t worry: few installation problems are unique. Google will show you the way.

With the compiler installed, you’re ready to write and compile some protobuf. Let’s get to it!

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

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