Introduction
Go (or Golang) is an open source programming language launched by Google in 2009. It builds on the best concepts and practices from languages like C, Python and Java to create a performant, scalable and reliable language focused on modern development needs.
As a full stack developer, I‘ve found Go to be a versatile choice for building web services, cloud-native applications and developer tooling. It‘s no surprise that Go adoption has rapidly grown, with Stack Overflow reporting it as the 4th most loved language for 5 years running.
Some key benefits of Go:
- Performance – Go compiles down to static binaries that leverage multiple CPU cores well. Performance nears languages like C/C++.
- Concurrency – Native concurrency features make it easy to write highly parallel applications.
- Scalability – Lightweight processes fully utilize resources for greater scale.
- Reliability – Features like garbage collection, type safety and excellent tooling lead to more robust code.
- Simplicity – A clean syntax and design choices make Go straightforward to learn.
- Portability – Compiles to standalone binaries usable across platforms and architectures.
This combination of speed, safety and ease of use explains Go‘s rise for systems programming. Companies like Google, Uber, Dropbox, Twitch and Cloudflare use Go for core infrastructure and services.
In this comprehensive guide, we will explore different methods for installing Go on Debian 11 Linux so you can start building your own applications.
Pre-Installation Considerations
Before installing Go, it is good practice to ensure your Debian 11 system is fully up to date. Run the following commands in a terminal:
sudo apt update
sudo apt upgrade
Apply any pending updates and reboot if required. This prevents any potential conflicts.
Some background on Debian package versions is also helpful context. Debian 11 "Bullseye" ships with Go 1.17 from the main package repositories. However, new Go versions get added in the form of backports. At time of writing, Go 1.19 is available this way.
I recommend installing the latest backports version where possible for access to new features and performance improvements. However, if your software specifically requires an older Go release, adjust commands in this guide accordingly.
With that background covered, let‘s explore our installation options!
Method 1: Installing Go with APT
The easiest and most common way to install Go on Debian is by using the Advanced Packaging Tool (APT).
APT handles obtaining, configuring and installing binary packages automatically. It also readily integrates with other Debian utilities like dpkg for lower level package management.
Here are the steps to install Go using APT:
- First, enable the backports repository to access newer package versions:
sudoedit /etc/apt/sources.list
- Append this line:
deb http://deb.debian.org/debian bullseye-backports main
- Update repositories and install Go:
sudo apt update sudo apt -t bullseye-backports install golang-go
The -t
flag targets the backports repo instead of default. By leveraging backports, we install Go 1.19 without having to upgrade the whole system to Debian 12.
Let‘s check the installed version:
go version
You should see output like:
go version go1.19.5 linux/amd64
That‘s it! We now have the latest Go release installed with two quick commands.
Evaluating the APT Method
Installing from the official Debian repositories using APT is quick and robust. Dependencies get handled automatically. Updates are also easy to manage in future by just running:
sudo apt update
sudo apt upgrade
However, there is lag between a new Go version being released and landing in backports. So this method does not get you literally the latest.
Stability is prioritized over new features. Released versions undergo additional testing by Debian before acceptance into repositories.
Uninstalling Go
To cleanly uninstall Go installed via APT:
sudo apt remove golang-go
This is straightforward. It also removes any Go-related packages pulled in as dependencies.
Next let‘s look at a method to get the cutting edge Go releases.
Method 2: Install Latest Go Version from Binary Distro
The official Go project provides binary distributions for major platforms like Debian Linux. These contain the latest stable Go version immediately upon release.
Follow these steps:
- Download the archive containing binaries:
wget https://golang.org/dl/go1.20.1.linux-amd64.tar.gz
- Extract the archive contents:
sudo tar -xvf go1.20.1.linux-amd64.tar.gz
- Move it to the /usr/local directory:
sudo mv go /usr/local
- Update environment paths:
echo ‘export PATH=$PATH:/usr/local/go/bin‘ >> ~/.profile source ~/.profile
The last step sets PATH so Linux finds the Go binaries placed in /usr/local/go/bin.
Now test it:
go version
Output:
go version go1.20.1 linux/amd64
Great – we have Go 1.20.1 installed from latest binary distribution!
Evaluating the Binary Method
Benefits to this approach:
- Bleeding edge Go version immediately on release
- Avoids Debian testing lag
- Simple installation without managing repositories
Downsides to evaluate:
- Manual process to handle updates
- No Debian package integration for easy removal
- Enterprise environments may prefer stability over latest
For developers wanting new language features now, this method is ideal. Operations teams at scale may prefer APT‘s stability and ecosystem integration though.
Uninstalling Go
To cleanly uninstall Go installed via binary distro:
sudo rm -rf /usr/local/go
We simply delete the /usr/local/go directory we had created.
Now let‘s look at one more modern method for installing Go – using Snap.
Method 3: Install Go through Snap Store
Snaps are self-contained software packages managed by the Snapcraft project. They bundle dependencies and config to run apps consistently and securely across Linux distributions.
Snaps auto-update in the background. Channels like beta or edge also allow opting into pre-release versions.
Here is how to leverage Snaps to install the latest Go release on Debian:
- Ensure snapd is installed:
sudo apt update sudo apt install snapd
- Enable classic snap support:
sudo snap install core; sudo snap refresh core
- Install Go snap package:
sudo snap install go --classic
The --classic
flag is needed to give the Go snap permissions to compile binaries.
Let‘s check the version:
go version
This should display latest stable Go:
go version go1.20.1 linux/amd64
That wraps up installing Go via the Snap store!
Evaluating the Snap Method
Pros of using Snaps for Go:
- Auto-updating to latest Go version
- Option to follow pre-release channels
- Consistent experience across distributions
- Easily sandboxed from host environment
Potential cons to evaluate:
- Classic mode decreases security isolation
- Can conflict with native packages
- Debian community has some concerns around proprietary Snaps
For those wanting latest Go with minimal administration, Snaps offer a compelling approach. Aspects like auto-updates translate to higher production readiness. The security tradeoffs require thoughtful consideration however.
Uninstalling Go
To remove Go installed via snap:
sudo snap remove go
Snap packages clean up cleanly on removal as they confine themselves from the rest of the system.
Comparing Installation Methods
We have covered several options for installing Go on Debian – let‘s compare them:
APT | Binary | Snap | |
---|---|---|---|
Freshness | Backports version | Cutting edge | Auto-updating |
Convenience | Fully integrated | Manual updates | Auto manages |
Security | Stable packages | User responsibility | Sandboxed |
There is no definitively "best" approach – it depends on your priorities:
- APT – Best for stability and Debian ecosystem integration
- Binary – Gives leading edge features and performance
- Snap – Easy auto-updating with sandboxing
I recommend most Debian developers use APT or Binary methods. Both make Go readily available for building applications. Operations teams may prefer Snap‘s auto-updating.
Now let‘s go beyond just installation best practices…
Recommendations for Using Go
Once Go is installed via your preferred method, a few tips will set you up for success:
1. Environment Variables
Configure your GOPATH environment variable to keep Go code organized:
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$PATH
This GOPATH will contain the go subdirectory for actual source files. Then bin directory for compiled binaries.
2. Get Additional Tools
Install these Go tools for linting, formatting and dependency management:
go install golang.org/x/tools/gopls@latest
go install golang.org/x/lint/golint@latest
go install github.com/go-delve/delve/cmd/dlv@latest
go install github.com/uudashr/gopkgs/v2/cmd/gopkgs@latest
go install github.com/ramya-rao-a/go-outline@latest
go install github.com/go-delve/delve/cmd/dlv@latest
go install github.com/cweill/gotests/gotests@latest
This gives IDE-like functions for Go development environments.
3. Use a Code Editor with Go Plugin
I recommend Visual Studio Code with the Go extension for the best experience. These enhancements are invaluable:
- Code completion
- Built-in debugging
- Automatic formatting
- Snippets library
- Unit test execution
Additional highlighting, linting and navigation features make development very efficient.
This covers the key setup recommendations for effectively working with Go!
Troubleshooting Common Go Install Problems
While installing Go is generally straightforward, a few issues can arise depending on your system:
Can‘t Find go Binary in PATH
If Go installed successfully but the go command is not found, double check PATH contains the binary location.
For APT method, PATH should include:
/usr/lib/go-1.19/bin
Or for Binary method:
/usr/local/go/bin
If missing, revert back and update PATH using the installation instructions. A restart or reload of profile may be needed.
Permission Errors on Binary Install
Unpacking or moving Go files into system directories can trigger permission errors. Simply prepend commands with sudo like sudo tar
or sudo mv
.
Or consider installing into ~/go then updating PATH instead to avoid.
"Operation not permitted" on Snap Install
Using Snaps may fail with generic permission errors. Ensure core snap is refreshed and installed correctly per our instructions.
Also check that AppArmor / seccomp is not blocking the go snap. Try temporarily disabling AppArmor and retry.
Backports Package 404 Error
If apt install fails with a 404 on bullseye-backports, verify /etc/apt/sources.list has a valid entry present. Remove any typos in the URL before reattempting.
This covers the most common hiccups! Contact #go-nuts IRC or StackOverflow for further troubleshooting tips.
Conclusion
In this comprehensive 2600+ word guide, we thoroughly explored how to install Go on Debian 11 starting from the fundamentals.
We looked at:
- Feature overview of Go and its growth
- Preparing your system for success
- Leveraging APT repositories with backports
- Downloading latest binary distributions
- Auto-updating Go releases through Snaps
- Comparing installation tradeoffs
- Configuring your environment
- Extending your toolchain
- Enabling plugins for code editor integration
- Troubleshooting common issues
With Go installed via your preferred method, you now have an efficient language for building web, cloud and systems applications on Debian 11.
I wish you the best as you develop robust programs leveraging Go‘s strengths! Let me know if any part of this guide needs further detail.