Skip to content

Instantly share code, notes, and snippets.

@DavraYoung
Created January 19, 2026 15:50
Show Gist options
  • Select an option

  • Save DavraYoung/8a255f459e3768b400ce7ed2184ad983 to your computer and use it in GitHub Desktop.

Select an option

Save DavraYoung/8a255f459e3768b400ce7ed2184ad983 to your computer and use it in GitHub Desktop.
Claude Code Termux Setup - Patches Claude Code to work on Termux (fixes /tmp path issues and ripgrep)
#!/data/data/com.termux/files/usr/bin/bash
# ============================================================
# Claude Code Termux Setup Script
# ============================================================
# This script installs and patches Claude Code to work on Termux.
#
# Termux doesn't allow access to /tmp, so Claude Code fails
# because it hardcodes /tmp paths. This script patches those
# paths to use Termux-compatible directories.
#
# Usage:
# curl -fsSL https://gist.githubusercontent.com/YOUR_GIST_URL/raw | bash
# # or
# bash claude-code-termux-setup.sh
#
# Requirements:
# - Termux (https://termux.dev)
# - Node.js 18+ (pkg install nodejs-lts)
#
# ============================================================
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
echo -e "${BLUE}"
echo "╔════════════════════════════════════════════════════════╗"
echo "║ Claude Code Termux Setup Script ║"
echo "╚════════════════════════════════════════════════════════╝"
echo -e "${NC}"
# Configuration
TERMUX_TMP="${TMPDIR:-/data/data/com.termux/files/home/tmp}"
CLAUDE_CLI="/data/data/com.termux/files/usr/lib/node_modules/@anthropic-ai/claude-code/cli.js"
RIPGREP_DIR="/data/data/com.termux/files/usr/lib/node_modules/@anthropic-ai/claude-code/vendor/ripgrep/arm64-android"
PROFILE="$HOME/.bashrc"
# ============================================================
# Step 1: Check prerequisites
# ============================================================
echo -e "${YELLOW}[1/5] Checking prerequisites...${NC}"
if ! command -v node &> /dev/null; then
echo -e "${RED}Error: Node.js is not installed${NC}"
echo "Install it with: pkg install nodejs-lts"
exit 1
fi
NODE_VERSION=$(node -v | cut -d'v' -f2 | cut -d'.' -f1)
if [ "$NODE_VERSION" -lt 18 ]; then
echo -e "${RED}Error: Node.js 18+ required (found v$NODE_VERSION)${NC}"
echo "Update with: pkg upgrade nodejs-lts"
exit 1
fi
echo -e " ${GREEN}✓${NC} Node.js $(node -v)"
# ============================================================
# Step 2: Install Claude Code if not present
# ============================================================
echo -e "${YELLOW}[2/5] Installing Claude Code...${NC}"
if [ -f "$CLAUDE_CLI" ]; then
echo -e " ${GREEN}✓${NC} Claude Code already installed"
else
echo " Installing @anthropic-ai/claude-code..."
npm install -g @anthropic-ai/claude-code
if [ ! -f "$CLAUDE_CLI" ]; then
echo -e "${RED}Error: Installation failed${NC}"
exit 1
fi
echo -e " ${GREEN}✓${NC} Claude Code installed"
fi
# ============================================================
# Step 3: Create tmp directory and apply tmpdir patches
# ============================================================
echo -e "${YELLOW}[3/5] Patching tmp directory paths...${NC}"
# Create tmp directory
mkdir -p "$TERMUX_TMP/claude"
echo -e " ${GREEN}✓${NC} Created $TERMUX_TMP/claude"
# Backup original if not already backed up
if [ ! -f "$CLAUDE_CLI.bak" ]; then
cp "$CLAUDE_CLI" "$CLAUDE_CLI.bak"
echo -e " ${GREEN}✓${NC} Created backup at cli.js.bak"
else
echo -e " ${GREEN}✓${NC} Backup already exists"
fi
# Restore from backup to ensure clean state
cp "$CLAUDE_CLI.bak" "$CLAUDE_CLI"
# Apply patches using sed
# Patch 1: Sandbox TMPDIR environment variable
sed -i 's|"TMPDIR=/tmp/claude"|"TMPDIR='"$TERMUX_TMP"'/claude"|g' "$CLAUDE_CLI"
echo -e " ${GREEN}✓${NC} Patched sandbox TMPDIR"
# Patch 2: Allowed paths array
sed -i 's|"/tmp/claude","/private/tmp/claude"|"'"$TERMUX_TMP"'/claude"|g' "$CLAUDE_CLI"
echo -e " ${GREEN}✓${NC} Patched allowed paths"
# Patch 3: CLAUDE_CODE_TMPDIR simple fallback
sed -i 's#CLAUDE_CODE_TMPDIR||"/tmp"#CLAUDE_CODE_TMPDIR||"'"$TERMUX_TMP"'"#g' "$CLAUDE_CLI"
echo -e " ${GREEN}✓${NC} Patched simple fallback"
# Patch 4: Windows/Linux ternary (d0 function)
sed -i 's#(d0()==="windows"?\$q7():"/tmp")#"'"$TERMUX_TMP"'"#g' "$CLAUDE_CLI"
echo -e " ${GREEN}✓${NC} Patched platform ternary (d0)"
# Patch 5: Win32 ternary (A variable)
sed -i 's#(A==="win32"?process.env.TEMP||"C:\\\\Temp":"/tmp")#"'"$TERMUX_TMP"'"#g' "$CLAUDE_CLI"
echo -e " ${GREEN}✓${NC} Patched platform ternary (win32)"
# Restore executable permission
chmod +x "$CLAUDE_CLI"
# ============================================================
# Step 4: Install and link ripgrep
# ============================================================
echo -e "${YELLOW}[4/5] Setting up ripgrep...${NC}"
# Install ripgrep if not present
if ! command -v rg &> /dev/null; then
echo " Installing ripgrep..."
pkg install ripgrep -y
fi
echo -e " ${GREEN}✓${NC} ripgrep installed ($(rg --version | head -1))"
# Create vendor directory and symlink
mkdir -p "$RIPGREP_DIR"
ln -sf /data/data/com.termux/files/usr/bin/rg "$RIPGREP_DIR/rg"
echo -e " ${GREEN}✓${NC} Linked ripgrep to Claude Code vendor directory"
# ============================================================
# Step 5: Configure environment variables
# ============================================================
echo -e "${YELLOW}[5/5] Configuring environment...${NC}"
# Add to .bashrc if not present
if ! grep -q "CLAUDE_CODE_TMPDIR" "$PROFILE" 2>/dev/null; then
echo "" >> "$PROFILE"
echo "# Claude Code Termux configuration" >> "$PROFILE"
echo "export CLAUDE_CODE_TMPDIR=\"$TERMUX_TMP\"" >> "$PROFILE"
echo -e " ${GREEN}✓${NC} Added CLAUDE_CODE_TMPDIR to $PROFILE"
else
echo -e " ${GREEN}✓${NC} CLAUDE_CODE_TMPDIR already configured"
fi
# Export for current session
export CLAUDE_CODE_TMPDIR="$TERMUX_TMP"
# ============================================================
# Done!
# ============================================================
echo ""
echo -e "${GREEN}╔════════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ Setup Complete! ║${NC}"
echo -e "${GREEN}╚════════════════════════════════════════════════════════╝${NC}"
echo ""
echo -e "To start Claude Code, run:"
echo -e " ${BLUE}claude${NC}"
echo ""
echo -e "If this is a new terminal, first run:"
echo -e " ${BLUE}source ~/.bashrc${NC}"
echo ""
echo -e "${YELLOW}Note:${NC} After updating Claude Code (npm update -g @anthropic-ai/claude-code),"
echo "re-run this script to re-apply the patches."
echo ""
echo -e "To restore original Claude Code:"
echo -e " cp $CLAUDE_CLI.bak $CLAUDE_CLI"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment