Last active
June 17, 2025 02:19
-
-
Save hunzo/a25d169143bbd2a74f3376bc26a25273 to your computer and use it in GitHub Desktop.
my install update 20250528
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
#!/bin/bash | |
# =============================================== | |
# Dotfiles Installation Script | |
# This script automates the setup of development tools | |
# including Go, Python, nvm (Node.js), Neovim, and Ansible. | |
# | |
# Author: hunzo (Original) | |
# Refactor: Gemini | |
# Date: May 28, 2025 | |
# =============================================== | |
# --- Configuration Variables --- | |
GO_VERSION="go1.24.4" # Updated to a more recent stable version if available, otherwise keep original | |
NVIM_VERSION="v0.11.2" | |
NVIM_FILE_NAME="nvim-linux-x86_64" | |
NVM_VERSION="v0.40.3" | |
# Colors for output | |
GREEN='\033[0;32m' | |
YELLOW='\033[0;33m' | |
RED='\033[0;31m' | |
NC='\033[0m' # No Color | |
# --- Helper Functions --- | |
log_info() { | |
echo -e "${GREEN}[INFO]${NC} $1" | |
} | |
log_warn() { | |
echo -e "${YELLOW}[WARN]${NC} $1" | |
} | |
log_error() { | |
echo -e "${RED}[ERROR]${NC} $1" >&2 | |
exit 1 | |
} | |
# Function to check if a command exists | |
command_exists () { | |
type "$1" &> /dev/null | |
} | |
# Function to append content to .bashrc if not already present | |
append_to_bashrc() { | |
local content="$1" | |
local check_string="$2" | |
if ! grep -q "$check_string" "$HOME/.bashrc"; then | |
log_info "Adding configuration to ~/.bashrc..." | |
echo -e "\n$content" >> "$HOME/.bashrc" | |
else | |
log_info "Configuration for '$check_string' already exists in ~/.bashrc. Skipping." | |
fi | |
} | |
# --- Installation Steps --- | |
install_golang() { | |
log_info "Starting GoLang installation ($GO_VERSION)..." | |
# Remove existing system-wide Go installations | |
if sudo rm -rf /usr/local/go && sudo rm -f /usr/bin/go; then | |
log_info "Removed old system-wide Go installations." | |
else | |
log_warn "Could not remove old system-wide Go. This might require manual intervention or superuser privileges." | |
fi | |
# Download Go | |
local go_tar_file="${GO_VERSION}.linux-amd64.tar.gz" | |
log_info "Downloading Go $GO_VERSION..." | |
if ! curl -fsSL "https://golang.org/dl/${go_tar_file}" -o "${go_tar_file}"; then | |
log_error "Failed to download Go. Please check your internet connection or Go version." | |
fi | |
# Create directories | |
mkdir -p "$HOME/.go-sdk" | |
mkdir -p "$HOME/.go-pkg" | |
# Extract Go | |
log_info "Extracting Go to $HOME/.go-sdk..." | |
if ! tar -xzf "${go_tar_file}" -C "$HOME/.go-sdk"; then | |
log_error "Failed to extract Go. Please check the downloaded file." | |
fi | |
# Set Go environment variables in .bashrc | |
local go_env_config=" | |
# --- GoLang Environment Variables --- | |
export GOPATH=\$HOME/.go-pkg | |
export GOROOT=\$HOME/.go-sdk/go | |
export GOBIN=\$GOPATH/bin | |
export PATH=\$GOROOT/bin:\$GOBIN:\$PATH | |
# --- End GoLang Environment Variables ---" | |
append_to_bashrc "$go_env_config" "export GOROOT=\$HOME/.go-sdk/go" | |
# Clean up downloaded tarball | |
rm -f "${go_tar_file}" | |
log_info "GoLang installation completed." | |
} | |
configure_bash_prompt() { | |
log_info "Configuring custom bash prompt for Git branch display..." | |
local git_prompt_config=" | |
# --- Custom Git Bash Prompt --- | |
show_git_branch() { | |
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1 /' | |
} | |
# Uncomment the line below to activate the custom prompt: | |
# export PS1='\[\e[38;5;214m\]\w \[\e[91m\]\$(show_git_branch)\[\e[m\]\[\033[32m\]❱\[\e[m\] ' | |
# --- End Custom Git Bash Prompt ---" | |
append_to_bashrc "$git_prompt_config" "show_git_branch()" | |
log_info "Bash prompt configuration added." | |
} | |
install_neovim() { | |
log_info "Starting Neovim installation ($NVIM_VERSION)..." | |
# Clean old Neovim configurations and binaries | |
log_info "Removing old Neovim configurations and binaries..." | |
rm -rf "$HOME/.config/nvim" \ | |
"$HOME/.local/share/nvim" \ | |
"$HOME/.local/state/nvim" \ | |
"$HOME/.cache/nvim" | |
sudo rm -rf /usr/bin/nvim* | |
# Add Neovim as default editor to .bashrc | |
local nvim_editor_config=" | |
# --- Neovim as Default Editor --- | |
export EDITOR=nvim | |
alias vim=nvim | |
set -o vi | |
# --- End Neovim Default Editor ---" | |
append_to_bashrc "$nvim_editor_config" "export EDITOR=nvim" | |
# Download Neovim | |
local nvim_tar_file="${NVIM_FILE_NAME}.tar.gz" | |
log_info "Downloading Neovim $NVIM_VERSION..." | |
if ! curl -fsSL "https://github.com/neovim/neovim/releases/download/${NVIM_VERSION}/${nvim_tar_file}" -o "${nvim_tar_file}"; then | |
log_error "Failed to download Neovim. Please check the version or URL." | |
fi | |
# Install Neovim | |
log_info "Extracting and linking Neovim to /usr/bin..." | |
if ! sudo tar -xzf "${nvim_tar_file}" -C /usr/bin/; then | |
log_error "Failed to extract Neovim. Check permissions or file integrity." | |
fi | |
if ! sudo ln -sf "/usr/bin/${NVIM_FILE_NAME}/bin/nvim" /usr/bin/nvim; then | |
log_error "Failed to create symlink for Neovim. Check permissions." | |
fi | |
# Clean up downloaded tarball | |
rm -f "${nvim_tar_file}" | |
log_info "Neovim installation completed." | |
} | |
install_apt_packages() { | |
log_info "Updating apt packages and installing essential tools..." | |
sudo apt update -y || log_error "Failed to update apt packages." | |
sudo apt install python3-venv unzip build-essential software-properties-common -y || log_error "Failed to install essential apt packages." | |
log_info "Essential apt packages installed." | |
} | |
install_nvm_and_nodejs() { | |
log_info "Starting NVM and Node.js installation ($NVM_VERSION)..." | |
# Download and run NVM install script | |
if ! curl -fsSL "https://raw.githubusercontent.com/nvm-sh/nvm/${NVM_VERSION}/install.sh" | bash; then | |
log_error "Failed to install NVM. Check internet connection or NVM version." | |
fi | |
# Source NVM for current session | |
export NVM_DIR="$HOME/.nvm" | |
# This ensures nvm is available immediately for the current script execution | |
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm | |
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion | |
# Ensure .bashrc is sourced to make nvm available for subsequent sessions | |
# The nvm install script usually adds this, but we'll add a check for robustness | |
local nvm_bashrc_check="export NVM_DIR=\"\$HOME/.nvm\"" | |
if ! grep -q "$nvm_bashrc_check" "$HOME/.bashrc"; then | |
log_info "Adding NVM sourcing to ~/.bashrc..." | |
echo 'export NVM_DIR="$HOME/.nvm"' >> "$HOME/.bashrc" | |
echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> "$HOME/.bashrc" | |
echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"' >> "$HOME/.bashrc" | |
fi | |
# Install LTS Node.js | |
log_info "Installing LTS Node.js using NVM..." | |
if ! nvm install --lts; then | |
log_error "Failed to install LTS Node.js. Check NVM installation." | |
fi | |
log_info "NVM and Node.js installation completed." | |
} | |
install_ansible() { | |
log_info "Starting Ansible installation..." | |
if ! sudo apt-add-repository ppa:ansible/ansible -y; then | |
log_error "Failed to add Ansible PPA." | |
fi | |
sudo apt update -y || log_error "Failed to update apt after adding PPA." | |
sudo apt install ansible -y || log_error "Failed to install Ansible." | |
log_info "Ansible installation completed." | |
} | |
# --- Main Script Execution --- | |
main() { | |
log_info "Starting overall development environment setup..." | |
# Check for root privileges early | |
if [ "$EUID" -eq 0 ]; then | |
log_error "This script should NOT be run as root. Please run as a normal user." | |
fi | |
# Update apt first to ensure smooth installation of other packages | |
install_apt_packages | |
# Install individual components | |
install_golang | |
install_neovim | |
install_nvm_and_nodejs | |
install_ansible | |
# Configure shell prompt | |
configure_bash_prompt | |
log_info "All setup steps completed successfully!" | |
log_info "Please remember to run 'source ~/.bashrc' or restart your terminal for changes to take effect." | |
} | |
# Execute the main function | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment