Skip to content

Instantly share code, notes, and snippets.

@theprantadutta
Last active September 17, 2024 08:15
Show Gist options
  • Save theprantadutta/698d1fdc8bb5e38207a886cf102b34d4 to your computer and use it in GitHub Desktop.
Save theprantadutta/698d1fdc8bb5e38207a886cf102b34d4 to your computer and use it in GitHub Desktop.
A bash script that will install Go on Ubuntu machine, asking for the Go version and ensuring it has sudo privileges. Run this with ```curl -O https://gist.githubusercontent.com/theprantadutta/698d1fdc8bb5e38207a886cf102b34d4/raw/install-go.sh && chmod +x install-go.sh && ./install-go.sh```
#!/bin/bash
# Colors for echo statements
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Check for sudo privileges
if [ "$(id -u)" -ne 0 ]; then
echo -e "${RED}This script requires sudo privileges. Please run it with 'sudo'.${NC}"
exit 1
fi
# Step 1: Ask for Go version
echo -e "${YELLOW}Please enter the Go version you want to install (e.g., 1.23.1):${NC}"
read GO_VERSION
# Step 2: Construct the download link
GO_TARBALL="go${GO_VERSION}.linux-amd64.tar.gz"
DOWNLOAD_LINK="https://go.dev/dl/${GO_TARBALL}"
# Step 3: Download Go using wget
echo -e "${YELLOW}Downloading Go ${GO_VERSION} using wget...${NC}"
wget ${DOWNLOAD_LINK}
# Step 4: Install Go
echo -e "${YELLOW}Installing Go...${NC}"
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf ${GO_TARBALL}
# Step 5: Set up PATH
echo -e "${YELLOW}Setting up Go path...${NC}"
# Add the Go binary path to ~/.zshrc
if ! grep -q "/usr/local/go/bin" ~/.zshrc; then
echo 'export PATH=$PATH:/usr/local/go/bin' | sudo tee -a ~/.zshrc
fi
# Source ~/.zshrc to apply changes
source ~/.zshrc
# Step 6: Verify installation
echo -e "${YELLOW}Verifying Go installation...${NC}"
if command -v go >/dev/null 2>&1; then
go version
else
echo -e "${RED}Go command not found. Please restart your terminal or source your profile.${NC}"
exit 1
fi
# Step 7: Clean up
echo -e "${YELLOW}Cleaning up...${NC}"
rm -f ${GO_TARBALL}
echo -e "${GREEN}Go ${GO_VERSION} installed successfully and tar file removed!${NC}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment