Go, often referred to as Golang, is a powerful, open-source programming language designed for simplicity, efficiency, and reliability. Whether you’re developing web applications, cloud services, or command-line tools, Go is an excellent choice. This guide will walk you through the installation process for Go on Linux, Mac, and Windows, ensuring you can start coding in no time.

Installing Go on Linux

  1. Uninstall existing Go binaries: If you have an existing Go installation, it’s crucial to remove it to avoid conflicts. Execute the following command:

    $ sudo rm -rf /usr/local/go
    
  2. Download and extract Go: Download the Go archive from the official website . Then, extract it to /usr/local to create a fresh Go tree:

    $ sudo tar -C /usr/local -xzf go1.22.5.linux-amd64.tar.gz
    

    Note: Ensure you run these commands with root privileges, using sudo if necessary.

  3. Update the PATH environment variable: Add /usr/local/go/bin to your PATH by appending the following line to your $HOME/.profile or /etc/profile for a system-wide installation:

    export PATH=$PATH:/usr/local/go/bin
    
  4. Apply the changes: To immediately apply the changes without rebooting, run:

    $ source $HOME/.profile
    
  5. Verify the installation: Open a command prompt and check the Go version:

    $ go version
    

    If installed correctly, the command will print the installed Go version.

Installing Go on Mac

  1. Install the package: Download the Go package for macOS from the official website . Open the package file and follow the installation prompts. The installer will place the Go distribution in /usr/local/go.

  2. Ensure PATH is updated: The installer should automatically add /usr/local/go/bin to your PATH environment variable. If it doesn’t, manually add the following line to your shell profile (e.g., .zshrc, .bash_profile):

    export PATH=$PATH:/usr/local/go/bin
    
  3. Restart Terminal sessions: Close and reopen any open Terminal sessions for the changes to take effect.

  4. Verify the installation: Open a command prompt and type:

    $ go version
    

    Confirm that the installed Go version is displayed.

Installing Go on Windows

  1. Run the installer: Download the Go MSI installer from the official website . Open the MSI file and follow the prompts to install Go. The default installation path is C:\Program Files\Go or C:\Program Files (x86)\Go.

  2. Update the PATH variable: The installer typically updates the PATH environment variable. To verify, open a new command prompt window.

  3. Verify the installation:

    • Click the Start menu.
    • Type cmd in the search box and press Enter.
    • In the Command Prompt window, type:
      $ go version
      

    Confirm that the installed Go version is displayed.

Installing Go from Source

  1. Install Go Compiler Binaries for Bootstrap: You need an existing Go compiler to build Go from source. Ensure you have a Go compiler in your $PATH or set the GOROOT_BOOTSTRAP environment variable to the root of a Go installation.

  2. Install Git: Ensure Git is installed by running git --version. If not installed, follow the instructions on the Git downloads page .

  3. Install a C Compiler (Optional): For cgo support, install a C compiler like gcc or clang. If not needed, set CGO_ENABLED=0.

  4. Fetch the Repository: Choose a directory for Go installation and clone the Go repository:

$ git clone https://go.googlesource.com/go goroot
$ cd goroot
$ git checkout <tag>

Replace <tag> with the desired Go release version, e.g. go1.22.0.

  1. Build Go

Navigate to the src directory and run the build script:

$ cd src
$ ./all.bash

For Windows, use all.bat instead.

  1. Verify Installation: Check your Go installation by creating a hello.go file with the following content:
package main

import "fmt"

func main() {
	fmt.Println("hello, world!")
}

Run the program:

$ go run hello.go

You should see the output hello, world!.

Conclusion

Installing Go is a straightforward process on all major operating systems. By following these steps, you ensure a clean and functional Go setup, allowing you to dive into Go programming without any hitches. Whether you’re on Linux, Mac, or Windows, you can get Go up and running with minimal effort. Happy coding!