Skip to content

Instantly share code, notes, and snippets.

@tobychui
Created August 5, 2021 05:16
Show Gist options
  • Save tobychui/80afe6e866fea8db2b7edf9a422b78b9 to your computer and use it in GitHub Desktop.
Save tobychui/80afe6e866fea8db2b7edf9a422b78b9 to your computer and use it in GitHub Desktop.
Go installer script for Linux ARM SBC
#/bin/bash
echo "Input the go arch to install (arm/arm64/amd64)"
read -p "Architecture: " arch
if [ "$arch" = "arm" ]; then
echo "Installing arm version of go"
wget https://golang.org/dl/go1.15.3.linux-armv6l.tar.gz
fi
if [ "$arch" = "arm64" ]; then
echo "Installing arm64 version of go"
wget https://golang.org/dl/go1.15.3.linux-arm64.tar.gz
fi
if [ "$arch" = "amd64" ]; then
echo "Installing amd64 version of go"
wget https://golang.org/dl/go1.15.3.linux-amd64.tar.gz
fi
sudo tar -C /usr/local -xzf go*
#echo "ADD THE FOLLOWING LINE TO YOUR ~/.bashrc FILE:"
#echo 'export PATH=$PATH:/usr/local/go/bin'
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
source .bashrc
echo "Install Complted"
@NorkzYT
Copy link

NorkzYT commented May 28, 2023

The following Bash script is an updated version I made that will work for future use cases even when the latest stable version of GO is updated:

#!/bin/bash

# Function to check if BeautifulSoup is installed
is_beautifulsoup_installed() {
    python3 - <<END
import pkgutil

if pkgutil.find_loader("bs4"):
    print("True")
else:
    print("False")
END
}

# Install BeautifulSoup if it is not already installed
if ! pip freeze | grep -q beautifulsoup4; then
    echo "BeautifulSoup is not installed. Installing it..."
    echo ""
    pip install beautifulsoup4
else
    echo "BeautifulSoup is already installed."
    echo ""
fi


# Function to fetch the latest Go archive URL for the specified architecture
get_latest_url() {
    local architecture="$1"
    local url="https://go.dev/dl/"

    # Use Python to scrape the webpage and extract the latest URL for the specified architecture
    local latest_url=$(python3 - <<END
import requests
from bs4 import BeautifulSoup

architecture = "$architecture"
url = "$url"

# Fetch the webpage content
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")

# Find the download links based on the architecture
links = [a["href"] for a in soup.find_all("a", href=True) if a["href"].startswith("/dl/go") and architecture in a["href"] and 'linux' in a["href"] and a["href"].endswith('tar.gz')]
if links:
    # Extract the latest URL for the specified architecture
    latest_url = "https://go.dev" + links[0]
    print(latest_url)
END
)

    echo "$latest_url"
}

valid_archs=("armv6l" "arm64" "amd64")
while true; do
    echo ""
    echo "Input the Go arch to install (Options: armv6l/arm64/amd64) or 'x' to exit."
    read -p "Architecture: " arch
    echo ""
    if [[ " ${valid_archs[@]} " =~ " ${arch} " ]]; then
        break
    elif [ "$arch" == "x" ]; then
        exit 0
    else
        echo "Invalid input. Please try again."
        sleep 1
        clear
    fi
done

# Get the latest URL for the specified architecture
latest_url=$(get_latest_url "$arch")

if [ -z "$latest_url" ]; then
    echo "Failed to retrieve the latest Go archive URL for the specified architecture"
    exit 1
fi

file_name=$(basename "$latest_url")

if [ -f "$file_name" ]; then
   echo "Go archive file already exists. Skipping download."
else
   echo "Downloading Go archive..."
   wget "$latest_url"
fi

if [ -f "$file_name" ]; then
   sudo rm -rf /usr/local/go
   sudo tar -C /usr/local -xzf "$file_name"
   echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.profile
   source ~/.profile
   echo "Go installation completed."
   echo "Verify that you've installed Go by opening a new terminal and typing the following command:"
   echo -e "\033[32mgo version\033[0m"
   echo "Confirm that the command prints the installed version of Go."
else
   echo "Failed to find the Go archive file: $file_name"
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment