1. Preface
In production environments or development machines, the latest stable version of Go (Golang) is often required to use the latest features.
The APT source version that comes with Ubuntu may lag, so this article will explain howManually download the official binary packageCarry out installation and focus on how to troubleshoot and solve the problem.Executable file format error"question.
2. System architecture detection
First, confirm the CPU architecture of the current machine to download the corresponding Go installation package.
uname -m
Common outputs and corresponding installation packages:
-
x86_64
oramd64
→ Downloadlinux-amd64
Version -
aarch64
orarm64
→ Downloadlinux-arm64
Version -
i386
ori686
→ Downloadlinux-386
Version
Optional: Further confirm whether the decompressed Go binary architecture matches itself:
file /usr/local/go/bin/go # Sample output:ELF 64-bit LSB executable, x86-64, ...
likefile
The architecture detected by the command anduname -m
If it is inconsistent, it means that the binary does not match the system and you need to re-download the correct version.
3. Uninstall the old version Go
If you have installed Go manually or APT before, it is recommended to remove it completely first:
sudo rm -rf /usr/local/go
Notice:
Don't forget to clean up the old ones/usr/local/go
Directory to avoid conflict between old and new versions.
4. Download and install the correct version
1. Get the download link
Access /dl/ and copy what is suitable for your architecture.
Link.
2. Download and decompress
Go 1.21.0,amd64
As an example:
wget /dl/go1.21. sudo tar -C /usr/local -xzf go1.21. rm go1.21.
3. Verify the decompression result
ls /usr/local/go/bin # Should include:go gofmt etc executable files
5. Configure environment variables
In the user's shell configuration file (~/.bashrc
、~/.zshrc
etc.) Add:
# Go root directoryexport GOROOT=/usr/local/go # Go workspace (can be modified on demand)export GOPATH=$HOME/go # Add the go executable file to PATHexport PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Save to make the configuration take effect:
source ~/.bashrc
6. Verify installation
- View Go version
go version # Expected output:go version go1.21.0 linux/amd64
- Run the sample program
mkdir -p $GOPATH/src/hello && cd $_ cat > << 'EOF' package main import "fmt" func main() { ("Hello, Go on Ubuntu!") } EOF go run # Run directlygo build -o hello #Compilation./hello # implement
The output should be:
Hello, Go on Ubuntu!
7. Frequently Asked Questions and Suggestions
"Executable file format error"
- Most of the reasons why the download package architecture does not match the system.
- Be sure to use
uname -m
andfile
After checking the command, select the correct one to download.
。
Subsequent upgrades
- Just delete
/usr/local/go
And follow the above steps to download the new version, without repeated configuration of environment variables.
Go Modules
- Go 1.11+ has natively supported modular management without strict dependencies
GOPATH
, execute in the project root directory:
go mod init /your-module
You can enable module mode.
IDE Support
- It is recommended to use VS Code + Go plug-in, or IDEs such as GoLand to enjoy intelligent completion, debugging and code formatting.
- Through the above steps, you can obtain a Go development environment that exactly matches the system architecture on Ubuntu, avoiding common pitfalls such as "executable file format errors".
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.