|
| 1 | +--- |
| 2 | +title: "Fixing Go Command Not Found on macOS" |
| 3 | +lang: en |
| 4 | +layout: post |
| 5 | +audio: false |
| 6 | +translated: false |
| 7 | +generated: true |
| 8 | +--- |
| 9 | + |
| 10 | +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. |
| 11 | + |
| 12 | +### Steps to Install Go on macOS |
| 13 | + |
| 14 | +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. |
| 15 | + |
| 16 | +#### Prerequisites |
| 17 | +- Ensure your macOS version is 10.10 or later for compatibility with recent Go versions.[](https://tecadmin.net/install-go-on-macos/) |
| 18 | +- You need administrator access to install Go and modify system files. |
| 19 | +- A terminal application (found in Applications > Utilities > Terminal). |
| 20 | + |
| 21 | +#### Method 1: Install Go Using Homebrew (Recommended) |
| 22 | +Homebrew is a popular package manager for macOS that simplifies software installation. |
| 23 | + |
| 24 | +1. **Install Homebrew (if not already installed)**: |
| 25 | + - Open Terminal and run: |
| 26 | + ```bash |
| 27 | + /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" |
| 28 | + ``` |
| 29 | + - Follow the on-screen instructions to complete the installation.[](https://www.digitalocean.com/community/tutorials/how-to-install-go-and-set-up-a-local-programming-environment-on-macos) |
| 30 | + |
| 31 | +2. **Install Go**: |
| 32 | + - Run the following command to install the latest version of Go: |
| 33 | + ```bash |
| 34 | + brew install go |
| 35 | + ``` |
| 36 | + - This installs Go to `/usr/local/Cellar/go` (or a similar path) and adds the Go binary to `/usr/local/bin`.[](https://www.feliciano.tech/blog/how-to-install-go-on-linux-macos/)[](https://formulae.brew.sh/formula/go) |
| 37 | + |
| 38 | +3. **Verify Installation**: |
| 39 | + - Check the installed Go version by running: |
| 40 | + ```bash |
| 41 | + go version |
| 42 | + ``` |
| 43 | + - You should see output like `go version go1.23.x darwin/amd64`, confirming Go is installed.[](https://tecadmin.net/install-go-on-macos/) |
| 44 | + |
| 45 | +4. **Set Up Environment Variables** (if needed): |
| 46 | + - Homebrew typically adds Go to your PATH automatically, but if `go` commands don't work, add the Go binary path to your shell profile: |
| 47 | + - 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): |
| 48 | + ```bash |
| 49 | + nano ~/.zshrc |
| 50 | + ``` |
| 51 | + - Add the following lines: |
| 52 | + ```bash |
| 53 | + export PATH=$PATH:/usr/local/go/bin |
| 54 | + ``` |
| 55 | + - Save the file (Ctrl+X, then Y, then Enter in nano) and apply the changes: |
| 56 | + ```bash |
| 57 | + source ~/.zshrc |
| 58 | + ``` |
| 59 | + - If you want to use a custom workspace, set `GOPATH` (optional, as Go modules often eliminate the need for this): |
| 60 | + ```bash |
| 61 | + export GOPATH=$HOME/go |
| 62 | + export PATH=$PATH:$GOPATH/bin |
| 63 | + ``` |
| 64 | + - Source the file again: |
| 65 | + ```bash |
| 66 | + source ~/.zshrc |
| 67 | + ``` |
| 68 | +
|
| 69 | +5. **Test Go Installation**: |
| 70 | + - Run `go version` again to ensure the command is recognized. |
| 71 | + - Optionally, create a simple Go program to confirm everything works: |
| 72 | + ```bash |
| 73 | + mkdir -p ~/go/src/hello |
| 74 | + nano ~/go/src/hello/main.go |
| 75 | + ``` |
| 76 | + - Add the following code: |
| 77 | + ```go |
| 78 | + package main |
| 79 | + import "fmt" |
| 80 | + func main() { |
| 81 | + fmt.Println("Hello, World!") |
| 82 | + } |
| 83 | + ``` |
| 84 | + - Save and exit (Ctrl+X, Y, Enter), then compile and run: |
| 85 | + ```bash |
| 86 | + cd ~/go/src/hello |
| 87 | + go run main.go |
| 88 | + ``` |
| 89 | + - You should see `Hello, World!` as output.[](https://www.digitalocean.com/community/tutorials/how-to-install-go-and-set-up-a-local-programming-environment-on-macos) |
| 90 | +
|
| 91 | +#### Method 2: Install Go Using the Official Installer |
| 92 | +If you prefer not to use Homebrew, you can install Go using the official macOS package. |
| 93 | +
|
| 94 | +1. **Download the Go Installer**: |
| 95 | + - Visit the official Go download page: https://go.dev/dl/ |
| 96 | + - Download the macOS package (`.pkg`) for your system architecture (e.g., `go1.23.x.darwin-amd64.pkg` for Intel Macs or `go1.23.x.darwin-arm64.pkg` for Apple Silicon).[](https://medium.com/%40priyamjpatel/installing-go-on-a-mac-machine-bca6746fff0b)[](https://golangdocs.com/install-go-mac-os) |
| 97 | +
|
| 98 | +2. **Run the Installer**: |
| 99 | + - Double-click the downloaded `.pkg` file in Finder. |
| 100 | + - Follow the on-screen instructions to install Go. It will be installed to `/usr/local/go` by default. |
| 101 | + - You may need to enter your administrator password.[](https://www.scaler.com/topics/golang/install-golang/)[](https://golangdocs.com/install-go-mac-os) |
| 102 | +
|
| 103 | +3. **Set Up Environment Variables**: |
| 104 | + - Open Terminal and edit your shell configuration file (e.g., `~/.zshrc` or `~/.bash_profile`): |
| 105 | + ```bash |
| 106 | + nano ~/.zshrc |
| 107 | + ``` |
| 108 | + - Add the following lines: |
| 109 | + ```bash |
| 110 | + export GOROOT=/usr/local/go |
| 111 | + export GOPATH=$HOME/go |
| 112 | + export PATH=$GOPATH/bin:$GOROOT/bin:$PATH |
| 113 | + ``` |
| 114 | + - Save and apply the changes: |
| 115 | + ```bash |
| 116 | + source ~/.zshrc |
| 117 | + ``` |
| 118 | + - Note: `GOROOT` is optional unless you're developing Go itself or need a non-standard installation path. Modern Go versions often don't require `GOROOT` to be set.[](https://stackoverflow.com/questions/12843063/install-go-with-brew-and-running-the-gotour)[](https://tecadmin.net/install-go-on-macos/) |
| 119 | +
|
| 120 | +4. **Verify Installation**: |
| 121 | + - Run: |
| 122 | + ```bash |
| 123 | + go version |
| 124 | + ``` |
| 125 | + - You should see the installed Go version (e.g., `go version go1.23.x darwin/amd64`).[](https://golangdocs.com/install-go-mac-os) |
| 126 | +
|
| 127 | +5. **Test Go Installation**: |
| 128 | + - Follow the same steps as in Method 1, Step 5 to create and run a "Hello, World!" program. |
| 129 | +
|
| 130 | +#### Troubleshooting the Original Issue |
| 131 | +After installing Go, navigate back to your `clash-core` directory and retry the `make` command: |
| 132 | +```bash |
| 133 | +cd /path/to/clash-core |
| 134 | +make |
| 135 | +``` |
| 136 | +
|
| 137 | +If you encounter issues: |
| 138 | +- **Proxy Settings**: Your terminal output shows `HTTP_PROXY` and `HTTPS_PROXY` set to `http://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: |
| 139 | + ```bash |
| 140 | + unset HTTP_PROXY HTTPS_PROXY |
| 141 | + make |
| 142 | + ``` |
| 143 | +- **Permissions**: If you get permission errors, ensure you have write access to the project directory and Go workspace (`$GOPATH` or `$HOME/go`). |
| 144 | +- **Go Modules**: The `clash-core` project likely uses Go modules. Ensure you're in the correct directory containing `go.mod`, and run `go mod tidy` to fetch dependencies before `make`: |
| 145 | + ```bash |
| 146 | + go mod tidy |
| 147 | + make |
| 148 | + ``` |
| 149 | +- **Architecture Mismatch**: The `make` command is building for `linux-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 target `darwin-amd64` (for Intel Macs) or `darwin-arm64` (for Apple Silicon). Check the Makefile for the `linux-amd64` target and adjust it, or run: |
| 150 | + ```bash |
| 151 | + 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 |
| 152 | + ``` |
| 153 | + Replace `amd64` with `arm64` if you're on Apple Silicon. |
| 154 | + |
| 155 | +#### Additional Notes |
| 156 | +- **Uninstalling Previous Go Versions**: If Go was previously installed, remove it to avoid conflicts: |
| 157 | + ```bash |
| 158 | + sudo rm -rf /usr/local/go |
| 159 | + sudo rm -f /etc/paths.d/go |
| 160 | + ``` |
| 161 | + Then reinstall using one of the methods above.[](https://hostman.com/tutorials/how-to-install-go-on-macos/)[](https://go.dev/doc/install) |
| 162 | +- **Go Workspace**: If you set `GOPATH`, create the directory: |
| 163 | + ```bash |
| 164 | + mkdir -p $HOME/go/{bin,src,pkg} |
| 165 | + ``` |
| 166 | + However, with Go modules (used by most modern projects like `clash-core`), `GOPATH` is less critical.[](https://www.geeksforgeeks.org/how-to-install-golang-on-macos/) |
| 167 | +- **Xcode Command Line Tools**: Some Go projects require C dependencies (e.g., `CGO_ENABLED=1`). Install Xcode Command Line Tools if needed: |
| 168 | + ```bash |
| 169 | + xcode-select --install |
| 170 | + ``` |
| 171 | + [](https://www.digitalocean.com/community/tutorials/how-to-install-go-and-set-up-a-local-programming-environment-on-macos) |
| 172 | +- **Multiple Go Versions**: If you need multiple Go versions, consider using a version manager like `gvm` (install via Homebrew or script).[](https://jimkang.medium.com/install-go-on-mac-with-homebrew-5fa421fc55f5) |
| 173 | + |
| 174 | +#### Verification |
| 175 | +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: |
| 176 | +```bash |
| 177 | +./bin/clash-darwin-amd64 --version |
| 178 | +``` |
| 179 | + |
| 180 | +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! |
0 commit comments