The protoc is an important piece of command in Linux. It is developed by Google, and is necessary for compiling the protocol buffers and their definitions. It is capable of producing output for several different languages, including C++, Java, and Python. To use the protoc command, you need to have a a file with .proto extension, which is similar to a JSON file, as it is used to represent structural data.
Many other programs and commands in Linux use this protoc command under the hood to pair it with other commands, for various different tasks like compiling the source code of a program along with other tools. In such scenarios, it is possible that you encounter the error message “protoc: command not found” in your Linux Machine. Hopefully, this article should be able to help you resolve the issue.
Why is the protoc command not found?
Protobuf not installed
The protoc command comes with the protobuf package in some Linux distributions, or the protobuf-compiler package in others. You won’t directly find any packages with the name protoc in any Linux repositories, so give up on that, if you’ve been trying to install it that way!
So the verdict here is that if you don’t have the protobuf or the protobuf-compiler package installed in your system, you’ll have to encounter the error message.
Path to protoc command not set
One common reason for not being able to run the protoc command is not setting the path to the installation folder of protoc properly. If you’ve compiled the protoc command from source, or have downloaded one of the pre-compiled binaries available in the GitHub page of protoc, it is possible that you missed out on directing the global path variable to scan protoc’s installation directory. The next section will help you fix all of that in detail.
How to fix protoc command not found
Install Protoc from distro repository
The first option for you to fix the protoc command not found error is by installing the protobuf package in your repository. It is, however, a bit tricky to get into, as different distributions name the package differently. You will need to execute one of the below commands based on the Linux Distribution you’re running, so try to pay close attention! Some of the commands required to install the protobuf package in different distros are listed below:
$ sudo pacman -Syu protobuf
$ sudo apt update
$ sudo apt install protobuf-compiler
$ sudo yum makecache
$ sudo yum install protobuf-compiler
$ sudo dnf makecache
$ sudo dnf install protobuf-compiler
This should hopefully resolve your issue with running the protoc command in your Linux Machine. If for some reason, you’re unable to install the package, or aren’t able to find it in your system’s repository, you don’t need to freak out just yet! There are more ways by which you can resolve the issue. Though they’re not as simple as this method, they will at least be able to fix the problem.
Install pre-compiled protoc executable
In the GitHub page of Protocol Buffers, you’ll be able to find pre-compiled binaries which you can simply download, and copy to the primary executables directory on your system to get the protoc command running right away. This should work across all the Linux Distributions, as long as you’re sure that you’re copying the protoc executable to the right directory.
First, download one of the .zip or .tar.gz files found under Releases. For this tutorial, I’ll be downloading the protoc-21.8-linux-x86_64.zip package, and that is pretty much what you’ll need by yourself too. If you want the command for a specific language only, you can download the other packages that have the name of the language mentioned on the package.
Once downloaded, navigate to the directory where you stored the download folder, using your terminal. For me it’d be the Downloads folder:
$ cd Downloads
Afterwards, I’ll extract the .zip file I had downloaded using the unzip command:
$ unzip protoc-21.8-linux-x86_64.zip
I will then enter the bin directory which got extracted from the .zip file:
$ cd bin
Now is the most important part of the whole process. It is not only important, but is also the step which many people make a mess in, and is one of the primary reasons for not being able to execute the command. What I’ll have to do, is to copy the protoc executable found in this bin directory and paste it into the main executables folder of my system, which is “/usr/bin”. I’ll do this by running the following command in my terminal:
$ sudo cp protoc /usr/bin
Once done, I should now be able to run the protoc command without any hassles. I’ll use the following command to see if it has properly been installed:
$ protoc --version
If you’ve gotten something similar to the above image, congratulations. You’ve successfully installed the protoc command in your system. But if luck didn’t find you, and you failed to execute it still, try looking out for the next sections that follow.
Install protoc from source code
There’s one last method for installing protoc from source. If you want maximum control over how the package is installed, and what features are included, you can compile the program from source.
First up, download the protobuf-all package in the very beginning of the releases, as this will be the preferred source code for most. It doesn’t matter whether you’re downloading the .zip package or the .tar.gz package as long as you know how to extract them. Both packages have the very same files, so you don’t have to worry about it.
Then, extract the package, the same way as the previous part. If you’ve downloaded the .tar.gz one, you’ll use:
$ tar -xf protobuf-all-21.8.tar.gz
Or if you prefer the .zip version, you’d use:
$ unzip protobuf-all-21.8.zip
to extract the packages. Make sure to type out the latest version instead of just copy pasting this!
Now enter the directory that got extracted. Inside the directory, run the following command:
Then run make to compile the package:
Lastly, install the protoc command in the whole system by running:
$ sudo make install
You should now hopefully be able to run the protoc command in your Linux machine.
However, as I had previously mentioned, it is possible that the path to the installation folder to protoc may not be set correctly, in case you have installed it in some other directory than the default one, which is /usr/bin. The installation directory must be included in the global PATH variable so that bourne again shell can scan for it when the terminal starts. Otherwise, you’d be unable to run the command.
To fix this, you need to add one line of code to the .bashrc file located in your home directory. It is actually just a configuration file for the Bourne Again Shell, which also allows you to assign commands to be executed when the terminal starts.
To do this, add the following line in your .bashrc file:
Make sure to replace the /path/to/installation/directory part with the actual path of the protoc where you have installed it.
Install protoc for C++ Users
For those willing to use protoc with C++, they will have to go through the same procedure as the installation from source. THe only difference is that, if you protoc to only work with C++, then choose the protobuf-cpp package instead of the protobuf-all package. The rest of the process should be the same.
After all that we’ve been through, I’m confident that you’ve successfully been able to run the protoc command in your Linux machine. For many users, the protoc command might not turn out to be for a direct use case, but rather to install a third-party program which requires protoc while building those packages. In such scenarios, you’d probably not care about the features you can tinker with if you build the command from source. Instead, if the easier methods get your job done, why bother?