Building Protocol Buffers on the Raspberry Pi 2

Introduction

Protocol Buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data. Protocol Buffers is a requirement for building Hadoop. This post will outline the steps you need to perform, to build Protocol Buffers on the Raspberry Pi 2.

Install Dependencies

Protocol Buffers has some dependencies that we will need to install. Running the following command will install what we need:

sudo apt-get install build-essential g++ autoconf automake \
  libtool zlib1g-dev pkg-config libssl-dev -y

Obtaining and Building the Source Code

Normally, we would get the latest version of the code from the official GitHub project for Protocol Buffers, but Hadoop expects version 2.5.0.

Every user places source code in a different directory, so before you perform any of the following steps, please navigate to that directory first.

wget https://protobuf.googlecode.com/files/protobuf-2.5.0.tar.gz
tar xf protobuf-2.5.0.tar.gz
rm protobuf-2.5.0.tar.gz

Once the source code is downloaded and extracted, we will need navigate to the protobuf-2.5.0 directory.

cd protobuf-2.5.0

Next we will run the configure script. We want to have Protocol Buffers installed to the /opt/protobuf-2.5.0 directory.

./configure --prefix=/opt/protobuf-2.5.0

We will be able to run the make commands after the configure script completes without errors.

make
make check

Installing Protocol Buffers

We will be able to install Protocol Buffers once the build completes without errors.

sudo make install

Let’s make a friendlier path for Protocol Buffers…

sudo ln -s /opt/protobuf-2.5.0 /opt/protobuf

Next we’ll need to update the /etc/profile file to include a reference to /opt/protobuf/bin.

sudo nano /etc/profile

At the top of the file, add the following lines:

PROTOBUF_HOME=/opt/protobuf
export PROTOBUF_HOME

We also need to update PKG_CONFIG_PATH so other programs can find the includes and libraries for Protocol Buffers…

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$PROTOBUF_HOME/lib/pkgconfig
export PKG_CONFIG_PATH

Find where PATH is defined or updated and add the following to the end:

:$PROTOBUF_HOME/bin

Your PATH will be similar to:

PATH=$PATH:$PROTOBUF_HOME/bin

Validating Protocol Buffers

Now that we have everything setup, we can check the version of Prototype Buffers.

protoc --version

Which should have an output similar to this:

libprotoc 2.5.0

We can also check to see if other builds from source can find Protocol Buffers.

pkg-config --cflags --libs protobuf

Which should have an output similar to this:

-pthread -I/opt/protobuf-2.5.0/include  -pthread -L/opt/protobuf-2.5.0/lib -lprotobuf -lpthread

Complete Script (minus /etc/profile updates)

#!/bin/bash
sudo apt-get install build-essential -y
sudo apt-get install g++ autoconf automake libtool cmake zlib1g-dev pkg-config libssl-dev -y
mkdir -p $HOME/src
cd $HOME/src
wget https://protobuf.googlecode.com/files/protobuf-2.5.0.tar.gz
tar xf protobuf-2.5.0.tar.gz
rm protobuf-2.5.0.tar.gz
cd protobuf-2.5.0
./configure --prefix=/opt/protobuf-2.5.0
make
make check
sudo make install
sudo ln -s /opt/protobuf-2.5.0 /opt/protobuf

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.