Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 1, 2025

Problem

When running the installation script on macOS, users encountered critical issues:

  1. Homebrew installation fails with sudo: Running the script with sudo causes Homebrew to fail with the error "Don't run this as root!" because Homebrew explicitly forbids root execution for security reasons.

  2. Missing Xcode Command Line Tools: Homebrew requires Xcode Command Line Tools as a prerequisite, but the script attempted to install Homebrew without verifying this dependency was present, leading to installation failures.

  3. Non-interactive mode sudo issues: When running via curl | bash, Homebrew installation fails because it can't prompt for sudo password in non-interactive mode (stdin is not a TTY).

  4. Docker detection only checks Homebrew: The script only checked if Docker was installed via Homebrew, ignoring Docker installations from other sources (e.g., direct download from Docker's website).

Solution

This PR adds essential checks and warnings to prevent these failures and provide clear user guidance:

1. Root/Sudo Execution Check

Added an early check that detects if the script is run with sudo or as root:

if [ "$EUID" -eq 0 ]; then
    echo "❌ This script should NOT be run with sudo or as root"
    echo "   Homebrew installation requires a regular user account"
    # ... provides correct usage instructions
    exit 1
fi

This provides immediate, actionable feedback to users before any installation attempts.

2. Xcode Command Line Tools Check

Added a new ensure_xcode_tools() function that runs before any other installation steps:

ensure_xcode_tools() {
    if ! xcode-select -p &>/dev/null; then
        echo "📦 Xcode Command Line Tools are required but not installed"
        xcode-select --install 2>/dev/null || true
        # Provide clear instructions and exit
    else
        echo "✅ Xcode Command Line Tools already installed"
    fi
}

This function:

  • Checks if Xcode Command Line Tools are installed using xcode-select -p
  • Triggers the installation dialog if they're missing
  • Provides clear instructions to the user about completing the installation
  • Exits gracefully and asks the user to re-run the script after installation completes

3. Non-Interactive Mode Warning

Added detection and warning when Homebrew is being installed in non-interactive mode:

if [ ! -t 0 ]; then
    echo "⚠️  WARNING: Running in non-interactive mode (stdin is not a TTY)"
    echo "   Homebrew installation requires sudo access and needs to prompt for your password."
    # ... provides alternative interactive installation steps
fi

This warns users that the Homebrew installation may fail due to inability to prompt for sudo password, and provides clear instructions to run the script interactively instead.

4. Improved Docker Detection

Updated Docker installation check to detect Docker from any installation source:

# Check if Docker is already available (from any source)
if command_exists docker; then
    echo "✅ Docker already installed"
elif ! brew list --cask docker &>/dev/null; then
    echo "📦 Installing Docker Desktop..."
    brew install --cask docker
else
    echo "✅ Docker Desktop already installed via Homebrew"
fi

This ensures the script respects existing Docker installations from any source (direct download, other package managers, etc.) and avoids unnecessary reinstallation attempts.

5. Updated Installation Order

Modified the main() function to ensure proper order of operations:

1. Check for root/sudo execution → exit if detected
2. Check for Xcode Command Line Tools → install if missing, exit with instructions
3. Clone setup repository → now safe with git available from Xcode tools
4. Install Homebrew with non-interactive mode warning → now safe with Xcode tools present
5. Continue with package installation (with improved Docker detection)

Changes Made

  • Root/Sudo Check (Lines 20-29): Prevents running the entire script with sudo, which causes Homebrew installation to fail. Includes clarification that sudo will be prompted for specific tasks.

  • ensure_xcode_tools Function (Lines 44-70): Checks if Xcode Command Line Tools are installed, triggers installation if missing, and provides clear user instructions.

  • main() Function (Lines 236-237): Calls ensure_xcode_tools() before attempting any other installations, ensuring proper order of operations.

  • setup_dnsmasq Function (Line 137): Informs users that the dnsmasq setup step will require sudo access for system configuration.

  • install_homebrew Function (Lines 101-112): Detects non-interactive mode and warns that Homebrew installation may fail due to inability to prompt for sudo password. Provides alternative interactive installation steps.

  • Docker Installation Check (Lines 277-286): Now checks if Docker is available from any source first using command_exists docker, not just Homebrew. Only attempts Homebrew installation if Docker is not found on the system.

  • clone_setup_repo Function: Improved error messaging for clarity.

Impact

Before:

  • ❌ Running with sudo → Homebrew installation fails silently or with confusing error
  • ❌ Missing Xcode tools → Homebrew installation fails with unclear error messages
  • ❌ Running via curl | bash → Homebrew fails with sudo access error in non-interactive mode
  • ❌ Docker from non-Homebrew sources → Script attempts to reinstall Docker

After:

  • ✅ Running with sudo → Clear error message with correct usage instructions
  • ✅ Missing Xcode tools → Detected immediately, installation triggered, user receives clear guidance
  • ✅ Running via curl | bash → Warning displayed with instructions to run interactively if needed
  • ✅ Docker from any source → Properly detected and respected, no unnecessary reinstallation
  • ✅ Better user experience with actionable error messages at each step

Key Points About Sudo and Interactive Mode

  • The script should NOT be run with sudo (e.g., sudo bash install.sh), as this causes Homebrew installation to fail
  • The script WILL prompt for sudo password when needed for specific tasks like dnsmasq setup:
    • Creating /etc/resolver directory
    • Writing resolver configuration
    • Starting dnsmasq service
  • When run via curl | bash (non-interactive mode), Homebrew's sudo prompts may fail
  • Recommended: Download and run the script interactively for best results:
    curl -fsSL https://raw.githubusercontent.com/munezaclovis/setup/refs/heads/main/install.sh -o setup.sh
    bash setup.sh

Testing

  • ✅ Bash syntax validation passed
  • ✅ Script maintains backward compatibility with existing CI/CD workflows
  • ✅ No breaking changes to existing functionality
  • ✅ Proper execution order verified
  • ✅ Non-interactive mode detection working correctly
  • ✅ Docker detection works for installations from any source

These minimal, surgical changes directly address the reported issues while maintaining full backward compatibility and providing clear guidance for different execution scenarios.

Original prompt

When I run this command on an actual macos. I run into these issues.

when installing homebrew, It needs xcode command tools. Please make sure to install before homebrew. (xcode-select --install)

next is, when I got to the homebrew step, I got this

📦 Installing Homebrew... Warning: Running in non-interactive mode because stdinis not a TTY. ==> Checking forsudoaccess (which may request your password)... Don't run this as root! 🧹 Cleaning up temporary files...
I'm not sure why this one is failing bcz I literally run this command as sudo "curl -fsSL https://raw.githubusercontent.com/munezaclovis/setup/refs/heads/main/install.sh | sudo bash"


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: munezaclovis <51137458+munezaclovis@users.noreply.github.com>
Copilot AI changed the title When I run this command on an actual macos. I run into these issues. when installing homebrew, It needs xcode command tools. Please make sure to install before homebrew. (xcode-select --install) next is, when I got to the homebrew step, I got this ... Fix sudo execution error and add Xcode Command Line Tools prerequisite check Oct 1, 2025
Copilot AI requested a review from munezaclovis October 1, 2025 14:41
@munezaclovis
Copy link
Collaborator

@copilot Good, now we have a problem. What's gonna happen when we get to the dnsmasq step since we're pretty much need sudo access lol

Co-authored-by: munezaclovis <51137458+munezaclovis@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 1, 2025

@copilot Good, now we have a problem. What's gonna happen when we get to the dnsmasq step since we're pretty much need sudo access lol

Good catch! I've updated the script to clarify this (commit a2b680f).

The key distinction:

  • ❌ Don't run the entire script with sudo: sudo bash install.sh (breaks Homebrew)
  • ✅ Run script normally: bash install.sh (script will prompt for sudo when needed)

The script now:

  1. Still blocks running the entire script as root (prevents Homebrew failure)
  2. Clarifies in the error message that sudo will be prompted for specific tasks
  3. Adds a note before dnsmasq setup that it requires sudo

When you run it normally, the script will prompt for your password at lines 139, 144, and 151 for:

  • Creating /etc/resolver
  • Writing resolver config
  • Starting dnsmasq service

This is the standard approach for setup scripts - run as regular user, prompt for sudo only when needed.

@munezaclovis
Copy link
Collaborator

@copilot getting this error now

📦 Installing Homebrew...
Warning: Running in non-interactive mode because stdin is not a TTY.
==> Checking for sudo access (which may request your password)...
Need sudo access on macOS (e.g. the user [user] needs to be an Administrator)!

Co-authored-by: munezaclovis <51137458+munezaclovis@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 1, 2025

@copilot getting this error now

...

This error occurs because Homebrew needs sudo access to install, but when running via curl | bash (non-interactive mode), it can't prompt for your password.

I've added a warning that will appear before Homebrew installation (commit 7114af1). The script will now:

  1. Detect when running in non-interactive mode
  2. Display a warning about potential sudo issues
  3. Provide instructions to run interactively if the installation fails

To avoid this issue, run the script interactively:

curl -fsSL https://raw.githubusercontent.com/munezaclovis/setup/refs/heads/main/install.sh -o setup.sh
bash setup.sh

This way, Homebrew can properly prompt for your sudo password when needed.

@munezaclovis
Copy link
Collaborator

@copilot It is possible that docker might already be installed from another place. not only from homebrew, Please edit the code that check if docker is install to account for that situation

Co-authored-by: munezaclovis <51137458+munezaclovis@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 29, 2025

@copilot It is possible that docker might already be installed from another place. not only from homebrew, Please edit the code that check if docker is install to account for that situation

Updated the Docker installation check (commit 97a35d4). The script now:

  1. First checks if docker command exists (from any installation source)
  2. Only attempts Homebrew installation if Docker is not found
  3. Skips installation if Docker is already available from any source

This properly handles Docker Desktop installed directly from Docker's website or other sources, not just Homebrew.

Copilot AI changed the title Fix sudo execution error and add Xcode Command Line Tools prerequisite check Fix sudo execution error, add Xcode Command Line Tools check, and improve Docker detection Oct 29, 2025
@munezaclovis munezaclovis marked this pull request as ready for review October 29, 2025 23:28
@munezaclovis munezaclovis merged commit 8152489 into main Oct 29, 2025
3 checks passed
@munezaclovis munezaclovis deleted the copilot/fix-652ac40a-3dae-43ae-95f5-fec155831efc branch October 29, 2025 23:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants