Cannot Install Delve Go Debugger on Mac
Asked Answered
D

2

11

I am trying to follow along with the following YouTube video on getting started with Go Debugging.

It recommends following the Delve installation instructions on the official Delve github repo. For Mac users, they are as follows:

Making sure the toolchain is in place

xcode-select --install
xcode-select: error: command line tools are already installed, use "Software Update" to install updates

Using "go get" to install Delve

go get -u github.com/go-delve/delve/cmd/dlv

Making sure developer mode is enabled in Xcode

sudo /usr/sbin/DevToolsSecurity -enable
Developer mode is already enabled.

To check that the installation has been completed correctly, I tried running the following in my Go project directlry:

dlv debug
zsh: command not found: dlv

The author of the video tutorial recommends updating GOPATH and PATH variables in the ~/.bash_profile file in the case that the command is not recognized. I did so so by adding:

export GOPATH=/Users/<user_name>/go/src/
export PATH=$PATH:/Users/<user_name/go/src/my_project

However, even after doing so, I get the same result when trying to run the debugger:

dlv debug
zsh: command not found: dlv

Even if I change the shell for the default zsh to bash, using exec bash, I get the same result.

Dogcart answered 29/11, 2019 at 20:50 Comment(0)
Y
31

In order to run an executable, it needs to be available in your PATH.

1. Configure your path.

Make sure that your GOPATH and $GOPATH/bin directories are set correctly in your shell environment. You can do this by adding the following lines to your shell configuration.

~/.zshrc if you're using zsh.

~/.bash_profile if you're using bash.

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

2. Re-load your shell configuration.

Make sure to either restart your shell or run source on your shell configuration file after the changes:

source ~/.zshrc if you're using zsh.

source ~/.bash_profile if you're using bash.


3. Install the dlv package.

go install github.com/go-delve/delve/cmd/dlv

This is assuming that you're using /Users/<username>/go as your GOPATH.


You should now be able to run dlv from your terminal session.

Good luck!

Yearling answered 29/11, 2019 at 21:2 Comment(13)
what is the purpose of the semi-colon at the end of the second line?Dogcart
Followed the steps you suggested, both with and without the semi-colon. Still getting the same result zsh: command not found: dlvDogcart
Sorry, the ;is a typo. You can leave it out. I will edit the post.Yearling
After you run the go install, is the executable inside $GOPATH/bin?Yearling
If you have added $GOPATH/bin to your PATH, and the executable is in $GOPATH/bin - I'm not sure why you're getting troubles. Are you sure you are editing the correct shell config? i.e. .zshrc if you're using ZSH and .bash_profile if you're using bash.Yearling
I've only edited .bash_profile. Then I tried running it from both bash and zsh, switching between the two with the exec command.Dogcart
If zsh is your main shell, then rather add the lines into your ~/.zshrc file. If you're wanting to use bash, then use chsh to change your default shell to bash and restart your terminal session.Yearling
I haven't used dlv previously, as I usually debug using Goland. I just followed by own instructions and successfully debugged some tests that I'm having issues with using dlv - I'm using zsh and Mac OS.Yearling
This looks perfect, should work fine. (I have the same setup, MacOS and Zsh)Ara
Following your updated instructions, I now get a new error message: ``` can'tload package: package .: no Go files in /Users/<user_name>/go/bin exit status 1```Dogcart
Did you replace <user_name> with your username?Yearling
Yes, I replaced user_nameDogcart
Can you post the output of echo $GOPATH and echo $PATH?Yearling
S
10

Set the environment variable GOBIN to be where you want the dlv binary to be installed.

For example:

GOBIN=~/bin go install github.com/go-delve/delve/cmd/dlv

This will install dlv in ~/bin

Clarification

When you run go install, the installation path can be specified by setting the GOBIN environment variable.

There are two ways to set the environment variable:

1) Run export GOBIN=<SOMETHING> before you run go install ..

$ export GOBIN="$HOME/bin"
$ go install github.com/go-delve/delve/cmd/dlv

The export command will alter the environment in the current terminal session. Any later command you execute will see the value you set for GOBIN

When you go with this approach, you usually want to have this environment variable active not only in this session, but all future sessions as well. So it's better to add the line to your bash profile.

2) Set the environment variable only for the command.

$ A=10 some-command

In this case, some-command will see the value of the environment variable A set to '10'. If you run a later command, it will not see this value.

This approach is useful when you are just trying things out, or if you only want to set certain environment variables in certain situations.

The command line I provided as the answer follows this second approach.

It sets the GOBIN variable to the ~/bin directory, and then invokes go install in the same line. This way, this invocation of go install will install dlv in ~/bin

This of course assumes you have a bin directory in your home directory.

If you don't have such directory, then this will not work.

The idea is not to copy paste the line as is. The idea is to change ~/bin to be the directory where you would like the dlv binary to be installed.

Sting answered 6/12, 2019 at 14:4 Comment(7)
Should I write this as export GOBIN=~/... or just insert it as you have listed it in my source ~/.bash_profile?Dogcart
You can do it inline like I showed here without the export keyword. If you want it to always be set then you can add it to your bash profile as export GOBIN=~/binSting
After adding your line, with the export and then running source ~/.bash_profile, I get the following error message: bash: export: `github.com/go-delve/delve/cmd/dlv': not a valid identifier Dogcart
Define "basic understanding"?Dogcart
In your bash profile, only put export GOBIN=~/bin. Don't put the go install .. as part of the export line.Sting
I added some clarifications. Does it clear things up, or do you still have some confusions?Sting
Looks like we are making some progress now. I am running dlv debug```` from my ~./go/bin``` file, but I am getting the following errror: can't load package: package .: no Go files in /Users/<username>/go/bin exit status 1. Despite the fact that I have several compiled go binaries in that folder.Dogcart

© 2022 - 2025 — McMap. All rights reserved.