Install protoc on Ubuntu 20.04

Protocol buffers also known as protobuf is a cross-platform data format for serializing structured data. The protoc is a protocol buffer compiler used to compile .proto files which describes message and service definitions.

This tutorial explains how to install protoc on Ubuntu 20.04.

Prepare environment

Make sure you have installed unzip package for extracting protoc executable file from a ZIP archive.

sudo apt update
sudo apt install -y unzip

Install protoc

Get the latest version tag of protoc release and assign it to variable:

PROTOC_VERSION=$(curl -s "https://api.github.com/repos/protocolbuffers/protobuf/releases/latest" | grep -Po '"tag_name": "v\K[0-9.]+')

Download ZIP archive from releases page of the protoc repository:

curl -Lo protoc.zip "https://github.com/protocolbuffers/protobuf/releases/latest/download/protoc-${PROTOC_VERSION}-linux-x86_64.zip"

Run the following command to extract executable file from a ZIP archive:

sudo unzip -q protoc.zip bin/protoc -d /usr/local

Set execute permission:

sudo chmod a+x /usr/local/bin/protoc

Now protoc command is available for all users as a system-wide command.

We can check protoc version:

protoc --version

Remove unnecessary ZIP archive:

rm -rf protoc.zip

Testing protoc

Create .proto file:

nano example.proto

Add the following code which defines simple message format:

syntax = "proto3";

package example;

message Person {
  string name = 1;
}

Once you have .proto file, you need to compile it with protoc compiler. It will parse the .proto file and will generate source files according to specified programming language. For example, the following command generates example_pb2.py file in current working directory:

protoc --python_out=. example.proto

Generated file for specific language is like a template which can be used to provide the data based on defined structure for serialization.

Uninstall protoc

If you decided to completely remove protoc, delete executable file:

sudo rm -rf /usr/local/bin/protoc

For environment preparation we needed to install unzip package. You can remove it:

sudo apt purge --autoremove -y unzip

Leave a Comment

Your email address will not be published.