Fixing Go Command Not Found on macOS | Generated by AI
The error message /bin/sh: go: command not found
indicates that the Go programming language is not installed on your macOS system, or it’s not properly configured in your PATH. The make
command is trying to run a Go build process, which requires the Go compiler (go
). Below are the steps to install Go on macOS and resolve this issue.
Steps to Install Go on macOS
There are two primary ways to install Go on macOS: using the official Go installer or using Homebrew. I’ll cover both methods, but Homebrew is often simpler for macOS users. Choose one method based on your preference.
Prerequisites
- Ensure your macOS version is 10.10 or later for compatibility with recent Go versions.
- You need administrator access to install Go and modify system files.
- A terminal application (found in Applications > Utilities > Terminal).
Method 1: Install Go Using Homebrew (Recommended)
Homebrew is a popular package manager for macOS that simplifies software installation.
- Install Homebrew (if not already installed):
- Install Go:
- Verify Installation:
- Set Up Environment Variables (if needed):
- Homebrew typically adds Go to your PATH automatically, but if
go
commands don’t work, add the Go binary path to your shell profile:- Open or create the appropriate shell configuration file (e.g.,
~/.zshrc
for Zsh, which is default on macOS since Catalina, or~/.bash_profile
for Bash):nano ~/.zshrc
- Add the following lines:
export PATH=$PATH:/usr/local/go/bin
- Save the file (Ctrl+X, then Y, then Enter in nano) and apply the changes:
source ~/.zshrc
- If you want to use a custom workspace, set
GOPATH
(optional, as Go modules often eliminate the need for this):export GOPATH=$HOME/go export PATH=$PATH:$GOPATH/bin
- Source the file again:
source ~/.zshrc
- Open or create the appropriate shell configuration file (e.g.,
- Homebrew typically adds Go to your PATH automatically, but if
- Test Go Installation:
- Run
go version
again to ensure the command is recognized. - Optionally, create a simple Go program to confirm everything works:
mkdir -p ~/go/src/hello nano ~/go/src/hello/main.go
- Run
Method 2: Install Go Using the Official Installer
If you prefer not to use Homebrew, you can install Go using the official macOS package.
- Download the Go Installer:
- Run the Installer:
- Set Up Environment Variables:
- Open Terminal and edit your shell configuration file (e.g.,
~/.zshrc
or~/.bash_profile
):nano ~/.zshrc
- Add the following lines:
export GOROOT=/usr/local/go export GOPATH=$HOME/go export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
- Save and apply the changes:
source ~/.zshrc
- Note:
GOROOT
is optional unless you’re developing Go itself or need a non-standard installation path. Modern Go versions often don’t requireGOROOT
to be set.
- Open Terminal and edit your shell configuration file (e.g.,
- Verify Installation:
- Test Go Installation:
- Follow the same steps as in Method 1, Step 5 to create and run a “Hello, World!” program.
Troubleshooting the Original Issue
After installing Go, navigate back to your clash-core
directory and retry the make
command:
cd /path/to/clash-core
make
If you encounter issues:
- Proxy Settings: Your terminal output shows
HTTP_PROXY
andHTTPS_PROXY
set tohttp://127.0.0.1:7890
. Ensure your proxy is active and not interfering with Go’s network access (e.g., downloading dependencies). You can temporarily disable the proxy to test:unset HTTP_PROXY HTTPS_PROXY make
- Permissions: If you get permission errors, ensure you have write access to the project directory and Go workspace (
$GOPATH
or$HOME/go
). - Go Modules: The
clash-core
project likely uses Go modules. Ensure you’re in the correct directory containinggo.mod
, and rungo mod tidy
to fetch dependencies beforemake
:go mod tidy make
- Architecture Mismatch: The
make
command is building forlinux-amd64
(GOOS=linux GOARCH=amd64
). If you intend to run the binary on macOS, you may need to modify the Makefile or build command to targetdarwin-amd64
(for Intel Macs) ordarwin-arm64
(for Apple Silicon). Check the Makefile for thelinux-amd64
target and adjust it, or run:GOARCH=amd64 GOOS=darwin CGO_ENABLED=0 go build -trimpath -ldflags '-X "github.com/Dreamacro/clash/constant.Version=1.18" -X "github.com/Dreamacro/clash/constant.BuildTime=Sat Jun 28 12:24:27 UTC 2025" -w -s -buildid=' -o bin/clash-darwin-amd64
Replace
amd64
witharm64
if you’re on Apple Silicon.
Additional Notes
- Uninstalling Previous Go Versions: If Go was previously installed, remove it to avoid conflicts:
sudo rm -rf /usr/local/go sudo rm -f /etc/paths.d/go
- Go Workspace: If you set
GOPATH
, create the directory:mkdir -p $HOME/go/{bin,src,pkg}
However, with Go modules (used by most modern projects like
clash-core
),GOPATH
is less critical. - Xcode Command Line Tools: Some Go projects require C dependencies (e.g.,
CGO_ENABLED=1
). Install Xcode Command Line Tools if needed:xcode-select --install
- Multiple Go Versions: If you need multiple Go versions, consider using a version manager like
gvm
(install via Homebrew or script).
Verification
Once Go is installed and the make
command succeeds, the clash-core
binary should appear in the bin/
directory (e.g., bin/clash-linux-amd64
or bin/clash-darwin-amd64
). Verify it works by running:
./bin/clash-darwin-amd64 --version
If you need further assistance or run into specific errors, let me know the exact error message or context, and I can provide more targeted help!