Setting up Go Lang on Debian
Setting up Go Lang on Debian
So you need Go in your Debian, and apt gives something, but not the version you need? Or worst, you need several versions of Go, one for each project you're working on?
Fear no more!
If you’re looking for a way to manage multiple language runtimes on your Debian system, asdf
is an exceptional tool to consider. Not only does it allow you to manage versions of languages like Python, Ruby, Node.js, and many others, but with the asdf-golang
plugin, it also offers an easy way to install and manage different versions of Go (Golang). This guide will walk you through setting up Go on a Debian system using asdf
.
Why Use asdf?
asdf
is a versatile CLI tool designed to manage multiple language runtimes on a per-project basis. It simplifies the process of switching between different versions of languages, offering a single interface for various runtime environments. Here's why you should consider using asdf
:
-
Single CLI for Multiple Languages:
asdf
eliminates the need for separate version managers likenvm
,rbenv
, orpyenv
by combining them into one tool. -
Consistent Commands: It provides a uniform set of commands to manage all your language versions.
-
Global and Per-Project Configurations: With
asdf
, you can maintain a single global configuration file, as well as per-project.tool-versions
files. -
Automatic Version Switching:
asdf
automatically switches runtime versions as you navigate through different directories. -
Shell Completion: It offers shell completion for common shells like Bash, Zsh, Fish, and more.
Installing asdf on Debian
Before you start, make sure you have the necessary dependencies installed on your system. Here’s how you can get started:
-
Install Dependencies:
sudo apt update sudo apt install curl git coreutils
-
Clone the asdf Repository:
git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.12.0
-
Add asdf to Your Shell: Add the following lines to your
.bashrc
or.zshrc
file:export PATH="$HOME/.asdf/shims/:$PATH" . $HOME/.asdf/asdf.sh . $HOME/.asdf/completions/asdf.bash
-
Reload Your Shell: After editing the file, reload your shell:
source ~/.bashrc
Installing the asdf-golang Plugin
With asdf
installed, you can now add the golang
plugin:
-
Add the golang Plugin:
asdf plugin add golang https://github.com/asdf-community/asdf-golang.git
-
Install a Version of Go: To install a specific version of Go, use:
asdf install golang 1.22.6
Replace
1.22.6
with the version you wish to install. -
Set a Global or Local Version: To set the global version of Go (the default for all projects):
asdf global golang 1.22.6
Or, set a local version for a specific project:
asdf local golang 1.22.6
Configuring Go Environment Variables
To properly set up Go's environment variables, you need to add a function to your .bashrc
file. This ensures that the GOROOT
, GOPATH
, and GOBIN
variables are correctly set each time you start a new shell session.
Add the following to your .bashrc
file:
# GOLANG
asdf_update_golang_env() {
local go_bin_path
go_bin_path="$(asdf which go 2>/dev/null)"
if [[ -n "${go_bin_path}" ]]; then
abs_go_bin_path="$(readlink -f "${go_bin_path}")"
export GOROOT
GOROOT="$(dirname "$(dirname "${abs_go_bin_path}")")"
export GOPATH
GOPATH="$(dirname "${GOROOT}")/packages"
export GOBIN
GOBIN="$(dirname "${GOROOT}")/bin"
export PATH="$GOBIN:$PATH"
fi
}
asdf_update_golang_env
After adding this, reload your shell:
source ~/.bashrc
How .tool-versions
File Works with asdf local
When you use the asdf local
command in a project directory, asdf
creates a file named .tool-versions
in that directory. This file stores the language versions that asdf
should use when you are within that specific directory.
Example of .tool-versions
:
If you run the following command in your project directory:
asdf local golang 1.22.6
It will create a .tool-versions
file in that directory with the following content:
golang 1.22.6
This file tells asdf
to use Go version 1.22.6
whenever you are in that project directory. If you have other languages managed by asdf
(e.g., Node.js, Python), their versions would also be listed in this file.
How .tool-versions
Affects Version Selection
-
Local Version: When you navigate to the directory containing the
.tool-versions
file,asdf
automatically switches to the specified version of Go (or other languages) for that project. -
Global Version Override: The versions specified in the
.tool-versions
file take precedence over any global versions you have set withasdf global
. This allows you to customize the language environment for each project without affecting others. -
Project-Specific Setup: This is particularly useful when working on multiple projects that require different versions of the same language. The
.tool-versions
file ensures that you are always using the correct version for the project at hand.
Checking Installation of Global Go Packages
After installing Go, you can install global Go packages using go install
. Here’s how you can check that a package is installed globally:
-
Install a Package Globally:
go install github.com/air-verse/air@latest
-
Check Installation: After installation, verify that the package was installed globally by running:
air -v
This command should display the version of
air
, confirming that the installation was successful.
Managing Go Versions with asdf
asdf
makes it easy to switch between different versions of Go, whether globally or locally for a specific project.
Installing Multiple Versions of Go
-
Install a New Version:
asdf install golang 1.18.3
-
Set a Global Version: To set
1.18.3
as the global version:asdf global golang 1.18.3
-
Set a Local Version: If you want to use a different version for a specific project, navigate to the project directory and run:
asdf local golang 1.22.6
Switching Between Versions
You can easily switch between different Go versions using asdf
. For example:
-
To switch to a globally installed version:
asdf global golang 1.22.6
-
To switch to a version for a specific project:
asdf local golang 1.18.3
Conclusion
With asdf
and the asdf-golang
plugin, managing multiple versions of Go on Debian becomes effortless. By following the steps outlined in this guide, you’ll have a robust setup that allows you to switch between Go versions seamlessly, manage your Go environment variables efficiently, and ensure your projects are always using the correct Go version. Happy coding!