Script 04 · Runtime

4. Install Golang

Installs the latest Go toolchain from go.dev and updates shell profile PATH entries.

Category: Runtime Risk: Low Lines: calculating Language: Bash / Linux
Back to index

What this script does

  • Prepare the VPS to build Go-based Erupe components.
  • Install Go outside distro package lag.
  • Expose go and GOPATH binaries to user shells.

Prerequisites

  • curl, tar, grep
  • Root permission for /usr/local
  • Internet access to go.dev

Execution flow

  1. Fetches latest Go version metadata
  2. Detects CPU architecture
  3. Downloads tarball
  4. Replaces /usr/local/go
  5. Updates shell profiles

Validation checklist

  • go version
  • which go
  • echo $PATH

Operational cautions

  • Replacing /usr/local/go removes the previous manual Go install.
  • Restart shell if PATH is not active.

Original script notes

ℹ️ Script Info: Automatically fetches and installs the latest version of the Go programming language directly from Google's servers, configuring the necessary path variables for all users.

Script source
cat << 'EOF' > install_go_auto.sh && chmod +x install_go_auto.sh && source ./install_go_auto.sh
#!/bin/bash

# Configuration Colors
GREEN='\033[0;32m'; RED='\033[0;31m'; YELLOW='\033[1;33m'; NC='\033[0m'

# Function to keep terminal open on error (since we use source)
install_golang() {
    echo -e "${GREEN}[*] Starting Golang Auto-Install Script...${NC}"

    # 1. Check Dependencies
    for cmd in curl tar grep; do
        if ! command -v "$cmd" > /dev/null; then
            echo -e "${RED}[!] Error: $cmd not found. Please install it first.${NC}"
            return 1
        fi
    done

    # 2. Find Latest Version
    echo -e "${YELLOW}[*] Searching for latest Golang version...${NC}"
    LATEST_VERSION=$(curl -s https://go.dev/dl/?mode=json | grep -o 'go[0-9.]*' | head -n 1)
    
    if [ -z "$LATEST_VERSION" ]; then
        echo -e "${RED}[!] Failed to check version. Check internet connection.${NC}"
        return 1
    fi
    echo -e "${GREEN}[+] Latest version found: ${LATEST_VERSION}${NC}"

    # 3. Check Architecture
    ARCH=$(uname -m)
    case $ARCH in
        x86_64) GO_ARCH="amd64" ;;
        aarch64) GO_ARCH="arm64" ;;
        *) echo -e "${RED}[!] Architecture $ARCH not supported by this script.${NC}"; return 1 ;;
    esac

    # 4. Download
    FILE="${LATEST_VERSION}.linux-${GO_ARCH}.tar.gz"
    URL="https://go.dev/dl/$FILE"
    echo -e "${YELLOW}[*] Downloading $FILE...${NC}"
    
    if ! curl -L --progress-bar -o "$FILE" "$URL"; then
        echo -e "${RED}[!] Download failed.${NC}"; return 1
    fi

    # 5. Install (Sudo required)
    echo -e "${YELLOW}[*] Installing to /usr/local/go (requires sudo password)...${NC}"
    if sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf "$FILE"; then
        rm -f "$FILE"
        echo -e "${GREEN}[+] Extraction complete.${NC}"
    else
        echo -e "${RED}[!] Extraction failed.${NC}"; return 1
    fi

    # 6. Configure Path (Persistent)
    update_shell_rc() {
        if [ -f "$1" ]; then
            if ! grep -q "/usr/local/go/bin" "$1"; then
                echo -e "\n# GOLANG CONFIG" >> "$1"
                echo "export PATH=\$PATH:/usr/local/go/bin:\$HOME/go/bin" >> "$1"
                echo -e "${GREEN}[+] Path added to $1${NC}"
            else
                echo -e "${YELLOW}[i] Path already exists in $1${NC}"
            fi
        fi
    }
    
    update_shell_rc "$HOME/.bashrc"
    update_shell_rc "$HOME/.profile"
    update_shell_rc "$HOME/.zshrc"

    # 7. Apply Path to Current Session (Environment Variable)
    export PATH=$PATH:/usr/local/go/bin:$HOME/go/bin
    
    echo -e "${GREEN}[OK] Installation Complete & Environment Variable active!${NC}"
    echo -e "${YELLOW}[i] Script saved at: $(pwd)/install_go_auto.sh${NC}"
}

# Run main function
install_golang

# Check final version
if command -v go > /dev/null; then
    echo -e "\nInstalled Version:"
    go version
else
    echo -e "${RED}[!] Failed to call 'go' command. Try restarting the terminal.${NC}"
fi
EOF
Done