Log In

How to Install Go on MacOS

How to Install Go on MacOS
19.02.2024
Reading time: 7 min
Hostman Team
Technical writer

MacOS is an operating system for desktop computers and tablets developed by Apple specifically for its devices. It is initially pre-installed on all Apple devices, specifically the Apple Macintosh or Mac for short.

Unlike Linux, macOS is a proprietary operating system, which, of course, brings certain peculiarities in installing various development tools on it.

In this article, we will take a detailed look at how to install Go on a macOS computer.

Golang, or simply Go, is an open source programming language developed by Google to create applications based on microservice architecture.

To ensure the stability of your computer and the Go compiler, we recommend using the latest version of macOS.

1. Uninstall old Golang versions

Check if Golang is already installed

Before you start installing Golang, first check if it is installed on your system already. A simple way to do this is to run a command that outputs the Golang version:

go version

If Go is indeed installed, you will see a message in the console terminal displaying the language version and the operating system's name. Something like this:

go version go1.21.3 darwin/amd64

Uninstall Golang

If Go is present on your system, you need to uninstall it to avoid possible installation conflicts.

MacOS stores files from the Golang package in a predetermined location:

  • The /usr/local/go directory. This is where Golang itself is placed.

  • The /etc/paths.d/go file. Golang environment variables are specified here.

So, to uninstall Golang, you need to clear the above directories:

rm -rf /usr/local/go
rm -rf /etc/paths.d/go

The rm command deletes a directory or a file, while the -rf flag indicates a recursive-forced type of deletion.

  • r stands for recursive and is used to delete the specified folder, all its subfolders, subfolders of subfolders, etc.

  • f stands for force so no external states or variables can prevent the deletion from occurring

Great! Golang is now removed from your computer. This means we can move on to downloading the Golang package for macOS and then installing it.

2. Download Golang

There are two ways to download the Go language archive to your computer. One is manual, and the other is more automatic. Let's look at both.

Manual download

The official Golang website has a special page with links to download the latest version of Go.

Once you open it, you will see several buttons leading to the latest language version for a particular platform. We are interested in the Apple operating system.

At the moment of writing this article, there are two versions of the language for MacOS. One is for the new Apple ARM64 processor architecture, and the other is for the classic Intel 65-bit architecture.

You should choose the one that suits your device. The latest Mac models have ARM64 processors.

Clicking on the link will start downloading the archive file named go1.21.3.darwin-amd64.pkg, or a later version.

Download via console

An alternative to downloading manually is using the command line.

MacOS has a special curl tool included in the operating system.

So we can use the curl utility with the exact URL where the Golang archive file is available:

curl -o golang.pkg https://dl.google.com/go/go1.21.3.darwin-amd64.pkg

This command uses a special flag -o (--output), which ensures that the data received through the curl request is written to the golang.pkg file.

Note that the URL contains the exact name of the file we want to download and the Golang version.

When the curl command is finished, we will have a golang.pkg file containing the Golang language package. Then we just need to install it.

3. Install the Go package

As with the download, installation is also available in two ways: through the GUI or the command line interface.

Installing via GUI

To install Go on macOs, simply run the downloaded package. 

After the automatic installation is done, you will get a success message confirming that the software is installed.

Installing via command line

If you prefer working with the terminal, run the following command:

sudo open golang.pkg

Then follow the terminal prompts until a similar window appears, informing about the successful installation.

4. Set environment variables

After installation, we must tell the system where to find the Golang compiler when the console terminal receives the command to compile and run the application.

First, let's navigate to the home directory using the following command:

cd ~

Now add the locations of the Golang components to .bash_profile. This file is automatically loaded when you log in to your macOS account and contains all the startup configurations for the command line interface.

Add environment variables to the end of the file, either manually or via the echo command:

echo "export GOROOT=/usr/local/go" >> .bash_profile
echo "export GOPATH=$HOME/Documents/go" >> .bash_profile
echo "export PATH=$GOPATH/bin:$GOROOT/bin:$PATH" >> .bash_profile

The >> operator indicates that the text in quotes after echo will be written to the .bash_profile file.

The GOROOT variable points to the directory where the Go compiler is installed. GOPATH contains the address of the Go working directory. And PATH helps the command line to find binary files during source compilation.

5. Check the installation

To verify that Golang has been successfully installed on macOS, you need to restart the command line terminal and query the Go version:

go version

If the installation was done correctly, the console will display a message:

go version go1.21.3 darwin/amd64

6. Launch a test application

In this article we won't go into the details of Golang syntax and peculiarities of programming in this language. We will just write, compile, and run a simple program with trivial output to the console to make sure that the installed compiler works.

Let's create a new file in our home directory using the nano editor:

nano main.go

Then fill it with the following contents:

package main

import "fmt"

func main() {
     fmt.Println("Hello, World!") // CONCLUSION: Hello, World!
}

To exit nano, press "CTRL+X". When prompted to save the file, press "Y", then "ENTER".

Now we can compile and run our program with just one command:

go run main.go

There is also another command that builds the application source code into a complete executable file that can be distributed and deployed to other local machines:

go build

If you don't specify the name of the go file as an argument, the command will compile the file with the standard name main.go.

For example, if the file containing our program were named test.go, the build command would look like this:

go build test.go

During build, the Go compiler will include all the .go files involved in the final "build", adding the auxiliary code needed to run the application on any computer with the same system architecture.

Building to an executable file allows programs to run on other computers regardless of whether the Golang compiler itself is directly installed on them.

Conclusion

Despite being a proprietary operating system, macOS allows you to install tools from third-party companies and developers (in our case, Google), including open-source solutions.

In this article, we have looked at the standard way of installing the Golang compiler on macOS, which includes a few basic steps:

  • Checking for older versions

  • Uninstalling the old versions if they exist

  • Downloading the package from the official website (manually or automatically)

  • Installing the downloaded package (via GUI or terminal)

  • Adding environment variables

  • Checking if the installation is correct

  • Compiling and running a simple code

With these steps, we installed Go on macOS and ran our first program using fairly simple commands. For further study of the language and deeper familiarization with its syntax, we recommend checking the documentation on the official Golang website.

Share