'All of a sudden go tool: no such tool "compile"

I had installed go on my Ubuntu desktop and it worked fine before I switch off the computer.

Now as I started my machine and resumed my work on the project, I get this

$ go build
go tool: no such tool "compile"
go tool: no such tool "compile"
go tool: no such tool "compile"
go tool: no such tool "compile"
go tool: no such tool "compile"

When I try to build a project.

The only thing that I did before poweroff which might have some effect was to install godoc using

sudo apt-get install golang-doc

I had install go directly by downloading go1.10.1.linux-amd64.tar.gz file, not using apt-get

go env

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/me/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/me/go"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build255010769=/tmp/go-build -gno-record-gcc-switches"

$ go version

go version go1.10.1 linux/amd64

I can still run comiled go code.

What could have gone wrong here? How can I fix it?

go


Solution 1:[1]

It may seem odd, but running export GOROOT= fixed the issue for me.

Solution 2:[2]

Downloading .tar.gz versus using apt-get may cause go env values conflicts/overrides. Better to stick to one installation approach.

I had faced a similar problem in Mac. When I installed Go using .dmg (similar to your .tar.gz case), it set a completely different value for GOTOOLDIR, versus when I installed Go using brew install golang (similar to your apt-get case).

Analysis:

If you do the following:

go env | grep GOTOOLDIR
cd <value of GOTOOLDIR>
ls

You should see the following (notice the compile binary):

addr2line   buildid     cover       fix     objdump     test2jsonapi        
cgo         dist        link        pack    trace
asm         compile     doc         nm      pprof       vet

If you don't, then you have probably switched to the wrong/overwritten/earlier Go installation.

Solution:

  1. If you are using an IDE, it's easy to switch among multiple Go SDKs by going to Settings or Preferences. Then restart the Terminal in IDE
  2. If you are not using an IDE, a simple solution is to uninstall both the Go installations. Then reinstall Go using only one method. And use the same method to install any other Go related stuff.

Solution 3:[3]

After upgrading go I started getting this error and updating GOROOT from /Users/bharkum3/.go to /Users/bharkum3/.go/go-1.18.2 worked for me.

Looks like go install is creating a separate folder now for each versions (our script is doing this I guess) and need the above to redirect the compiler to the right version.

Solution 4:[4]

I was facing the same issue in my MAC M1.

Previously, I had downloaded it using "brew". I tried downloading it manually and reinstalling it. That worked for me.

go version: 1.18.2 Download: https://go.dev/dl/

Solution 5:[5]

Ubuntu 18 specific solution:

  • removed manually installed Go SDK and Go SRC
  • removed manually set GOROOT variable
  • installed Golang package from golang-backports:
sudo add-apt-repository ppa:longsleep/golang-backports
sudo apt-get install golang-1.15
  • Updated my ${HOME}/.bashrc with:
export PATH=$PATH:/usr/lib/go-1.15/bin
  • Rebooted the OS

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 OlegG
Solution 2 Abhinav Sureka
Solution 3 Bharath Kumar
Solution 4 Alok Tripathi
Solution 5 Dan M