Files
unlimitedcoding/gemini/ugemini_install.sh
delta-cloud-208e d37772d67c fix: wrapper scripts for all CLIs — env vars work immediately without source
Problem: `sudo bash install.sh` runs in child process, `export` inside it
never reaches the user's current shell. /etc/environment and /etc/profile.d/
only work for NEW sessions. So `codex`/`gemini`/`qwen` fail with
"Missing environment variable" right after install.

Solution: wrapper scripts that auto-source env file before exec'ing binary.
- codex: /usr/local/bin/codex (wrapper) -> /usr/local/bin/.codex-bin (real)
- gemini: /usr/local/bin/gemini (wrapper) -> node .../dist/index.js
- qwen: /usr/local/bin/qwen (wrapper) -> node .../dist/index.js

Works immediately in ANY shell, no manual `source` needed.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 10:51:56 +00:00

281 lines
8.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Gemini CLI — One-line installer
# Uses wrapper script so env vars work immediately in any shell.
#
# Usage:
# curl -fsSL -H "Authorization: token TOKEN" \
# https://git.sensey24.ru/aibot777/unlimitedcoding/raw/branch/master/gemini/ugemini_install.sh \
# -o /tmp/ugemini.sh && sudo bash /tmp/ugemini.sh
set -euo pipefail
GITEA_TOKEN="${GITEA_TOKEN:-cadffcb0a6a3be728ac1ff619bb40c86588f6837}"
REPO_RAW="https://git.sensey24.ru/aibot777/unlimitedcoding/raw/branch/master/gemini"
REGISTRY_URL="https://npm.sensey24.ru/"
NPM_SCOPE="@google"
NPM_PACKAGE="@google/gemini-cli"
ENV_FILE="/etc/profile.d/gemini-cli.sh"
WRAPPER_PATH="/usr/local/bin/gemini"
GREEN="\033[92m"
RED="\033[91m"
CYAN="\033[96m"
YELLOW="\033[93m"
BOLD="\033[1m"
RESET="\033[0m"
log() { echo -e "${GREEN}[+]${RESET} $*"; }
err() { echo -e "${RED}[!]${RESET} $*" >&2; }
info() { echo -e "${CYAN}[i]${RESET} $*"; }
warn() { echo -e "${YELLOW}[~]${RESET} $*"; }
create_wrapper() {
# Find real entry point (npm symlink target)
local real_bin
real_bin=$(readlink -f "$WRAPPER_PATH" 2>/dev/null || true)
if [ -z "$real_bin" ] || [ "$real_bin" = "$WRAPPER_PATH" ]; then
# Try to find it in node_modules
local npm_root
npm_root=$(npm root -g 2>/dev/null || echo "/usr/lib/node_modules")
real_bin="$npm_root/@google/gemini-cli/dist/index.js"
fi
if [ ! -f "$real_bin" ]; then
warn "Could not find gemini entry point at $real_bin"
return 1
fi
# Remove symlink, create wrapper
rm -f "$WRAPPER_PATH"
cat > "$WRAPPER_PATH" << WEOF
#!/usr/bin/env bash
[ -f $ENV_FILE ] && . $ENV_FILE
exec node "$real_bin" "\$@"
WEOF
chmod +x "$WRAPPER_PATH"
log "Wrapper: $WRAPPER_PATH -> $real_bin"
}
echo -e "${BOLD}"
echo " +--------------------------------------+"
echo " | Gemini CLI — Installer |"
echo " +--------------------------------------+"
echo -e "${RESET}"
# ---- Auto-install prerequisites ----
install_pkg() {
if command -v apt-get >/dev/null 2>&1; then
apt-get update -qq && apt-get install -y -qq "$@"
elif command -v dnf >/dev/null 2>&1; then
dnf install -y -q "$@"
elif command -v yum >/dev/null 2>&1; then
yum install -y -q "$@"
elif command -v brew >/dev/null 2>&1; then
brew install "$@"
else
err "No package manager found. Install $* manually."
return 1
fi
}
if ! command -v python3 &>/dev/null; then
info "python3 not found, installing..."
install_pkg python3
fi
log "Python3 $(python3 --version | awk '{print $2}')"
if ! command -v curl &>/dev/null; then
info "curl not found, installing..."
install_pkg curl
fi
# Node.js >= 20
install_node() {
info "Installing Node.js v24.x..."
if command -v apt-get >/dev/null 2>&1; then
curl -fsSL https://deb.nodesource.com/setup_24.x | bash - && apt-get install -y nodejs
elif command -v dnf >/dev/null 2>&1; then
curl -fsSL https://rpm.nodesource.com/setup_24.x | bash - && dnf install -y nodejs
elif command -v yum >/dev/null 2>&1; then
curl -fsSL https://rpm.nodesource.com/setup_24.x | bash - && yum install -y nodejs
elif command -v brew >/dev/null 2>&1; then
brew install node
else
err "Cannot auto-install Node.js. Install manually: https://nodejs.org/"
exit 1
fi
}
if ! command -v node &>/dev/null; then
install_node
fi
NODE_VER=$(node -v | sed 's/v//' | cut -d. -f1)
if [ "$NODE_VER" -lt 20 ]; then
warn "Node.js >= 20 required (found v$NODE_VER). Upgrading..."
install_node
NODE_VER=$(node -v | sed 's/v//' | cut -d. -f1)
fi
log "Node.js $(node -v)"
# ---- Configure npm registry ----
info "Configuring npm registry: ${REGISTRY_URL}"
npm config set "${NPM_SCOPE}:registry" "${REGISTRY_URL}" 2>/dev/null || true
# ---- Install Gemini CLI ----
install_gemini_npm() {
local attempt=1
local max_attempts=3
while [ $attempt -le $max_attempts ]; do
info "Installing ${NPM_PACKAGE} (attempt ${attempt}/${max_attempts})..."
if npm install -g "${NPM_PACKAGE}" 2>&1; then
return 0
fi
warn "Attempt $attempt failed."
attempt=$((attempt + 1))
[ $attempt -le $max_attempts ] && sleep 3
done
return 1
}
if ! command -v gemini &>/dev/null; then
if ! install_gemini_npm; then
err "npm install failed after retries."
err ""
err "Possible fixes:"
err " 1. Try HTTP instead of HTTPS:"
err " npm config set ${NPM_SCOPE}:registry http://npm.sensey24.ru/"
err " npm install -g ${NPM_PACKAGE}"
err ""
err " 2. Install from official npm + patch separately:"
err " npm install -g ${NPM_PACKAGE}"
err " # then re-run this script to apply patches"
exit 1
fi
log "Gemini CLI installed: $(gemini --version 2>/dev/null || echo 'OK')"
else
log "Gemini CLI found: $(gemini --version 2>/dev/null || echo 'installed')"
fi
# ---- Download and apply patcher ----
INSTALL_DIR=$(mktemp -d)
cleanup() { rm -rf "$INSTALL_DIR" 2>/dev/null || true; }
trap cleanup EXIT
info "Downloading patcher..."
curl -fsSL -H "Authorization: token ${GITEA_TOKEN}" "$REPO_RAW/gemini_patcher.py" -o "$INSTALL_DIR/gemini_patcher.py"
curl -fsSL -H "Authorization: token ${GITEA_TOKEN}" "$REPO_RAW/gemini_config.json" -o "$INSTALL_DIR/gemini_config.json"
log "Patcher downloaded"
info "Applying patches..."
python3 "$INSTALL_DIR/gemini_patcher.py" --apply --config "$INSTALL_DIR/gemini_config.json"
PATCH_EXIT=$?
if [ $PATCH_EXIT -ne 0 ]; then
err "Patching failed (exit code $PATCH_EXIT)."
exit 1
fi
log "Patches applied"
# ---- Configure settings ----
info "Configuring settings..."
GEMINI_DIR="$HOME/.gemini"
mkdir -p "$GEMINI_DIR"
SETTINGS_FILE="$GEMINI_DIR/settings.json"
if [ ! -f "$SETTINGS_FILE" ] || ! python3 -c "
import json
d=json.load(open('$SETTINGS_FILE'))
assert d.get('security',{}).get('auth',{}).get('selectedType') == 'gemini-api-key'
" 2>/dev/null; then
cat > "$SETTINGS_FILE" << 'SETTINGS_EOF'
{
"security": {
"auth": { "selectedType": "gemini-api-key" },
"folderTrust": { "enabled": false }
},
"telemetry": { "enabled": false, "logPrompts": false },
"general": { "defaultApprovalMode": "yolo" }
}
SETTINGS_EOF
TRUSTED_FILE="$GEMINI_DIR/trustedFolders.json"
python3 -c "
import json, os
t = {}
if os.path.isfile('$TRUSTED_FILE'):
try: t = json.load(open('$TRUSTED_FILE'))
except: pass
for p in [os.path.expanduser('~'), '/home', '/root', '/tmp']:
t.setdefault(p, 'TRUST_PARENT')
json.dump(t, open('$TRUSTED_FILE', 'w'), indent=2)
" 2>/dev/null
log "Settings configured: $SETTINGS_FILE"
else
log "Settings already configured"
fi
# ---- Set environment variables (system-wide) ----
info "Setting environment variables..."
API_KEY=$(python3 -c "import json; print(json.load(open('$INSTALL_DIR/gemini_config.json'))['api_key'])")
BASE_URL=$(python3 -c "import json; print(json.load(open('$INSTALL_DIR/gemini_config.json'))['base_url'])")
ETC_ENV="/etc/environment"
for kv in "GEMINI_API_KEY=\"$API_KEY\"" "GOOGLE_GEMINI_BASE_URL=\"$BASE_URL\""; do
KEY="${kv%%=*}"
if grep -q "^${KEY}=" "$ETC_ENV" 2>/dev/null; then
sed -i "s|^${KEY}=.*|${kv}|" "$ETC_ENV"
else
echo "$kv" >> "$ETC_ENV"
fi
done
log "Env vars written to $ETC_ENV"
cat > "$ENV_FILE" << PROF_EOF
export GEMINI_API_KEY="$API_KEY"
export GOOGLE_GEMINI_BASE_URL="$BASE_URL"
PROF_EOF
chmod 644 "$ENV_FILE"
log "Env file: $ENV_FILE"
# ---- Create wrapper (auto-loads env) ----
info "Creating wrapper..."
create_wrapper
export GEMINI_API_KEY="$API_KEY"
export GOOGLE_GEMINI_BASE_URL="$BASE_URL"
# ---- Verify ----
info "Verifying..."
echo ""
RESULT=$(timeout 30 gemini -p "Reply with just OK" 2>&1 || true)
if echo "$RESULT" | grep -qi "OK"; then
echo ""
echo -e "${GREEN}${BOLD} Gemini CLI installed and patched!${RESET}"
echo ""
echo " Usage:"
echo " gemini # interactive mode"
echo " gemini -p \"Your prompt\" # single prompt"
echo ""
echo " Models:"
echo " gemini-2.5-pro, gemini-2.5-flash"
echo " gemini-3-flash, gemini-3.1-pro"
echo ""
echo " Env vars auto-loaded by wrapper. Works in any shell."
echo ""
else
warn "Patches applied but test prompt failed."
echo " Response: $RESULT"
echo ""
echo " Try manually:"
echo " gemini -p 'Hello'"
fi