In this tutorial, we will be showing you how to install the Go Compiler on the Ubuntu operating system.
Go is a high-level compiled programming language that was originally designed at Google.
Syntax-wise, the language is very similar to the C language. However, it introduces many modern improvements to make it easier to use, especially with memory and concurrency.
Of course, being a compiled language means that to run your code, you must first compile it using the Go compiler. This compiler isn’t available through the Ubuntu package repository. Luckily, installing it doesn’t require that much extra effort.
In the following sections, we will show you how to install the latest version of the Go compiler to your Ubuntu operating system.
Installing and Testing the Go Compiler on Ubuntu
These sections will walk you through the process of getting the Go Compiler up and running on Ubuntu.
We cover installing the compiler and testing it with a simple “Hello world
” script.
Preparing your System for the Go Compiler
1. Our first step is to update our Ubuntu operating system’s package list cache by running the command below.
This cache is used so the package manager can easily see what packages can be installed, their version, and where apt can download them.
sudo apt update
2. Once the update has been completed, we must ensure the wget package is available on your system. We will be using this tool to download Go to your Ubuntu device.
We can attempt to install a package again to check if it is installed.
sudo apt install wget -y
The "-y
” option tells the “apt” package manager to install the specified package without prompting for confirmation.
Downloading and Installing the Go Compiler on Ubuntu
3. Now that we have the operating system set up, we can download and install the Go Compiler to your Ubuntu system.
Thanks to the wget tool we installed earlier, we can download Go using the command below. Make sure you select the command relevant to the hardware you are using. There are different releases of Go for x86/x64 and ARM-based systems.
The command we are using will download the archive for Go 1.21.5. You can check for newer versions of the compiler by going to the official Go website.
x64 Based Operating system:
wget https://go.dev/dl/go1.21.5.linux-amd64.tar.gz -O go.tar.gz
ARM64 Based Operating system:
wget https://go.dev/dl/go1.21.5.linux-amd64.tar.gz -O go.tar.gz
4. If you have never installed Go, skip to step 5. Otherwise, you will want to run the following command.
Extracting a new version of Go over an older one is known to have issues. We must ensure the old one does not exist by wiping the directory to correct this.
sudo rm -rf /usr/local/go
5. Installing Go to Ubuntu just requires us to extract the archive we downloaded earlier. We will be extracting the compiler into the “/usr/local/
” directory.
To extract this archive, use the command below in the terminal.
sudo tar -C /usr/local/ -xvf go.tar.gz
The “-C
” option allows us to tell the tar utility where we want this archive’s files extracted. Next, we use the “-xvf
” option to tell tar that we want to extract an archive and we want it to print what it’s doing to the terminal.
Updating the Path Environment Variable for Go
6. Even though we now have Go extracted, and you could technically start using it, you won’t be able to run it by just typing “go
” into the terminal.
To resolve this slight annoyance, we must add the Go directory to the Ubuntu Path variable. This environment variable is read by the terminal when you run commands.
Let us add Go to the path by editing the global “profile
“.
sudo nano /etc/profile
7. With the profile open, add the following line to the bottom of the file.
This line adds the “/usr/local/go
” directory to the front of the PATH environment variable.
export PATH="/usr/local/go/bin:$PATH"
8. After making the changes above, you can save and quit by pressing CTRL + X, followed by Y, and finally, the ENTER key.
9. To save us from restarting or logging out, we can tell the terminal to use the updated profile by running the command below.
source ~/.profile
10. Finally, we can verify that Go is installed and now easily runnable from the command line by getting it to print out its version.
go version
If everything is working correctly, you should see a version number like we have shown below
go version go1.21.5 linux/amd64
Writing First Go Script on Ubuntu
11. To test that everything is working properly, let us write a quick script in Go that will output the text “Hello World
” to the terminal.
This is a super simple script and will allow us to verify we have Go running on our Ubuntu system
nano hello-world.go
12. Within this file, you will want to type the following lines of code. We will explain each block if you are interested. Otherwise, you can skip to the next step for the final product.
a. With the Go programming language, every file must start by defining the package name.
In our example, we declare this script as part of the “main
” package.
package main
b. We import the “fmt
” package with the following line of code. This package comes bundled with Go and handles formatted I/O such.
In our case, we will be using this package to output the text “Hello World
” to the terminal.
import "fmt"
c. Next, we declare our “main()
” function. This function will be automatically executed when your Go application is run on Ubuntu.
Within this function, we use the “fmt
” package’s “Println
” function to output the text “Hello World
“.
func main() {
fmt.Println("Hello World")
}
12. By the time you finish writing in the code above, you should end up with the following script.
It is a super simple usage of Go, but it allows you to test that everything works.
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
13. Once you have finished adding the lines above to the script, you can save and quit by pressing CTRL + X, Y, and then the ENTER key.
Running and Compiling your Script with Go
14. With our script now written, we can use Go on Ubuntu to run the script. Running differs from a build in that it will compiler and run your script in one go.
You can run the script we wrote earlier by using the following command in the terminal.
go run hello-world.go
After running the script above, you should have the text “Hello World
” output in the terminal.
Hello World
14. Go can also build your script into an executable. This is useful if you plan on distributing your program to others.
You can compile the script by using Go’s “build
” option.
go build hello-world.go
15. After running the build option, you will be left with an executable named “hello-world
“.
We can run this executable just like you would with any other on Ubuntu. You can also share this with other devices and run it the same way.
The only caveat is that they must have the same processor type and operating system as the one you compiled the program on. Go does offer cross-compiling, but that is not something we will cover in this tutorial.
./hello-world
You should get the same result as when you used the “run
” command on your script.
Hello World
Conclusion
You should now have the Go compiler installed and working on your Ubuntu operating system at this stage.
Setting up this compiler is a super simple process and will allow you to get programming in the Go language quickly.
Please feel free to leave a comment below if you have any questions about getting this compiler to run on your system.
If you found this tutorial helpful, we highly recommend checking out our many other Ubuntu tutorials.