Introduction
Golang, or Go, is a modern and efficient programming language used for a variety of applications, including web development, system programming, and network programming. With its simple syntax and high-performance runtime, it’s no wonder that it has become a popular choice for developers.
If you have a Raspberry Pi and want to start developing in Go, you’ll need to install the latest version of the Golang runtime. In this article, we’ll guide you through the installation process, step by step.
Instructions
The first step is to update your Raspberry Pi’s package index and install the dependencies required for the Golang runtime:
sudo apt update && sudo apt install -y wget git build-essential
Next, download the latest version of Golang:
wget https://dl.google.com/go/go[version].linux-arm64.tar.gz
Replace [version]
with the latest version number of Golang. You can find that on their downloads ↗️ page. So if you wanted to install v1.22.1
the command will look like this:
wget https://dl.google.com/go/go1.22.1.linux-arm64.tar.gz
Also note that the file name will be different based on the architecture of your Raspberry Pi. If you are using a 32-bit OS, you need to download the linux-armv6l.tar.gz
file instead of linux-arm64.tar.gz
.
Since modern Raspberry Pi models support 64-bit OS, you can check the architecture of your Raspberry Pi by running the following command:
uname -m
If the output is aarch64
, then you are using a 64-bit OS. In that case, you should download the linux-arm64.tar.gz
file otherwise download the linux-armv6l.tar.gz
file. I am going to assume you are using a 64-bit OS for the rest of the article.
Moving on, Once it is downloaded we can extract the archive using the following command:
tar -xvf go1.22.1.linux-arm64.tar.gz
Move the extracted folder to ~/.local/share
:
mkdir -p ~/.local/share && mv go ~/.local/share
Set up the environment variables for Golang:
This depends based on the shell you are using, you can verify it using basename $SHELL
command, it will output zsh
, bash
or fish
.
Based on your shell, you need to update the correct config file for it. Let’s see the example of updating for zsh
echo 'export GOPATH=$HOME/.local/share/go' >> ~/.zshrcecho 'export PATH=$HOME/.local/share/go/bin:$PATH' >> ~/.zshrcsource ~/.zshrc
For bash, the default config file would be .bashrc
and for fish it would be .config/fish/config.fish
.
One Script for Everything
To summarise, we can put all of this into a single shell script to download and setup golang on armhf architecture.
#!/bin/bash
VERSION=1.22.1 # pick the latest version from https://golang.org/dl/ARCH=arm64 # arm64 for 64-bit OS, armv6l for 32-bit OS
## Download the latest version of Golangecho "Downloading Go $VERSION"wget https://dl.google.com/go/go$VERSION.linux-$ARCH.tar.gzecho "Downloading Go $VERSION completed"
## Extract the archiveecho "Extracting..."tar -C ~/.local/share -xzf go$VERSION.linux-$ARCH.tar.gzecho "Extraction complete"
## Detect the user's shell and add the appropriate path variablesSHELL_TYPE=$(basename "$SHELL")
if [ "$SHELL_TYPE" = "zsh" ]; then echo "Found ZSH shell" SHELL_RC="$HOME/.zshrc"elif [ "$SHELL_TYPE" = "bash" ]; then echo "Found Bash shell" SHELL_RC="$HOME/.bashrc"elif [ "$SHELL_TYPE" = "fish" ]; then echo "Found Fish shell" SHELL_RC="$HOME/.config/fish/config.fish"else echo "Unsupported shell: $SHELL_TYPE" exit 1fi
echo 'export GOPATH=$HOME/.local/share/go' >> "$SHELL_RC"echo 'export PATH=$HOME/.local/share/go/bin:$PATH' >> "$SHELL_RC"
## Verify the installationif [ -x "$(command -v go)" ]; then INSTALLED_VERSION=$(go version | awk '{print $3}') if [ "$INSTALLED_VERSION" == "go$VERSION" ]; then echo "Go $VERSION is installed successfully." else echo "Installed Go version ($INSTALLED_VERSION) doesn't match the expected version (go$VERSION)." fielse echo "Go is not found in the PATH. Make sure to add Go's bin directory to your PATH."fi
## Clean uprm go$VERSION.linux-$ARCH.tar.gz
Before running the script, make sure you make it executable.
You can do it like this chmod +x install-go.sh
. Replace install-go.sh
with whatever name you gave to the file.
That’s it, now run the file with ./install-go.sh
and it will install and setup golang on your system.
Enjoying the content? Support my work! 💝
Your support helps me create more high-quality technical content. Check out my support page to find various ways to contribute, including affiliate links for services I personally use and recommend.
Conclusion
By following these simple steps, you should now have the latest version of Golang installed on your Raspberry Pi. Whether you’re a seasoned Go developer or just starting out, the Raspberry Pi is a great platform for developing in Golang.
With its low cost, small form factor, and powerful hardware, it provides a great way to learn and experiment with the Go programming language.