24
votes

I am trying to build a sample app with go grpc but I am unable to generate the code using "protoc"

Have installed the required libraries/go packages using:

  1. go get -u google.golang.org/grpc
  2. go get -u github.com/golang/protobuf/protoc-gen-go

Have tried setting the path as well but no luck.

Sample "proto" file:

syntax = "proto3";

package greet;
option go_package="greetpb";

service GreetService{}

Error Message: "protoc-gen-go: program not found or is not executable --go_out: protoc-gen-go: Plugin failed with status code 1."

11
Can you add the exact command you are executing? - Jory Geerts
"protoc greet/greetpb/greet.proto --go_out=plugins=grpc:." Folder structure: greet->greetpb-> greet.proto file - Mayank Gupta

11 Answers

22
votes

There are two ways to install the protobuf compiler, if you're on Ubuntu you can use this,

sudo apt install protobuf-compiler

Then of course there's the standard way,

go get -u github.com/golang/protobuf/{proto,protoc-gen-go}

Here forward it's just adding the path. Assuming when you installed Go you did this,

echo 'export GOPATH=$HOME/Go' >> $HOME/.bashrc
source $HOME/.bashrc

Now you can just extend this,

echo 'export PATH=$PATH:$GOPATH/bin' >> $HOME/.bashrc
source $HOME/.bashrc

Strangely protoc can't expand ~.

17
votes

Resolved by following the steps:

Install go library using: go get -u github.com/golang/protobuf/{proto,protoc-gen-go}

  1. Run vim ~/.bash_profile
  2. Add:
export GO_PATH=~/go
export PATH=$PATH:/$GO_PATH/bin
  1. Run source ~/.bash_profile

Reference: Unable to build protobuf to go endpoint

10
votes

They commented above that the following package had to be added

sudo apt install protobuf-compiler

In my case I also had to add this one ..

sudo apt install golang-goprotobuf-dev
7
votes

From the github repo this solution has worked for me.

Go version is go version go1.14.1 linux/amd64

Add this to .bashrc and source it.

export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOROOT:$GOPATH:$GOBIN

https://github.com/golang/protobuf/issues/795

6
votes

Just in case if anyone else is still facing this issue, try the official solution from grpc.io documentation

Go plugins for the protocol compiler:

Install the protocol compiler plugins for Go using the following commands:

$ export GO111MODULE=on  # Enable module mode

$ go get google.golang.org/protobuf/cmd/protoc-gen-go \
         google.golang.org/grpc/cmd/protoc-gen-go-grpc

Update your PATH so that the protoc compiler can find the plugins:

$ export PATH="$PATH:$(go env GOPATH)/bin"

This worked for me.

5
votes

Make sure your GOBIN is set in the PATH variable. Otherwise, you may encounter this problem. Check GOBIN path by running go env and confirm GOBIN is not empty.

If it is empty then try like below

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
protoc --go_out=plugins=grpc:. *.proto
2
votes

Use go get to download the following packages:

$ go get google.golang.org/protobuf/cmd/protoc-gen-go
$ go get google.golang.org/grpc/cmd/protoc-gen-go-grpc

To set Setting GOPATH and GOROOT, follow the below procedure:

But, first, run go env.

If you see that the Go is not installed, you can install it via Homebrew. If you see the output, then your Go is installed. It shows you all the environments that are set and are not.

If you see empty for GOPATH:

Create any directory anywhere on your computer for Go projects in my case: ~/GO_PROJECTS then export GOPATH=~/GO_PROJECTS.

If you see empty for GOROOT:

Run which go (On my computer : /usr/local/bin/go) then Edit your ~/.zshrc file to add the following line export like this export GOROOT=/usr/local/go.

You can also edit your ~/.zshrc file to add the following line to set up the GOPATH and GOROOT:

If you have installed Go from the original pkg, download from https://golang.org/doc/install.

export GOPATH=$HOME/dev/go-workspace
export GOROOT=/usr/local/go
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin

If you have installed Go using Homebrew.

export GOPATH=$HOME/dev/go-workspace
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin

Save and exit your editor. Then, source your ~/.zshrc.

source ~/.zshrc
1
votes

When you run go get -u github.com/golang/protobuf/protoc-gen-go in vscode terminal. If vscode doesn't found $GOPATH & $GOBIN it will install the package at default user's home directory /home/$username/go{/bin}

The solution is to move all file in bin and pkg folder to your current go path (/usr/local/go{/bin}), go path is which contains go file in bin folder And add this line to end of .bashrc file

export GOPATH=/usr/local/go:$PATH
export GOBIN=/usr/local/go/bin:$PATH

Then reboot computer

0
votes

In case someone is facing the same issue now days.

Install go library using

go get -u github.com/golang/protobuf/{proto,protoc-gen-go}

Make sure the GOPATH variable is set In my case I created a folder called gocode but if your code is located in another folder you have to change it

export GOPATH=$HOME/gocode
export Path=$Path:$GOPATH/bin

After following this steps I found another error protoc-gen-go failed :: The import path must contain at least one forward slash ('/') character. To solve this change the path in the option

syntax = "proto3";

package greet;

option go_package="./greet/greetpb";

service GreetService{}
0
votes

I tried multiple solutions but at the end found out that go env variables are the actual culprits for this. If you are on linux add these variables in your bashrc or bashprofile file.

export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOROOT:$GOPATH:$GOBIN

And don't forget to source it after editing. (e.g $source ~/.bachrc)

-1
votes

Many of the other responses address path issues but if it's not installed, you can install it using the following command:

go install google.golang.org/grpc/cmd/protoc-gen-go-grpc