How to Install GoLang Using Brew

Sheeraz Gul Feb 12, 2024
  1. Install GoLang Using Brew on Linux or macOS
  2. Installing the Latest Stable Version of Go
  3. Installing a Specific Version of Go
  4. Installing Development Versions of Go
  5. Verifying the Installation
  6. Setting Up GOPATH and PATH
  7. Conclusion
How to Install GoLang Using Brew

Go, also known as Golang, is a powerful and efficient programming language that has gained popularity for its simplicity and performance. If you are a Linux or macOS user and want to set up Go on your machine, Homebrew provides a straightforward and convenient way to do so.

This tutorial demonstrates different methods on how to install GoLang using Brew on Linux or macOS, providing example codes and explanations for each step.

Install GoLang Using Brew on Linux or macOS

The brew installs missing packages in Linux and macOS. It makes it easy to install Golang on Linux or macOS.

Follow the steps below to install Golang on Linux or macOS using the brew command.

First, brew should be installed; if it is not already installed, run the command mentioned below. It may need admin permission and take a while.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Brew Install

We have to run a few other commands to complete the brew installation. Run the following three commands to add Homebrew to your PATH, and make sure the path is correct.

In our case:

echo '# Set PATH, MANPATH, etc., for Homebrew.' >> /home/sheeraz/.profile
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /home/sheeraz/.profile
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

To install Homebrew’s dependencies, use the following command:

sudo apt-get install build-essential

Once brew is installed, the next step is to update it and install Golang. Run the following command:

brew update && brew install golang

Brew Install Golang

The above command will install GoLang using the brew command; the next step is to set the workspace. We use the path $HOME/go to set up our workspace and run the following command:

mkdir -p $HOME/go/{bin,src,pkg}

One more important step is to set the environment for Golang; for that, we need to add the following info to the .bashrc file:

export GOPATH=$HOME/go
export GOROOT="$(brew --prefix golang)/libexec"
export PATH="$PATH:${GOPATH}/bin:${GOROOT}/bin"

Now, we can use GoLang and create a project using the src directory in the go path.

Installing the Latest Stable Version of Go

This method involves installing the latest stable version of Go using Homebrew. This ensures that you get the most up-to-date features and improvements.

# Update Homebrew to ensure you have the latest formulae
brew update

# Install the latest stable version of Go
brew install go

The brew update command updates the Homebrew package manager and fetches the latest information about available packages.

The brew install go command installs the latest stable version of Go. Homebrew takes care of downloading, configuring, and setting up the environment variables.

Installing a Specific Version of Go

Sometimes, you may need to use a specific version of Go for compatibility reasons or to match the requirements of your project. Homebrew allows you to install a specific version easily.

# Install a specific version of Go (replace X.X.X with the desired version)
brew install go@X.X.X

The brew install go@X.X.X command installs a specific version of Go. Replace "X.X.X" with the desired version number.

Homebrew will handle the installation of the specified version.

Installing Development Versions of Go

If you want to test bleeding-edge features or contribute to the Go project, you can install development versions using Homebrew.

# Install the latest development version of Go
brew install --HEAD go

The brew install --HEAD go command installs the latest development version of Go. The --HEAD flag tells Homebrew to install the latest commit from the Git repository.

Verifying the Installation

After installation, you can verify that Go is set up correctly by checking the installed version:

# Display the installed Go version
go version

This command should output the installed Go version, confirming that the installation was successful.

Setting Up GOPATH and PATH

Go requires the GOPATH and PATH environment variables to be correctly configured for your workspace. Add the following lines to your shell profile file (e.g., ~/.bashrc, ~/.zshrc, or ~/.bash_profile):

# Set GOPATH to your preferred workspace location
export GOPATH=$HOME/go

# Add Go binaries to the system PATH
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin

The export GOPATH=$HOME/go line sets the Go workspace path to ~/go. You can customize this path based on your preferences.

The export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin line adds the Go binaries directory to the system PATH, allowing you to run Go commands from any location.

Conclusion

We explored multiple methods to install Go (Golang) on macOS and Linux using Homebrew. Homebrew simplifies the process of installing different versions of Go, whether it’s the latest stable release, a specific version, or even a development version.

The guide covered the installation steps, including updating Homebrew, installing Go, and configuring the necessary environment variables (GOPATH and PATH). Examples and explanations were provided for each installation method, ensuring a thorough understanding.

By following these steps, users can effortlessly set up Go on their macOS systems, enabling them to leverage the language’s efficiency and simplicity for their development projects. The guide aims to cater to both beginners and experienced developers, offering a clear path to start coding in Go with confidence.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Go Installation