Created
October 9, 2025 11:36
-
-
Save Pymmdrza/3bf7c0d26ca64600bd5b133b763c216d to your computer and use it in GitHub Desktop.
Easy Install Script For GO in Ubuntu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| IFS=$'\n\t' | |
| # Function to print info messages | |
| info() { | |
| echo -e "\033[1;34m[INFO]\033[0m $*" | |
| } | |
| # Function to print error messages | |
| error() { | |
| echo -e "\033[1;31m[ERROR]\033[0m $*" >&2 | |
| exit 1 | |
| } | |
| # Check for root privileges | |
| if [ "$EUID" -ne 0 ]; then | |
| SUDO="sudo" | |
| else | |
| SUDO="" | |
| fi | |
| # Default Go version (change if needed) | |
| GO_VERSION="1.23.2" | |
| ARCH=$(dpkg --print-architecture) | |
| GO_TARBALL="go${GO_VERSION}.linux-${ARCH}.tar.gz" | |
| GO_URL="https://go.dev/dl/${GO_TARBALL}" | |
| # Remove any old Go installation | |
| info "Removing any existing Go installation..." | |
| $SUDO rm -rf /usr/local/go | |
| # Install dependencies | |
| info "Updating apt packages and installing prerequisites..." | |
| $SUDO apt-get update -y | |
| $SUDO apt-get install -y curl tar | |
| # Download Go tarball | |
| info "Downloading Go ${GO_VERSION}..." | |
| curl -fsSL -o /tmp/${GO_TARBALL} "${GO_URL}" || error "Failed to download Go" | |
| # Extract Go to /usr/local | |
| info "Extracting Go to /usr/local..." | |
| $SUDO tar -C /usr/local -xzf /tmp/${GO_TARBALL} | |
| # Set up environment variables | |
| if ! grep -q "/usr/local/go/bin" ~/.bashrc; then | |
| info "Adding Go binary path to ~/.bashrc" | |
| { | |
| echo "" | |
| echo "# Go environment setup" | |
| echo "export PATH=\$PATH:/usr/local/go/bin" | |
| echo "export GOPATH=\$HOME/go" | |
| echo "export PATH=\$PATH:\$GOPATH/bin" | |
| } >> ~/.bashrc | |
| fi | |
| # Load environment variables in current shell | |
| export PATH=$PATH:/usr/local/go/bin | |
| export GOPATH=$HOME/go | |
| export PATH=$PATH:$GOPATH/bin | |
| # Verify installation | |
| info "Verifying Go installation..." | |
| if go version >/dev/null 2>&1; then | |
| info "Go successfully installed!" | |
| go version | |
| else | |
| error "Go installation failed." | |
| fi | |
| info "Installation complete. Run 'source ~/.bashrc' or restart your shell to apply environment changes." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment