How to Install Google Protocol Buffers on Raspberry Pi and Test with Python

How to Install Google Protocol Buffers on Raspberry Pi and Test with

How to Install Google Protocol Buffers on Raspberry Pi and Test with Python

1. Installation

Make sure you have Python 2.6 or newer but not Python 3.x yet. If in doubt, then run:

$ python -V

1.1 Install C++ Protocol Compiler

We need C++ Protocol Compiler for python. This is required to compile .proto file

$ cd ~/
$ sudo apt-get install autoconf automake libtool curl python-dev
$ wget
$ tar -zxvf protobuf-2.6.1.tar.gz
$ cd protobuf-2.6.1/
$ ./configure
$ make
$ make check
$ sudo make install
$ sudo ldconfig

Note: You can also refer to the full installation guide from here OR read from pytobuf-2.6.1 folder

1.2 Build Python runtime library

This is required to run Python program. We will use c++ implementation for Python runtime.

$ cd ~/
$ cd protobuf-2.6.1/
$ cd python
$ export LD_LIBRARY_PATH=../src/.libs
$ python build --cpp_implementation
$ python test --cpp_implementation
$ sudo python install --cpp_implementation

Note: I cannot run $ python test --cpp_implementation for some reason. but you should try as it is stated in the full installation guide.

Note: You can refer to full installation guide from here OR read README.txt file in the python folder

2. Testing Protocol Message

2.1 Write .proto file

We need to write a protocol message. This can be use for both server side and client side if you are going to transfer across the network.

$ cd /~
$ mkdir ProtobuffTest
$ cd ProtobuffTest
$ gedit addressbook.proto

Copy the following text and save it as addressbook.proto

package message;

message Person {
    required string name = 1;
    optional string email = 2;
message AddressBook{
    repeated Person person = 1;

2.2 Compile Protocol Message

Before we use any of the protocol message, we first need to compile once. In the current folder, type

$ protoc addressbook.proto --python_out="."

After the compilation, will be generated and we will use this file for Python program.

2.3 Compile Python Program

We first create a python program first.

$ gedit

Copy the following text and save it as

#! /usr/bin/python

import addressbook_pb2

address_book = addressbook_pb2.AddressBook()

john = address_book.person.add() = "John" = ""

drake = address_book.person.add() = "Drake" = ""

for person in address_book.person:
    print " Hello ",
    print " Your email will be ",

Just like any other Python program, you can just run the program by typing

$ python

The output will be like this.

 Hello  John
 Your email will be
 Hello  Drake
 Your email will be


  1. SmallTalk Protocol Buffers
  2. Protocol Buffers - Google’s data interchange format
  3. Protocol Buffer Basics: Python

1 comment:

  1. Hi there - got the following when trying to build the runtime library - any help/pointers appreciated

    pi@raspberrypi:~/protobuf-2.6.1/python $ python build --cpp_implementation
    running build
    running build_py
    running build_ext
    building 'google.protobuf.pyext._message' extension
    arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -DGOOGLE_PROTOBUF_HAS_ONEOF=1 -I. -I../src -I/usr/include/python2.7 -c google/protobuf/pyext/ -o build/temp.linux-armv6l-2.7/google/protobuf/pyext/descriptor.o
    cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
    google/protobuf/pyext/ fatal error: Python.h: No such file or directory
    compilation terminated.
    error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1