83

I am trying to build a sample application with Go gRPC, but I am unable to generate the code using "protoc"

I have installed the required libraries and Go packages using:

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

I 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."

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mayank Gupta
  • 1,779
  • 1
  • 13
  • 19
  • Can you add the exact command you are executing? – Jory Geerts Aug 29 '19 at 14:37
  • "protoc greet/greetpb/greet.proto --go_out=plugins=grpc:." Folder structure: greet->greetpb-> greet.proto file – Mayank Gupta Aug 29 '19 at 17:02
  • 3
    Read the documentation https://grpc.io/docs/languages/go/quickstart/#prerequisites – Marvin Collins May 23 '21 at 06:59
  • 11
    For Mac Users: simply use `brew install protoc-gen-go` or another plugin like `brew install protoc-gen-go-grpc`, thereafter probably got install in `/usr/local/Cellar/protoc-gen-go/version/bin`, add it permanently either on .zshrc (recommend) or .bash_history or .bash_profile. check by `protoc-gen-go --version` simple! – ArifMustafa Jan 16 '22 at 23:23
  • @ArifMustafa This solved my issue well. Thanks. – Aditya Goel Mar 18 '22 at 06:46

19 Answers19

52

I resolved it by following these steps:

Install the 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
    
  3. Run source ~/.bash_profile

Reference: Unable to build protobuf to go endpoint

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mayank Gupta
  • 1,779
  • 1
  • 13
  • 19
  • 1
    This wouldn't work as the `protoc` compiler can't resolve `~` –  Aug 30 '19 at 17:47
  • 1
    As an aside, you really should be using `.bashrc` instead of `.bash_profile` –  Aug 30 '19 at 17:48
  • @SoumasishGoswami can you explain why use .bashrc instead of .bash_profile? – Joel Guerra Mar 20 '20 at 21:44
  • 2
    @TannishaHill `.bash_profile` or better yet `.profile` is preferable for env vars which you export because it is usually only executed once, by login shells. `.bashrc` is executed by all interactive shells, so you end up just blithely reexporting the env vars everytime you manually launch a child shell. – papiro Jun 09 '21 at 00:35
  • 2
    `go: module github.com/golang/protobuf is deprecated: Use the "google.golang.org/protobuf" module instead.` `go get: installing executables with 'go get' in module mode is deprecated.` – Ed Randall Jun 07 '22 at 17:35
  • 1
    'go get' is no longer supported outside a module. To build and install a command, use 'go install' with a version. Here is a sample command to use: ```go install github.com/golang/protobuf/proto@latest github.com/golang/protobuf/protoc-gen-go@latest``` – Sahil Jain Jun 15 '22 at 21:46
  • if you are using `zsh` shell you have to change `.zshrc` file – Javlon Aug 04 '22 at 16:57
52

Tannisha Hill indicated 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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Panchove
  • 531
  • 4
  • 4
  • module github.com/golang/protobuf is deprecated: Use the "google.golang.org/protobuf" module instead – shanwije Jan 11 '22 at 12:30
  • 1
    On what system and distribution (incl. versions)? Was it [Ubuntu 20.04](https://en.wikipedia.org/wiki/Ubuntu_version_history#Ubuntu_20.04_LTS_(Focal_Fossa)) (Focal Fossa)? Or something else? – Peter Mortensen Mar 08 '22 at 00:43
43

Go 1.17+

From https://go.dev/doc/go-get-install-deprecation

Starting in Go 1.17, installing executables with go get is deprecated. go install may be used instead.

~/.bashrc

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

Install

go install google.golang.org/protobuf/cmd/protoc-gen-go@latest

go: downloading google.golang.org/protobuf v1.27.1

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

go: downloading google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.2.0

go: downloading google.golang.org/grpc v1.44.0

file.go

protoc --go-grpc_out=. *.proto

Environment

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rbento
  • 9,919
  • 3
  • 61
  • 61
  • 3
    In my case, it worked when I added this part `export GOPATH=$HOME/go export PATH=$PATH:$GOPATH/bin` to ~/.bash_profile – Ahmed Mozaly Mar 13 '22 at 14:55
  • This probably isn't what most people want. Since most people are now using go mod, the appropriate option isn't to set GOPATH, but rather to use GOFLAGS=-mod=readonly go install {package} and then it will use GOPATH that's set in your go env. – Case May 09 '22 at 21:11
  • always remember to re-run `source ~/.bashrc` in your terminal before spending hours cursing as to why the top voted answer doesn't work... – Hansang Nov 07 '22 at 05:24
  • this guy is a hero – Lucas Meine Aug 28 '23 at 15:49
35

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 ~.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 'go get' is no longer supported outside a module. To build and install a command, use 'go install' with a version Here is a sample command to use: ```go install github.com/golang/protobuf/proto@latest github.com/golang/protobuf/protoc-gen-go@latest``` – Sahil Jain Jun 15 '22 at 21:47
17

From the GitHub repository, this solution has worked for me.

The 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

Ref.: protoc-gen-go: program not found or is not executable #795

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Krishnadas PC
  • 5,981
  • 2
  • 53
  • 54
10

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
Sathishkumar Rakkiyasamy
  • 3,509
  • 2
  • 30
  • 34
  • Exactly, as the [docs](https://developers.google.com/protocol-buffers/docs/reference/go-generated) state: _"Set the $GOBIN environment variable to change the installation location. It must be in your $PATH for the protocol buffer compiler to find it."_ – nichoio Sep 05 '21 at 12:04
10

Also 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yrineu Rodrigues
  • 1,306
  • 14
  • 12
7

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

For 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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rajiv Singh
  • 2,947
  • 2
  • 11
  • 30
  • But only on Mac (since [Homebrew](https://en.wikipedia.org/wiki/Homebrew_(package_manager)) is mentioned)? What version of macOS was it tried on? – Peter Mortensen Mar 08 '22 at 00:49
4

I tried multiple solutions, but at the end I found out that Go environment variables are the actual culprits for this.

If you are on Linux, add these variables in your .bashrc or .bash_profile 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 ~/.bashrc).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
USMAN FAZIL
  • 757
  • 5
  • 11
  • 1
    Worked for me once I had added `/home/username/go` and `/home/username/go/bin` to `PATH` (Go 1.16). It did not seem to make a difference whether env variables `$GOPATH` and `$GOBIN` are set or not (someone correct me if I'm wrong). – nichoio Sep 05 '21 at 11:55
2

Step 1:

sudo apt install protobuf-compiler

Step 2:

go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.26

Step 3:

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

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

export PATH="$PATH:$(go env GOPATH)/bin"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andy
  • 71
  • 1
  • 3
1

When you run go get -u github.com/golang/protobuf/protoc-gen-go in Visual Studio Code terminal, and if Visual Studio Code doesn't find $GOPATH and $GOBIN, it will install the package at the default user's home directory /home/$username/go{/bin}

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

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

Then reboot the computer.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ThogTq
  • 11
  • 2
0

In case someone is facing the same issue nowadays.

Install the 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 these 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{}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

NB: Switch to root privileges on your terminal and follow these steps. This got me out of the ditch

  1. git clone https://github.com/micro/protoc-gen-micro.git /usr/local/go/bin/src/github.com/micro/protoc-gen-micro
  2. cd src/github.com/micro/protoc-gen-micro
  3. go build
  4. cp protoc-gen-micro /bin

Happy coding!

Kathurima
  • 105
  • 1
  • 6
0

After installing Go, 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

rajkabbur
  • 187
  • 3
0

The reason you are seeing this message is because Go cannot reach the executable. Go looks for ~/go/bin to find the executables when it is called. Let's check to see if your PATH variable has the executable. But first, let's look at the error.

protoc-gen-go: program not found or is not executable

To solve this, we should first inspect to see if ~/go/bin is in our path.

echo $PATH

Inspect the result to see if ~/go/bin is in your path.

If not, let's add it to our path:

export PATH=$PATH:/$GO_PATH/bin

Everything should work now if protoc-gen-go is installed.

If not, install protoc-gen-go.

go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
Quan Truong
  • 197
  • 2
  • 11
0

None of above works for me. It turns out you have to go to this page and download the suitable file for your platform at unzip it into your GOPATH. For example this protoc-21.5-osx-x86_64.zip for Intel macOS or this one for linux 64 bit protoc-21.5-linux-x86_64.zip

Dat TT
  • 2,850
  • 2
  • 16
  • 18
0

It's worked for me:

check protoc-gen-go install on your system if it's not installed then install it on your system. For mac *

brew install protoc-gen-go
Javad
  • 2,033
  • 3
  • 13
  • 23
0

For this was what worked

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

Carver
  • 59
  • 5