swag: Can't install swag

Describe the bug I’m unable to install swag given the instructions on the README

To Reproduce Steps to reproduce the behavior:

  1. Execute go get github.com/swaggo/swag/cmd/swag on the command line
  2. See the following error:
go get github.com/swaggo/swag/cmd/swag
# github.com/ghodss/yaml
./yaml.go:102:28: undefined: yaml.UnmarshalStrict
  1. Try to run swag
  2. See the following error:
zsh: command not found: swag

Expected behavior swag should be installed properly on my machine.

Desktop (please complete the following information):

  • OS: macOS High Sierra
  • Version: go version go1.10.3 darwin/amd64

@pei0804

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 26 (4 by maintainers)

Most upvoted comments

Path variable must be set: After running go get -v -u github.com/swaggo/swag/cmd/swag Please check /usr/local/go/bin or $HOME/go/bin where those swag executable is present.

Make sure you have that PATH in /etc/profile or $HOME/.profile

e.g. export PATH=$PATH:$HOME/go/bin

@jasonhancock, please use export PATH=$(go env GOPATH)/bin:$PATH

use this command please: go install github.com/swaggo/swag/cmd/swag@latest

@jdiedrick Looks like you have older github.com/ghodss/yaml version at local, please add the -u flag when getting swag like: go get -u github.com/swaggo/swag/cmd/swag

@jasonhancock $HOME/go/bin/swag init
Worked for me

@hariraogotit from the directory with your main.go, run $HOME/go/bin/swag or add $HOME/go/bin to your $PATH, or copy $HOME/go/bin/swag to a directory in your $PATH

In my case, I added these two lines to my environment variable(MacOS):

export GOPATH=/Users/[user-folder-name]/go
export PATH=$GOPATH/bin:$PATH

Open a new terminal, then execute the Swaggo command.

Followed the recommendation and still “zsh: command not found: swag”

I searched for the same issue and came in.

I started tracking down the cause myself.

The conclusion is that the build failed because the go.mod, go.sum file did not have the right to update.

The fundamental solution is to change the permissions of some files in Makefile to 755, or adjust the permissions as follows.

git update-index --chmod=+x go.mod go.sum

My environment is as follows.

MacBook Pro 14 / Apple M2 Pro / 16GB
macOS 13.4.1
zsh 5.9 (x86_64-apple-darwin22.0)

Go Preferences are as follows.

GOVERSION="go1.20.3"
GOPATH="/Users/minias/go"
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
GOMOD="/Users/minias/go/pkg/mod/github.com/swaggo/swag@v1.16.1/go.mod"

The solution is as follows.

cd /Users/minias/go/pkg/mod/github.com/swaggo/swag@v1.16.1
chmod 755 go.mod
chmod 755 go.sum
make build && make install

/usr/local/go/bin/go get github.com/swaggo/cli /usr/local/go/bin/go get sigs.k8s.io/yaml /usr/local/go/bin/go get github.com/KyleBanks/depth /usr/local/go/bin/go get github.com/go-openapi/jsonreference /usr/local/go/bin/go get github.com/go-openapi/spec /usr/local/go/bin/go get github.com/stretchr/testify/assert /usr/local/go/bin/go get golang.org/x/tools/go/loader /usr/local/go/bin/go build -o swag ./cmd/swag /usr/local/go/bin/go get github.com/swaggo/cli /usr/local/go/bin/go get sigs.k8s.io/yaml /usr/local/go/bin/go get github.com/KyleBanks/depth /usr/local/go/bin/go get github.com/go-openapi/jsonreference /usr/local/go/bin/go get github.com/go-openapi/spec /usr/local/go/bin/go get github.com/stretchr/testify/assert /usr/local/go/bin/go get golang.org/x/tools/go/loader /usr/local/go/bin/go install ./cmd/swag

ls -al ~/go/bin/swag
-rwxr-xr-x@ 1 minias  staff    14M  8 11 11:22 /Users/minias/go/bin/swag

go install github.com/swaggo/swag/cmd/swag@latest

Works thanks!!