Go, also known as Golang, is an open-source programming language designed for building scalable and efficient software. Created by Google, it has become immensely popular among developers for its simplicity, speed, and strong support for concurrent programming. In this guide, we will walk you through the process of installing Go on Ubuntu 20.04, from prerequisites to setting up your first Go project. Let’s dive into the world of Go programming!
Why Use Go?
Before we jump into the installation process, it’s essential to understand why Go has gained traction in recent years. Here are some compelling reasons to consider Go for your next development project:
-
Simplicity and Efficiency: Go’s syntax is straightforward, making it easy for new developers to learn while being powerful enough for experienced programmers to create complex applications efficiently.
-
Concurrency Support: Go’s built-in concurrency support allows developers to manage multiple tasks simultaneously without the complexities of traditional multi-threading.
-
Strong Standard Library: Go comes with a rich standard library that provides many functions and packages for web development, networking, and data manipulation.
-
Cross-Platform Compatibility: Applications written in Go can easily be compiled to run on various operating systems, including Windows, macOS, and Linux.
-
Open Source Community: Go has a vibrant community that contributes to its ecosystem, which includes many libraries, frameworks, and tools for various applications.
Prerequisites for Installing Go
Before we proceed, ensure that you have the following prerequisites:
-
Ubuntu 20.04 Installed: Make sure you’re running Ubuntu 20.04. You can check your version by running:
lsb_release -a
-
Terminal Access: Familiarity with the terminal will be beneficial, as most installation commands will be executed from the command line.
-
Sudo Privileges: Ensure you have administrative privileges to install software on your machine.
Step 1: Update Your System
It’s always a good practice to start by updating your system to ensure all packages are up-to-date. Run the following command in your terminal:
sudo apt update && sudo apt upgrade -y
This command updates the package index and upgrades the existing packages on your system.
Step 2: Download the Go Binary
The next step is to download the latest version of Go. As of now, the latest stable version can be found on the official Go downloads page. At the time of writing, it is version 1.20. Here’s how you can download it:
-
First, navigate to the
/tmp
directory, which is used for storing temporary files:cd /tmp
-
Use the
wget
command to download the latest Go tarball. Replace the URL with the current version if necessary:wget https://golang.org/dl/go1.20.linux-amd64.tar.gz
Step 3: Install Go
Once the download is complete, we need to extract the files and install Go. Here are the commands to do that:
-
Extract the downloaded tarball:
sudo tar -C /usr/local -xzf go1.20.linux-amd64.tar.gz
This command extracts the Go files into the
/usr/local
directory, making it accessible globally. -
After extraction, we need to set up the Go environment variables. Open your profile file in a text editor. For example, using
nano
, run:nano ~/.bashrc
-
Add the following lines at the end of the file:
export PATH=$PATH:/usr/local/go/bin
-
Save the file and exit the text editor. To apply the changes, run:
source ~/.bashrc
This ensures that the Go binary is included in your system’s PATH, allowing you to run Go commands from anywhere in the terminal.
Step 4: Verify the Installation
To confirm that Go has been installed successfully, you can check the version of Go installed by running:
go version
If everything is set up correctly, you should see output similar to:
go version go1.20 linux/amd64
This output indicates that Go is installed and operational.
Step 5: Set Up Your Go Workspace
Now that Go is installed, the next step is to set up your Go workspace. The workspace is the directory where you will be writing your Go programs. By default, Go uses the directory ~/go
as the workspace.
-
Create the Go workspace directory:
mkdir ~/go
-
Next, set the GOPATH environment variable to point to your workspace. Open your
~/.bashrc
file again:nano ~/.bashrc
-
Add the following lines to the end of the file:
export GOPATH=$HOME/go export PATH=$PATH:$GOPATH/bin
-
Save the file and apply the changes again:
source ~/.bashrc
Step 6: Create Your First Go Program
Now that you have Go installed and your workspace set up, let’s create a simple Go program to test your setup.
-
Navigate to your Go workspace:
cd ~/go
-
Create a directory for your first project:
mkdir hello-go cd hello-go
-
Create a new Go file named
main.go
:nano main.go
-
Add the following code to the file:
package main import "fmt" func main() { fmt.Println("Hello, World!") }
-
Save the file and exit the text editor.
Step 7: Run Your Go Program
Now it’s time to run your first Go program! In your terminal, execute the following command:
go run main.go
You should see the output:
Hello, World!
Congratulations! You've just installed Go and run your first program.
Troubleshooting Common Installation Issues
While installing Go on Ubuntu 20.04 is generally straightforward, you might encounter some common issues. Here’s how to troubleshoot them:
-
Permission Denied: If you receive a permission denied error during the installation, ensure that you are using
sudo
where required, especially when extracting files to system directories. -
Command Not Found: If the terminal cannot find the
go
command, double-check that you added Go to your PATH correctly and that you have sourced your.bashrc
file. -
Version Issues: If you find that the Go version installed is not what you expected, ensure that you are downloading the correct tarball and that you haven’t previously installed Go through another method (e.g., Snap).
Conclusion
In this guide, we have explored the process of installing Go on Ubuntu 20.04 in detail. From downloading the binaries to setting up a workspace and creating a simple program, you now have a solid foundation to start your journey into Go programming.
With its simplicity, efficiency, and robust feature set, Go is a powerful language that can help you develop high-performance applications. Whether you're building web servers, microservices, or even CLI tools, Go is worth considering for your next project.
As you continue to explore the world of Go, consider diving into its rich ecosystem, exploring frameworks like Gin for web development, or using tools like Docker for containerization. The possibilities are endless!
Frequently Asked Questions (FAQs)
1. What is Go used for?
Go is widely used for building web applications, microservices, cloud computing solutions, and network programming. Its concurrency support makes it an excellent choice for applications that require high performance and scalability.
2. Can I install Go using a package manager?
Yes, you can install Go using package managers like Snap or APT, but it's often recommended to download the binary directly from the official Go website for the latest version.
3. How do I update Go once it’s installed?
To update Go, you can download the latest tarball from the official site and repeat the installation process. Ensure to remove the previous version if necessary.
4. Is Go suitable for beginners?
Absolutely! Go’s clean syntax and comprehensive documentation make it an excellent choice for beginners. It is straightforward to learn and provides a solid foundation for understanding programming concepts.
5. How do I learn Go effectively?
You can learn Go effectively by following online tutorials, reading the official Go documentation, and practicing by building small projects. Engaging with the Go community through forums and meetups can also enhance your learning experience.
By following this guide, we hope you feel confident in installing Go on your Ubuntu 20.04 system and embarking on your journey as a Go developer. Happy coding!