Hi everyone, I’m Tiger.
Recently, I encountered a problem where a friend asked me how to install Go on Linux. To be honest, as a programmer, who hasn’t dabbled in a few open-source projects or experimented with new languages? However, installing Go may seem simple, but there are actually quite a few tricks involved. So, I thought today I’d share how to install Go on Linux and share some pitfalls and interesting experiences I encountered during the installation process.
Download and Compile Go: Do It Yourself, Reap the Rewards
First, I want to say that directly downloading the release version of Go is definitely the most hassle-free choice. However, if you’re interested, downloading and compiling Go’s source code yourself is a very educational process. It’s like when you were a kid and opened up a toy to study its structure; you wouldn’t know why it moves unless you take it apart. The same goes for compiling Go; if you compile it yourself, you’ll basically understand how the language works at a fundamental level.
Of course, this isn’t an exaggeration. I genuinely believe that if you want to deeply understand Go, compiling it yourself is definitely a good thing. The official installation guide is also very considerate; it not only has download links but also detailed installation steps. However, if you find executing a few commands too boring, why not do it like I did and download Go’s source code, and then do a “manual” installation step by step.
Set Go Environment Variables: The Foundation of “Slacking Off”
The first step to installing Go is setting up the environment variables. As we all know, configuring environment variables in Linux can take quite a while. You need to set the root directory of Go (GOROOT); otherwise, the system won’t know what you’ve installed. Open the .bashrc or .profile file (which may vary depending on the Linux distribution) and add the following content:
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=$HOME/Applications/Go
At this point, you might wonder what the difference between the GOROOT and GOPATH environment variables is. Simply put, GOROOT is Go’s home, while GOPATH is where you write your code. Just like when you come home and want to flop on the sofa, the Go compiler also needs to know where its “home” is, as well as where your code is located. As for PATH, it tells the system where to find Go-related binaries.
When I finished setting the environment variables, I thought everything was set, but when I typed source .bashrc to activate the environment variables, the system reported an error! After a long time, I realized I had mistakenly entered the wrong path. Everyone should be cautious about these little pitfalls.
Install C Tools: A Must-Have
Before installing Go on Linux, there is another essential step: installing C tools. Many of Go’s toolchain components are written in C, and without these tools, compilation cannot proceed. For Ubuntu users, you can input the following command in the terminal:
sudo apt-get install bison ed gawk gcc libc6-dev make
However, considering the domestic network environment, I must remind everyone that it’s best to install one tool at a time; otherwise, you might see a screen full of “network timeout” errors. If you are using another distribution, such as CentOS, you will need to use tools like RPM to install. This process is actually quite boring; the only highlight is that you can take this time to brew a cup of coffee and enjoy a moment of tranquility.
Get and Compile Go Source Code: A Ticket for Compilation
Next, it’s time to get the Go source code. Although downloading the precompiled package is the easiest, I like to tinker, so I have to do it myself. After downloading the Go source package, use tar to extract it, and then move the extracted go directory to the location specified by GOROOT:
wget https://storage.googleapis.com/golang/go<VERSION>.src.tar.gz
tar -zxvf go<VERSION>.src.tar.gz
sudo mv go $GOROOT
At this point, things get much simpler. Enter the $GOROOT/src directory and run ./all.bash to start compiling:
cd $GOROOT/src
./all.bash
Once the compilation is complete, you will see some success messages in the terminal, and at that moment, you’ll feel, hey, I finally did it!
However, there is a small pitfall here; if you installed a firewall, you might encounter an error while compiling the net/http package. I experienced this too and was confused, not knowing what the problem was. Later, I found out that the firewall was blocking Go’s access to Google requests. The solution is actually simple: either temporarily disable the firewall or set an environment variable DISABLE_NET_TESTS=1 to skip these network tests.
Test Installation: Hello, World!
Finally, we reach the most exciting moment: testing whether the installation was successful. Write the simplest Hello, World! program:
package main
func main() {println("Hello", "world")}
Save it as hello_world1.go, then run it in the terminal:
go run hello_world1.go
If you see “Hello world” printed on the screen, congratulations, Go has been installed successfully! This process seems simple, but for a beginner, the moment you see the terminal output “Hello world” is truly fulfilling.
Verify Installation Version: Check What You Installed
If you want to confirm the version of Go you installed, you can enter the following command:
go version
This will print the current Go version information in the terminal, such as “go1.4.2”. If you’re still not satisfied and want to get the Go version information through code, try the following code:
package main
import ("fmt"
"runtime")
func main() {fmt.Printf("%s", runtime.Version())}
After running, you will see output similar to “go1.4.2”. This operation seems simple, but for someone like me who has OCD, it’s necessary to verify the version number; if I install the wrong version, wouldn’t that be a waste of effort?
Update Go Version: Staying Updated is a Must for Perfectionists
After installing Go, you might be curious about how to keep the Go version updated. The official documentation clearly states that Go has three branches: Go release (stable version), Go weekly (weekly updated version), and Go tip (latest beta version). For someone like me who values stability, I usually choose Go release; however, if you are the type who likes to experiment, you might consider Go tip, although it may have some undiscovered bugs, but at least you can experience the latest features first-hand.
Finally, a small tip: before updating Go, it’s best to pay attention to the information released on the official blog to avoid encountering unexpected issues after the update.
Well, this is the entire process of installing Go on Linux. Although the process is a bit cumbersome, the feeling of doing it yourself is quite satisfying. If you also want to give it a try, why not take the plunge? I look forward to hearing about your installation experiences, and feel free to share the various pitfalls you’ve encountered in the comments, so we can learn and communicate together.
Currently, for those interested in programming and career development, feel free to contact me on WeChat: golang404, and I will add you to the “Programmer Communication Group”.
🔥 Tiger’s Private Collection of Quality Recommendations 🔥
The materials include “IDEA Video Tutorial”, “Most Comprehensive GO Interview Question Bank”, “Most Comprehensive Project Source Code and Video”, and “Graduation Project System Source Code”, totaling up to650GB. All of it is available for free! Fully meets the learning needs of programmers at various stages!