Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save felipemeamaral/7ef508e59a42c4fa0d0b74f219f726c1 to your computer and use it in GitHub Desktop.
Save felipemeamaral/7ef508e59a42c4fa0d0b74f219f726c1 to your computer and use it in GitHub Desktop.
Install Go using asdf on macOS

Install Go using asdf for Visual Studio Code on macOS

I had a lot of issues trying to install Golang on macOS using asdf package manager to develop on Visual Studio Code.

So here's the steps needed to setup it properly:

Open Terminal and install asdf with this command:

You have to install Homebrew before running the installation command.

brew install asdf

Don't forget to setup asdf on your shell by following asdf documentation.

After it finishes, it's time to install Go:

asdf install go 1.20.2

Set the version that we just installed as Global:

asdf global golang 1.20.2

Create links to use the go command without any suffix on Terminal:

asdf reshim golang 1.20.2

Create Go package and source folder

mkdir -p ~/go/{bin,pkg,src}

If you get an error message, remove the folder with this command:

sudo rm -rf ~/go

Re-run the command above to create package and source folders. Note: You have to run it as sudo because Go changes this folder permissions.

Add GOROOT to your shell config file:

Add this line to your shell config file:

ZSH users: file is located at ~/.zshrc Bash users: file is located at ~/.zshrc

export GOROOT=~/.asdf/installs/golang/1.20.2/go

Reload your config file.

If you're using ZSH run this command on Terminal:

source ~/.zshrc

If you're using Bash run this command on Terminal:

source ~/.bashrc

Add these lines to your Visual Studio Code User Settings (bash):

Press Ctrl(Cmd) + Shift + P type User Settings bash and hit ENTER.

On this window paste the code down below and don't forget to Save:

"go.toolsGopath": "~/go/",
"go.gopath": "~/go/",

Install Go extension on Visual Studio Code

Open this URL and hit Install.

Install Go Tools

Press Ctrl(Cmd) + Shift + P type go install tools and hit ENTER. After you get a message like:

All tools successfully installed. You are ready to Go. :)

Reopen Visual Studio Code and everything should be working flawlessly.

@mybigman
Copy link

you should have just installed gobrew and be done with it.

@mfts
Copy link

mfts commented Mar 20, 2023

Hey @felipemeamaral, I just came across your thread on Reddit and I totally understand your pain in managing versions.

I'm working with an open-source package manager called tea from the creator of homebrew.
With tea you can use any version of go without the versioning pain.

tea +go.dev~1.20.1 will install go in a ~/.tea/go.dev/v1.20.1/ directory. It's isolated from your system installs so no conflicts.

@airtonix
Copy link

airtonix commented Apr 17, 2023

Hey @felipemeamaral, I just came across your thread on Reddit and I totally understand your pain in managing versions.

I'm working with an open-source package manager called tea from the creator of homebrew. With tea you can use any version of go without the versioning pain.

tea +go.dev~1.20.1 will install go in a ~/.tea/go.dev/v1.20.1/ directory. It's isolated from your system installs so no conflicts.

Yeah but, asdf already does this. What does tea bring to the party that asdf doesn't?

The biggest issue with homebrew is that you cannot create an installation manifest that pins software to a specific version. This is one of the many awesome things about asdf.

edit: ok so i looked at your project, and it looks fairly similar to asdf, but I have questions like:

  • asdf can be taught how to manage the versions of any piece of software via community plugins, how does tea account for this?
  • Initially I missed the fact that the cargo.toml was the "tea manifest". perhaps you should work out how to have th filename be teafile.toml ? (so it's incredibly obvious)

@kimdcottrell
Copy link

Thank you! This helped me a lot. I wanted to easily swap between go versions so I wanted to use asdf, though vscode was giving me grief.

@therealvio
Copy link

therealvio commented Nov 18, 2023

Thanks for this @felipemeamaral! ❤️

A small item of feedback if you don't mind: the GOROOT env var can be updated using a script provided by the asdf golang plugin. So instead of specifying the static version of golang for the GOROOT variable like you did, you can match the version of the golang you're running by referencing this script. Check out this part of their readme here. You can verify this using:

echo $GOROOT which should return something like /Users/username/.asdf/installs/golang/1.21.4/go; and
which go which should return /Users/username/.asdf/shims/go

As you have indicated, you do still have to reload your config every time still to make sure the switch happens :)
Update: You shouldn't need to reload your shell after setting this up. Re-shimming after an install/version pin may be required though.

An aside: I personally encountered a weird issue with vscode-go where its tools and binaries where being placed into the root of my projects and it had to do with it not being able to reconcile the version of go used partly because of the GOROOT env var, but also because homebrew installed a version of go to go along with golangci-lint which was interfering with which go install to use. Although golangci-lint recommend you use their pre-made binaries to avoid issues, you can use their docker image as an alternative. This is depicted in this commit for a personal project.

@pythoninthegrass
Copy link

pythoninthegrass commented Jan 31, 2024

To @therealvio's point about not hardcoding relative paths in shell configs, this is what I've got in mine and it appears to do the thing:

export ASDF_DIR="$HOME/.asdf"
export GOPATH="$ASDF_DIR/shims"
export GOROOT="$(go env GOROOT)"
export GOCACHE=$HOME/go/cache
export GOPROXY="https://proxy.golang.org,direct"
export PATH="$GOPATH/bin:$PATH"

Appreciate the hot tip for settings.json pointed at the ~/go directory. Mine looks like this now:

{
  "go.toolsGopath": "~/go/",
  "go.gopath": "~/go/",
  "go.testTimeout": "180s",
  "go.toolsEnvVars": {
    "GOPROXY": "https://proxy.golang.org,direct"
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment