Protoc error on build

I’ve been trying to build a simulation environment using these instructions

on Ubuntu 24.04.2 LTS

That seems to work but when I try:
make px4_sitl gz_x500

It fails and it seems to be from:
PX4-Autopilot/build/px4_sitl_default/src/modules/simulation/gz_msgs/opticalflow.pb.h:17:2: error: #error This file was generated by an older version of protoc which is
17 | #error This file was generated by an older version of protoc which is
| ^~~~~
compilation terminated due to -Wfatal-errors.
[452/738] Building CXX object src/modu…mulation__gz_bridge.dir/GZGimbal.cpp.o
ninja: build stopped: subcommand failed.
make: *** [Makefile:232: px4_sitl] Error 1

I found some suggestions in other threads, to install an earlier version of protobuf but there doesn’t seem to be a release 17 of protobuf.

I’m also looking into rebuilding opticalflow with a more recent version of protoc but I haven’t figured that out either.

I was facing the same error while using Ubuntu 22.04 on WSL2.
However, I followed the steps below and the issue was successfully resolved:

Step 1: Remove the Existing Protobuf Version

First, completely remove the version of protobuf that was installed by the package manager.

sudo apt-get remove --purge protobuf-compiler libprotobuf-dev
sudo apt-get autoremove

Step 2: Download and Compile protoc 3.6.1 from Source

This will download the source code, compile it on your machine, and install it.

Navigate to your home directory
cd ~

Download the source code for version 3.6.1
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.6.1/protobuf-all-3.6.1.tar.gz

Extract the archive
tar -xzf protobuf-all-3.6.1.tar.gz

Move into the extracted directory
cd protobuf-3.6.1

Configure the build (this prepares the makefiles)
./configure --prefix=/usr/local

Compile the source code using all available processor cores
make -j$(nproc)

Install the compiled files to your system
sudo make install

Update the shared library cache so the system can find the new files
sudo ldconfig

Step 3: Verify the Installation

Check that the correct version is now active on your system.

protoc --version

Step 4: Clean the PX4 Project Directory

You must remove all old build files from your PX4 project that were created with the previous protoc version.

cd ~/PX4-Autopilot
make distclean

Step 5: Restart WSL

A full restart of the WSL instance is crucial to ensure all changes are applied correctly.

  1. Close your WSL terminal or type exit.
  2. Open a Windows Command Prompt (cmd) or PowerShell and execute: PowerShellwsl --shutdown
  3. Wait a few seconds, then open a new WSL terminal.

Step 6: Attempt the Build Again

Navigate back to your project folder and try to build PX4 SITL.

cd ~/PX4-Autopilot
make px4_sitl