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 24.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:
wget -qO protoc.zip https://github.com/protocolbuffers/protobuf/releases/latest/download/protoc-$PROTOC_VERSION-linux-x86_64.zip
Run the following command to extract the 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 the 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 the .proto
file, you need to compile it with the protoc
compiler. It will parse the .proto
file and will generate source files according to specified programming language. For example, the following command generates the 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
The 3 Comments Found
Just use the package manager
apt install protobuf-compiler
Hi,
The
protoc
available through APT package manager could be outdated. The installation guide provided in the post explains how to install the latest version ofprotoc
.Thanks a lot!
Leave a Comment
Cancel reply