WSL Ubuntu环境下Go无法从GitHub下载依赖问题求助
Go模块依赖下载失败(WSL Ubuntu环境)
执行go get...或go mod tidy时出现如下错误:
server response: not found: github.com/...@v0.0.0-20221103104148-6de67823a269: invalid version: git ls-remote -q origin in /tmp/gopath/pkg/mod/cache/vcs/1be23be97dc50217968ea7f116bfef7f3861715200675bd9dd7563645ba58690: exit status 128: fatal: could not read Username for 'https://github.com': terminal prompts disabled Confirm the import path was entered correctly. If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.
已尝试的操作
- 更新Git配置添加
insteadOf规则,当前配置:
$ git config --list user.name=user-name user.email=user@gmail.com url.ssh://git@github.com/.insteadof=https://github.com/ core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true
- 已将SSH公钥添加至GitHub,可正常克隆仓库,认证环节无问题。
当前Go环境变量
$ go env GO111MODULE="" GOARCH="amd64" GOBIN="/usr/local/go/bin" GOCACHE="/home/user/.cache/go-build" GOENV="/home/user/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GOMODCACHE="/home/usr/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/home/user/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GOVCS="" GOVERSION="go1.19.3" GCCGO="gccgo" GOAMD64="v1" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="/home/test/test-wsl/go.mod" GOWORK="" 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 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1637247891=/tmp/go-build -gno-record-gcc-switches"
解决方案
1. 配置GOPRIVATE环境变量
如果依赖的是GitHub私有仓库,需要告知Go跳过代理直接用SSH拉取:
# 针对指定私有仓库配置 go env -w GOPRIVATE="github.com/你的用户名/*,github.com/你的组织名/*" # 若大部分依赖为私有,可直接匹配所有github.com仓库 go env -w GOPRIVATE="github.com"
2. 确保Git的insteadOf配置全局生效
检查当前insteadOf规则是全局还是仅仓库级:
git config --global --list
如果是仓库级配置,改为全局生效:
git config --global url.ssh://git@github.com/.insteadOf https://github.com/
3. 启用Go模块模式
当前GO111MODULE为空,显式开启模块模式:
go env -w GO111MODULE="on"
4. 清理模块缓存后重试
# 清理模块缓存 go clean -modcache # 重新执行依赖拉取 go mod tidy
5. 检查SSH代理状态(若使用)
如果WSL通过SSH代理连接GitHub,确保代理正常运行:
# 启动SSH代理 eval "$(ssh-agent -s)" # 将私钥添加至代理 ssh-add ~/.ssh/id_rsa
内容的提问来源于stack exchange,提问作者Yura
相关产品推荐
相关产品推荐

