Skip to content

Install Go on macOS

This guide will help you install Go language on macOS system.

Download Go

First, download the Go installation package suitable for your macOS system from the official website.

Choose the corresponding version based on your system architecture:

  • darwin-amd64 - Intel Mac (x86-64 architecture)
  • darwin-arm64 - Apple Silicon Mac (ARM64 architecture)

Installation Methods

This is the simplest installation method, suitable for most users.

1. Download the installer

Download the .pkg installation package from the official website.

2. Run the installer

Double-click the downloaded .pkg file and follow the installer's prompts to complete the installation.

The installer will automatically:

  • Install Go to /usr/local/go (Intel Mac) or /opt/homebrew/opt/go (Apple Silicon Mac)
  • Configure the necessary environment variables

3. Verify installation

Open a new terminal window and execute:

bash
go version

If the installation is successful, you should see output similar to:

go version go1.21.3 darwin/arm64

Method 2: Using Archive Package

This method is suitable for users who need to manage multiple Go versions or install to a custom path.

1. Download the archive package

Download the .tar.gz compressed package from the official website.

2. Delete old version (if any)

bash
sudo rm -rf /usr/local/go

3. Extract to /usr/local

bash
sudo tar -C /usr/local -xzf go1.21.3.darwin-amd64.tar.gz

4. Configure environment variables

Add the following to your ~/.zshrc or ~/.bash_profile file:

bash
export PATH=$PATH:/usr/local/go/bin

5. Make environment variables take effect

For zsh (default shell in macOS Catalina and later):

bash
source ~/.zshrc

For bash:

bash
source ~/.bash_profile

6. Verify installation

bash
go version

Method 3: Using Homebrew

If you use Homebrew package manager, you can install Go through Homebrew.

1. Install Go

bash
brew install go

2. Verify installation

bash
go version

3. Upgrade Go

bash
brew upgrade go

Custom Installation Path

If you want to install Go to a custom path, you can:

1. Extract to custom directory

bash
mkdir -p ~/go
tar -C ~/go -xzf go1.21.3.darwin-arm64.tar.gz

2. Configure environment variables

Add the following to your ~/.zshrc or ~/.bash_profile file:

bash
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin

3. Make environment variables take effect

bash
source ~/.zshrc

Configure GOPATH

By default, Go uses $HOME/go as the workspace directory. If you want to use a different path, you can set the GOPATH environment variable:

bash
export GOPATH=$HOME/gopath
export PATH=$PATH:$GOPATH/bin

Verify Installation

Create a simple Go program to verify that the installation is successful:

bash
mkdir -p $HOME/hello
cd $HOME/hello
cat > hello.go << 'EOF'
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}
EOF
go run hello.go

If the output is Hello, World!, it means the installation is successful.

Uninstall

Uninstall Installer Package Version

If you installed Go using the installer package, execute:

bash
sudo rm -rf /usr/local/go

Or for Apple Silicon Mac:

bash
sudo rm -rf /opt/homebrew/opt/go

Uninstall Homebrew Version

If you installed Go using Homebrew, execute:

bash
brew uninstall go

Uninstall Custom Path Version

If you installed Go to a custom path, just delete the corresponding directory:

bash
rm -rf ~/go

Then remove the Go-related environment variables from your ~/.zshrc or ~/.bash_profile file.

Troubleshooting

Command not found

If the go command is not found, please check:

  1. Whether the PATH environment variable is configured correctly
  2. Whether the environment variables have taken effect
  3. Try to restart the terminal or re-login

Version mismatch

If the displayed version does not match the installed version, please check:

  1. Whether there are multiple Go installations
  2. Whether the PATH environment variable points to the correct Go installation directory
  3. Execute which go to view the actual path of the go command

Permission denied

If you encounter a permission denied error, please use sudo to execute the command:

bash
sudo tar -C /usr/local -xzf go1.21.3.darwin-arm64.tar.gz

Next Steps

After installation is complete, you can:

Golang by www.golangdev.cn edit