If in the previous tutorial we have learned about What is Golang ?, now in this tutorial you’ll learn about How to install Golang on Windows, Linux, and MacOS . The first thing that needs to be done before being able to use Go is to install it first. The installation guide is actually provided on the official Go website http://golang.org/doc/install#install.
But in this tutorial, I’ll attempt to summarize the installation instruction provided in the above link to make it easier to follow, especially for readers who are new to learning. Before doing the installation process, please download directly the Golang installer from the link https://golang.org/dl/ or you can follow this instruction to start the Golang installation process on your laptop or computer.
Notes: The version of Go used in this tutorial is 1.13, and it is recommended to use that version.
Install Golang On Windows (stable and unstable versions)
For install Golang (stable and unstable versions) on Windows, you can follow these steps:
Install Golang Stable Version (recommended) :
1. First, You must visit the official Golang website: https://golang.org/dl/
2. Scroll down to the “Stable versions” section.
3. Download the MSI installer corresponding to your Windows architecture (32-bit or 64-bit). Click on the link to start the download.
4. Once downloaded, locate the downloaded file and run the installer and follow the prompts in the installer to complete the installation by clicking the ‘Next‘ button until the installation process is complete. By default, if you didn’t change the path during installation, Go will be installed in C:\go. This path will automatically be added to the PATH environment variable.
5. After the installation is complete, you should have Golang installed on your system. You can verify the installation by opening a Command Prompt / CMD and typing this command :
go version
If the output matches the installed Go version, it indicates a successful installation.
Sometimes, even after a successful installation, the ‘go version’ command may not work. The solution is to restart CMD (close CMD and open it again). Then, try running the command again.
Install Golang Unstable Version:
If you are interested in trying the development version of Golang or want to explore features that have not been officially released, there are a few ways to do it:
1. First, You must visit the official Golang website: https://golang.org/dl/
2. Scroll down to the “Unstable versions” section.
3. Download the archive file (.zip) for the unstable version you want to install. Click on the link to start the download.
4. Once the download is complete, extract the contents of the zip file to a directory of your choice. For example, you can extract it to C:\go-unstable
.
5. Next, open the Windows Control Panel and go to “System and Security” → “System” → “Advanced system settings“.
6. Click on the “Environment Variables” button. In the “System Variables” section, click on “New”.
7. Set the variable name GOROOT_UNSTABLE
and the variable value as the path to the directory where you extracted the unstable Go version. For example, C:\go-unstable
. Click “OK” to save the environment variable.
8. Finally, add the Go binary directory to the PATH
environment variable:
9. In the same “Environment Variables” window, locate the PATH
variable in the “System Variables” section and click on “Edit“.
10. Add the following entry to the list of paths: %GOROOT_UNSTABLE%\bin
. Click “OK” to save the changes.
11. To verify the installation, open a new command prompt and type go version
. You should see the installed unstable Go version printed on the screen.
Install Golang On MacOS (stable and unstable versions)
For Installing Golang on MacOS you can follow the steps below. The easiest way to install Go on MacOS is by using Homebrew.
1. First, open the Terminal application on your macOS system. You can find it in the “Utilities” folder within the “Applications” folder.
2. If you don’t have Homebrew installed, you can install it by running the following command in the Terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
3. After Homebrew is installed, you can use it to install Go. To install the stable version(recommended), run the following command:
brew install go
This command will install the stable version of Go available through Homebrew.
4. If you want to install an unstable or developed version of Go, you can use the --devel
flag. For example, to install the unstable version, run the following command:
brew install go --devel
This command will install the latest unstable version of Go.
5. After the installation is complete, you should set up the necessary environment variables for Go. Add the following lines to your ~/.bash_profile
or ~/.zshrc
file (depending on the shell you use):
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
These lines will set the GOPATH
environment variable and add Go binaries to your PATH
.
6. Save the file and exit the text editor.
7. To apply the changes to your current terminal session, run the following command:
source ~/.bash_profile
or
source ~/.zshrc
Verify that Go is installed correctly by running the following command:
go version
If the output shows the installed Go version, it indicates a successful installation.
Install Golang On Linux (stable and unstable versions)
To install Go (Golang) on Linux, you can follow these steps to install both stable and unstable versions:
1. Download the Go distribution: Visit the official Go downloads page at https://golang.org/dl/ and download the appropriate package for your Linux distribution. Choose either the stable version or the unstable (beta) version, depending on your preference.
2. Extract the archive: Once the download is complete, open a terminal and navigate to the directory where the downloaded file is located. Use the following command to extract the archive:
tar -C /usr/local -xzf go<VERSION>.linux-amd64.tar.gz
Replace <VERSION>
with the version number of the downloaded package. For example, if you downloaded Go 1.17, the command would be:
tar -C /usr/local -xzf go1.17.linux-amd64.tar.gz
This command extracts the Go distribution to the /usr/local/go
directory.
3. Set up Go environment variables: To use Go, you need to set up the appropriate environment variables. Add the following lines to your shell profile file (e.g., .bashrc
, .zshrc
, or .profile
):
export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
The first line adds the Go binary directory to the PATH
environment variable, allowing you to run the Go executable from anywhere in the terminal. The second line sets the GOPATH
environment variable, which specifies the location of your Go workspace. The third line adds the Go workspace’s bin
directory to the PATH
, allowing you to run Go binaries installed by go install
.
4. Reload the shell profile: After adding the environment variables, run the following command to reload the shell profile:
source ~/.bashrc
Replace ~/.bashrc
with the path to your shell profile file if you’re using a different one.
5. Verify the installation: To verify that Go is installed correctly, open a new terminal and run the following command:
go version
It should display the installed Go version. Now you have successfully installed Go on your Linux system.
Here is the Golang tutorial from me this time about how to install Golang. For the next tutorial, I will create another one soon, which will certainly still be about Golang tutorials.