claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

v2.1.113+ broken on Termux/Android: native binary requires glibc, no JS fallback

Open tpdhd opened this issue 7 months ago • 27 comments

Summary

Starting with v2.1.113, Claude Code switched from a JavaScript entry point (cli.js) to a native glibc Linux binary (bin/claude.exe). This completely breaks Claude Code on Termux (Android), where:

  • process.platform reports android, not linux
  • Android's kernel rejects ET_EXEC glibc ELF binaries (unexpected e_type: 2)
  • There is no glibc on Android/Termux (it uses Bionic libc)

v2.1.112 (last JS-based version) works perfectly.

Environment

  • Device: Pixel 9, Android 14 (non-rooted)
  • Termux: latest from F-Droid
  • Node.js: v22 (Termux arm64)
  • Claude Code: v2.1.113 / v2.1.114 broken, v2.1.112 works

Steps to reproduce

# On Termux
npm install -g @anthropic-ai/claude-code@latest
claude --version

Expected: Version output Actual:

Error: claude native binary not installed.

Either postinstall did not run (--ignore-scripts, some pnpm configs)
or the platform-native optional dependency was not downloaded
(--omit=optional).

Root cause

  1. install.cjs calls process.platform → returns android → not in PLATFORMS map → skips binary install
  2. Even force-installing @anthropic-ai/claude-code-linux-arm64 and copying the binary fails because Android's kernel rejects glibc executables:
    error: "bin/claude.exe" has unexpected e_type: 2
    
  3. Running via patchelf or the glibc ld-linux-aarch64.so.1 loader also fails (Could not find a PHDR: broken executable?)
  4. Running via proot-distro works (proot-distro login ubuntu -- /path/to/claude --version) but is too slow for interactive use

Workaround

Pin to the last JS-based version:

npm install -g @anthropic-ai/[email protected]

Suggested fix

One or more of:

  • Add android-arm64 platform support with a statically-linked or Bionic-compatible binary
  • Keep the JS fallback (cli.js / cli-wrapper.cjs) functional as a real fallback, not just an error message
  • Map android → linux in platform detection (won't fix the glibc issue alone, but would help if a compatible binary is provided)

Impact

All Termux users on Android are locked out of updates. Termux is one of the few ways to run Claude Code on mobile — a non-trivial use case for developers on the go.

tpdhd avatar Apr 18 '26 04:04 tpdhd

Please fix it, the whole idea of terminal agent is that I do not need IDE in most of the cases now, which means I do not really need my 4 screens , which means no need for a PC. Android termux will th 5 terminals running Claude and doing everything I need is almost all I need these days.

cyberb avatar Apr 18 '26 09:04 cyberb

+1, hitting the same regression on Samsung S918B (Galaxy S23 Ultra) running Termux native (not proot).

Environment

  • Device: Samsung SM-S918B
  • OS: Android 16, kernel 5.15.178-android13-8
  • Termux: native arm64 (no proot)
  • Node: v24.14.1
  • npm: 11.12.1
  • claude-code: 2.1.114 from npm install -g

Reproduction confirmed

process.platform on Termux Node returns "android", not "linux":

$ node -e "console.log(process.platform, require('os').arch())"
android arm64

install.cjs then falls through the if (platform === 'linux') branch, builds platform key "android-arm64", looks it up in PLATFORMS, gets undefined, and bails with the "Unsupported platform" message — leaving only the 155 KB wrapper stub. Every subsequent claude invocation prints:

Error: claude native binary not installed.
Either postinstall did not run (--ignore-scripts, some pnpm configs)
or the platform-native optional dependency was not downloaded
(--omit=optional).
...

Real-world impact

This broke an unattended cron job at 09:15 today: npm update -g --ignore-scripts ran at 08:32, pulled v2.1.114, and the next claude invocation crashed before any session could start. The script's mid-session resume logic was useless because the session never existed.

Workaround verified end-to-end

npm uninstall -g --ignore-scripts @anthropic-ai/claude-code
npm cache clean --force
npm install -g @anthropic-ai/[email protected]
claude --version  # → 2.1.112 (Claude Code)

Two requests for Anthropic

  1. Add an android-arm64 entry to the PLATFORMS map in install.cjs and cli-wrapper.cjs (Termux is one of the largest "Linux on phone" environments and this regression cuts it off entirely). Either ship a real Android-bionic native build, or fall back to the linux-arm64 package with a runtime check, or — at minimum — preserve the previous self-contained cli.js JS entry point as a fallback when no native binary matches.
  2. Until the above ships, please republish v2.1.112 as latest or otherwise prevent automatic updates from breaking every Termux user's unattended workflows. Right now anyone with npm update -g in their cron / Dockerfile / dotfiles bootstrap gets silently broken.

Happy to test patches against this device.

salviz avatar Apr 18 '26 15:04 salviz

Confirming on Termux / Android 13 / aarch64

  • Termux Node v25.3.0
  • npm i -g @anthropic-ai/[email protected] placed a 236MB native binary at $PREFIX/lib/node_modules/@anthropic-ai/claude-code/bin/claude.exe
  • file reports: ELF 64-bit LSB executable, ARM aarch64, …, interpreter /lib/ld-linux-aarch64.so.1, for GNU/Linux 3.7.0
  • ldd wants /lib/aarch64-linux-gnu/libc.so.6 etc. — none exist on Termux (bionic, Android linker at /system/bin/linker64). Binary refuses to exec.
  • package.json optionalDependencies only targets glibc/musl linux, darwin, win32 — no android-bionic entry.

Workaround: npm i -g @anthropic-ai/[email protected] (last version with "bin": { "claude": "cli.js" }) restores the pure-Node entry and works in Termux immediately.

Two requests:

  1. Until an android-arm64 / bionic target ships, could the wrapper fall back to cli-wrapper.cjs when no `PLATFORMS` key matches, instead of leaving a naked ELF that can't exec? The wrapper file is already shipped in the package but never invoked when the arch genuinely isn't listed.
  2. A documented "last-known-good" pin for Android users would unblock the downgrade path without archaeology.

daniel-thisnow avatar Apr 18 '26 16:04 daniel-thisnow

Follow-up: 2.1.114 has the same regression — and the in-process auto-updater is actively reverting manual pins.

Same Termux/Android 13/aarch64 device as my previous comment.

2.1.114 confirmed broken the same way as 2.1.113

  • npm i -g @anthropic-ai/[email protected] → bin/claude.exe is now just the 155-byte error stub (no android-arm64 in optionalDependencies, so postinstall bails). Previous cli.js is gone.
  • Every fresh claude invocation dies with "Error: claude native binary not installed."

New failure mode: the in-process auto-updater silently clobbers a manual downgrade

Inside a running claude session:

  1. npm i -g @anthropic-ai/[email protected] — confirmed "version": "2.1.112", cli.js restored, package.json timestamp fresh.
  2. A few seconds later, re-checked the same package.json: "version": "2.1.114", cli.js gone, directory mtime updated.

No other process was touching the tree — only the running claude. The built-in updater is on a timer and re-fetches latest, overwriting the pinned install while the user is mid-session. So for Termux users the current state is:

  • 2.1.113+ on-disk = broken.
  • Downgrading to 2.1.112 lasts only until the next updater tick, which can happen within the same session you downgraded from.

What actually survives on Termux right now

Belt-and-braces recipe that held:

DISABLE_AUTOUPDATER=1 npm i -g @anthropic-ai/[email protected]
chmod -R a-w \$PREFIX/lib/node_modules/@anthropic-ai/claude-code/
export DISABLE_AUTOUPDATER=1   # also in ~/.zshrc / ~/.bashrc

And in ~/.claude/settings.json:

"env": { "DISABLE_AUTOUPDATER": "1" }

The read-only chmod is load-bearing — without it, the updater's write silently wins.

Concrete asks, restated

  1. Make the updater honor DISABLE_AUTOUPDATER=1 and skip auto-update when the current install's platform key isn't in PLATFORMS (i.e., don't auto-"upgrade" a user into a broken state on platforms the release doesn't target).
  2. Restore cli-wrapper.cjs as an automatic fallback when no PLATFORMS entry matches the host. Shipping the file and never using it is a footgun.
  3. Until (1)+(2) land, please consider tagging 2.1.112 as npm dist-tag add @anthropic-ai/[email protected] termux (or similar) so the downgrade command is documentable and doesn't rot.

Happy to test any patch on the same device.

daniel-thisnow avatar Apr 18 '26 17:04 daniel-thisnow

For anyone landing here looking for a ready recovery path, I maintain claude-code-android specifically for this install target and shipped v2.7.0 last night with the pinned install plus the auto-updater defense.

@daniel-thisnow nailed the auto-updater issue above. Credit to him for the chmod finding. The v2.7.0 install script bundles his defense into an idempotent fresh-install / recover-in-place flow so users can go from the broken error to a working claude in one command.

Two install paths in the repo:

  • Path A (native Termux): pinned to 2.1.112, DISABLE_AUTOUPDATER=1 set in env and settings.json, install dir chmod a-w. Works today.
  • Path B (proot-distro Ubuntu): unaffected by this regression because process.platform reports linux inside the guest. Heavier setup, but immune to platform-native binary drift upstream.

Repo: https://github.com/ferrumclaudepilgrim/claude-code-android

I will keep it current as the upstream situation evolves.

ferrumclaudepilgrim avatar Apr 19 '26 07:04 ferrumclaudepilgrim

Confirming on Samsung A55, Termux native (no proot), aarch64

Same claude native binary not installed on npm i -g @anthropic-ai/claude-code@latest. My setup:

  • Samsung A55, Termux native (no proot-distro)
  • Node v24.14.1
  • process.platform returns android, arch arm64
  • npm root at /data/data/com.termux/files/usr/lib/node_modules

cliwrapper.cjs is in the package but there's no fallback. Running it directly just surfaces the same "unsupported platform" error from install.cjs. The file ships but never runs, which is the footgun the OP called out.

What's holding for me

Pinned to 2.1.112 (last version with a working cli.js) with three layers of lock against the autoupdater silently winning:

npm uninstall -g @anthropic-ai/claude-code
DISABLE_AUTOUPDATER=1 npm i -g @anthropic-ai/[email protected] --ignore-scripts
chmod -R a-w $PREFIX/lib/node_modules/@anthropic-ai/claude-code/

In ~/.bashrc:

export DISABLE_AUTOUPDATER=1
alias claude='node /data/data/com.termux/files/usr/lib/node_modules/@anthropic-ai/claude-code/cli.js'

In ~/.claude/settings.json:

{ "env": { "DISABLE_AUTOUPDATER": "1" } }

Without the read-only lock chmod -R a-w , the updater's write can still win against the env var in some paths and silently upgrade you back into the broken state.

Echoing the asks

  1. Honor DISABLE_AUTOUPDATER=1 everywhere, and skip auto-update when the current platform key isn't in PLATFORMS. Auto-upgrading users into a broken state on unsupported platforms is worse than not updating them.
  2. Restore cliwrapper.cjs as a real fallback when PLATFORMS has no match. The file is already in the package. Making it a trap instead of a safety net is reversible.
  3. Until 1 and 2 ship, an npm dist-tag like @anthropic-ai/claude-code@termux pointing at 2.1.112 would make the workaround documentable and stop the downgrade command from rotting.

Why this matters

Termux is how a lot of people run dev on Android. For me it's the phone side of an SSH-into my Debian workflow, with local Claude Code as the fallback when I'm offline or the server's not reachable or when i need to tweak Termux with Claude. Losing that breaks the "check on the agent loop from anywhere" use case, which is exactly what Claude Code agentic workflows are designed for.

Happy to test fixes on the android-arm64 path.

rafaelbressan avatar Apr 19 '26 12:04 rafaelbressan

Deep-dive + proposed minimal patches

Inspected the actual 2.1.114 npm tarball. The failure path is small and easy to fix even without shipping an Android binary — three compounding bugs each have a ~5-line fix:

Bug 1 — install.cjs silently no-ops on Android, leaves a misleading stub

install.cjs line 127-137 in 2.1.114:

function main() {
  const platformKey = getPlatformKey()
  const info = PLATFORMS[platformKey]

  if (!info) {
    console.error(
      `[${WRAPPER_NAME} postinstall] Unsupported platform: ${process.platform} ${arch()}`,
    )
    console.error(`  Supported: ${Object.keys(PLATFORMS).join(', ')}`)
    return   // ← silent success; exit code 0
  }
  ...
}

On Termux, process.platform === 'android', so getPlatformKey() returns 'android-arm64', PLATFORMS['android-arm64'] is undefined, and main() returns with exit code 0. npm reports a successful install. The stub bin/claude.exe (a 500-byte shell script) stays in place, and the user only discovers breakage at first claude invocation — with an error message that points them in the wrong direction (see Bug 2).

Bug 2 — the stub error message gives misleading advice on Termux

bin/claude.exe stub content:

echo "Error: claude native binary not installed." >&2
...
echo "Either postinstall did not run (--ignore-scripts, some pnpm configs)"
echo "or the platform-native optional dependency was not downloaded"
echo "(--omit=optional)."
...
echo "Run the postinstall manually:"
echo "  node node_modules/@anthropic-ai/claude-code/install.cjs"
echo "Or reinstall without --ignore-scripts / --omit=optional."

On Termux, none of this advice applies — postinstall ran, optional deps downloaded, the binary was just skipped because the platform isn't in PLATFORMS. Users follow the advice, reinstall, hit the same error, and conclude it's just broken.

Bug 3 — auto-updater re-breaks the pinned install

Even after npm install -g @anthropic-ai/[email protected] --force, something in the claude CLI auto-upgrades to @latest on next launch, silently re-bricking the install. Users hit the native-binary error a second time and don't understand why. The only reliable fix on Termux right now is also setting DISABLE_AUTOUPDATER=1 in settings/env — which isn't documented for this failure mode.

Proposed minimal patch (no Android binary required)

Just detecting Android in the two shared PLATFORMS/getPlatformKey() code blocks and failing loudly with correct advice would save every Termux user from this confusing debug loop. Proposed diff against install.cjs:

 function main() {
   const platformKey = getPlatformKey()
+
+  // Termux/Android: no compatible native binary exists (Bionic libc, not glibc).
+  // Fail loudly at install time with a working workaround so users don't hit the
+  // misleading "native binary not installed" stub at runtime.
+  if (process.platform === 'android') {
+    console.error(
+      `[${WRAPPER_NAME} postinstall] Android/Termux is not supported by the native binary (Bionic libc).`,
+    )
+    console.error(
+      '  Workaround: pin to the last JS-based release:',
+    )
+    console.error(
+      `    npm install -g ${WRAPPER_NAME}@2.1.112`,
+    )
+    console.error(
+      '  Also set DISABLE_AUTOUPDATER=1 in your environment to prevent silent',
+    )
+    console.error(
+      '  re-upgrade. Tracking issue: anthropics/claude-code#50270',
+    )
+    process.exitCode = 1
+    return
+  }
+
   const info = PLATFORMS[platformKey]
   ...
 }

Same block should be added to cli-wrapper.cjs inside getBinaryPath() for the --ignore-scripts path.

Broader fix (auto-updater)

The auto-updater inside the CLI should skip itself on process.platform === 'android' (or at minimum pin to @2.1.112 instead of @latest) until an Android binary is shipped. Right now it actively breaks working installs every few hours — arguably worse than the install bug itself.

Ideal fix

Ship @anthropic-ai/claude-code-android-arm64 built against Bionic (or statically linked). Termux has ~1M+ installs; this is a non-trivial userbase. Happy to test an alpha build on Pixel 9 / Android 14 if it'd help.


Environment: Termux on Android 14 (aarch64), Node 22, npm 10.

aryehbeitz avatar Apr 19 '26 13:04 aryehbeitz

how is this issue still open bruh

myudak avatar Apr 20 '26 01:04 myudak

Seems like we are being forced to use the app???

oespinozai avatar Apr 20 '26 10:04 oespinozai

@aryehbeitz is spot-on. It is bewildering for those of us who have been using Claude Code on Termux to suddenly find it bricked with the misleading stub error message after the updater silently broke it.

I am happy to report Codex (@mmmbuto/codex-cli-termux) continues to work on Termux.

Anthropic, get this fixed. Termux is popular and heavily used by those who need it. Even the old version on Google Play shows it has 10M+ downloads.

alex-rosenberg35 avatar Apr 21 '26 01:04 alex-rosenberg35

Confirming on 2.1.116 / Termux / Android (aarch64-linux-android), kernel 5.15.178-android13-8. Same regression — npm update bumped from 2.1.112 to 2.1.116, install.cjs rejected with Unsupported platform: android arm64, bin/claude.exe is the 500-byte stub, every claude invocation dies.

Additional data point on the musl fallback: I tried npm install -g @anthropic-ai/[email protected] --force to sidestep install.cjs entirely. Install succeeds, but the binary itself refuses to exec:

$ file .../claude-code-linux-arm64-musl/claude
ELF 64-bit LSB executable, ARM aarch64, ..., dynamically linked,
  interpreter /lib/ld-musl-aarch64.so.1, ...

$ .../claude
bash: .../claude: cannot execute: required file not found

The musl variant isn't a viable escape hatch on Termux either — it's dynamically linked against musl's loader, which Android's Bionic doesn't provide (unlike truly-static musl binaries like ruff/uv, which do work fine on Termux). Either a genuine `android-arm64` build or a statically-linked musl binary would unbreak this.

Rollback to 2.1.112 works as described above. +1 on restoring a `cli.js` / `cli-wrapper.cjs` JS fallback when no `PLATFORMS` key matches.

kingpanther13 avatar Apr 21 '26 12:04 kingpanther13

While waiting for a fix:

npm uninstall -g @anthropic-ai/claude-code
npm install -g @anthropic-ai/[email protected]

Then update ~/.claude/settings.json with:

{
  "autoUpdates": false
}

sb-dev avatar Apr 22 '26 11:04 sb-dev

Claude Code 2.1.113 has switched to a native executable (after npm deprecation was announced in January), which drops support for some platforms. We may add support for Android in the future - this requires either a bun target for android-arm64 or static musl.

You can pin to the last JS bundle release https://www.npmjs.com/package/@anthropic-ai/claude-code/v/2.1.112 to continue using Claude Code for now.

ant-kurt avatar Apr 23 '26 23:04 ant-kurt

+1 from a Haiku OS user. Same root cause as the Termux reports: process.platform returns haiku, which isn't in install.cjs's PLATFORMS map, so the postinstall skips the native binary download entirely and the npm wrapper fails at launch with "claude native binary not installed."

I've been running Claude Code happily on Haiku for months — it worked because the pre-2.1.113 CLI was plain Node.js and didn't care what platform it ran on. The switch to a platform-native binary wasn't aimed at platforms like mine, but it hard-fails on them rather than degrading gracefully.

I'm currently pinned to 2.1.112, which means I'm missing the fixes from the April 23 post-mortem (notably the verbosity system prompt reverted in 2.1.116). Staying pinned isn't sustainable long-term.

The ask here isn't "officially support Haiku" — I understand that's a real engineering commitment. The ask is much smaller: restore a JS fallback for platforms where no prebuilt binary exists. When process.platform isn't in the PLATFORMS map, fall back to the bundled cli.js path that worked fine through 2.1.112, possibly behind a flag or with a warning that the platform is unsupported. This would unblock Haiku, Termux/Android, FreeBSD, and any future platform without requiring Anthropic to maintain binaries for them.

Happy to test patches on Haiku if that would help.

ilfelice avatar Apr 24 '26 03:04 ilfelice

Workaround refinement not yet in this thread: npm install -g @anthropic-ai/claude-code@stable works identically to @2.1.112 today, and is more durable than version-pinning. As of 2026-04-25, the stable dist-tag still points to 2.1.112:

$ npm view @anthropic-ai/claude-code dist-tags
{ stable: '2.1.112', next: '2.1.119', latest: '2.1.119' }

Anthropic kept the last pure-JS release as @stable rather than promoting any native-binary version. Pinning to @stable is preferable to @2.1.112 because:

  • If 2.1.112 ever gets unpublished, @stable keeps working
  • If a 2.1.112.x patch backport ships, @stable picks it up automatically
  • It documents intent ("last known good") rather than a magic version

Combine with "autoUpdates": false in ~/.claude/settings.json (per @sb-dev) to defend against the in-session auto-updater silently re-bricking the install, and this is a one-shot, persistent fix:

# Wipe leftover native-binary opt-deps from prior failed installs (240MB+ each)
NM=$(npm root -g)
for V in linux-arm64 linux-arm64-musl linux-x64 linux-x64-musl darwin-arm64 darwin-x64 win32-arm64 win32-x64; do
  rm -rf "${NM}/@anthropic-ai/claude-code-${V}"
done

npm install -g @anthropic-ai/claude-code@stable

# Lock auto-updates without clobbering existing settings.
jq '. + {autoUpdates: false}' ~/.claude/settings.json > /tmp/s.json && mv /tmp/s.json ~/.claude/settings.json

Confirmed working 2026-04-25 on Samsung S26 Ultra (Android 16, kernel 6.12.30-android16, Termux 0.118.3, Node 24.14.1, npm 11.13.0) — adding to the device-confirmation list since Android 16 / S26 hardware hasn't been reported yet in this thread.

ScientificProgrammer avatar Apr 26 '26 02:04 ScientificProgrammer

Same issue on OpenBSD x64. The native binary is a glibc Linux ELF that can't run on OpenBSD (no Linux emulation layer in OpenBSD 7.x). I'm currently pinning to 2.1.109

The fix request is the same: restore a JS fallback path for platforms not supporting a native binary. I'm fine to eat the performance cost for more cross-platform support. I understand you can't ship a binary for everything.

cowile avatar May 01 '26 18:05 cowile

DeepseekPro4 is another beast. I've switched.

On Fri, May 1, 2026 at 7:11 PM cowile @.***> wrote:

cowile left a comment (anthropics/claude-code#50270) https://github.com/anthropics/claude-code/issues/50270#issuecomment-4360850037

Same issue on OpenBSD x64. The native binary is a glibc Linux ELF that can't run on OpenBSD (no Linux emulation layer in OpenBSD 7.x). I'm currently pinning to 2.1.109

The fix request is the same: restore a JS fallback path for platforms not supporting a native binary. I'm fine to eat the performance cost for more cross-platform support. I understand you can't ship a binary for everything.

— Reply to this email directly, view it on GitHub https://github.com/anthropics/claude-code/issues/50270#issuecomment-4360850037, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOEA43W4IZOBJUVUDTKGDT4YTSD7AVCNFSM6AAAAACX5VXAI6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DGNRQHA2TAMBTG4 . You are receiving this because you commented.Message ID: @.***>

-- Oscar Espinoza oscarespinoza.co.uk http://oscarespinoza.co.uk

oespinozai avatar May 01 '26 20:05 oespinozai

+1

nick-the-dev avatar May 03 '26 16:05 nick-the-dev

I hope this gets support.

noezra avatar May 05 '26 14:05 noezra

Run the current native binary on Termux (alternative to pinning to 2.1.112)

Patchelf the linux-arm64 binary's ELF interpreter to glibc-runner's ld.so, exec it directly with LD_PRELOAD cleared so termux-exec doesn't crash it on startup, then re-apply LD_PRELOAD via claude's settings env so subprocess shebang resolution still works.

Updated 2026-06-01 to keep LD_PRELOAD (so #!/usr/bin/env shebangs in pnpm/npx and your own scripts keep resolving) and fix the bundled grep/find/rg that the re-injected LD_PRELOAD otherwise breaks — via a length-preserving binary patch plus a compiled launcher wrapper. This avoids the lossy alternative of dropping LD_PRELOAD entirely, which silently breaks every #!/usr/bin/env-shebang tool.

One-time setup:

# glibc-runner ships a working glibc ld.so at $PREFIX/glibc/lib/;
# patchelf-glibc rewrites ELF interpreter strings. Both come from the
# glibc-packages termux repo, which the glibc-repo package enables.
# jq parses the upstream release manifest for checksum verification.
# clang compiles the launcher wrapper (must be a real ELF, not a script).
pkg install -y clang jq glibc-repo
pkg update
pkg install -y glibc-runner patchelf-glibc

# Put the wrapper directory on PATH if it isn't already.
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Install / re-run on each version bump (set VERSION to the desired release):

#!/data/data/com.termux/files/usr/bin/bash
set -euo pipefail

VERSION=2.1.156
BINARY=~/.local/share/claude-code/claude
WRAPPER=~/.local/bin/claude
DL=https://downloads.claude.ai/claude-code-releases/$VERSION
GLIBC_PREFIX=/data/data/com.termux/files/usr/glibc
PATCHELF=$GLIBC_PREFIX/bin/patchelf
GLIBC_LD=$GLIBC_PREFIX/lib/ld-linux-aarch64.so.1

mkdir -p "$(dirname "$BINARY")" "$(dirname "$WRAPPER")"

# Patch a tempfile, then rename in. Renaming over the running binary is
# safe; patching it in place would fail with "Text file busy".
tmp=$(mktemp); patched=$tmp.patched
trap 'rm -f "$tmp" "$patched"' EXIT
curl -fsSL "$DL/linux-arm64/claude" -o "$tmp"

# Verify against the published manifest checksum.
expected=$(curl -fsSL "$DL/manifest.json" | jq -er '.platforms["linux-arm64"].checksum')
actual=$(sha256sum "$tmp" | cut -d' ' -f1)
if [ "$actual" != "$expected" ]; then
  echo "checksum mismatch: $actual != $expected" >&2
  exit 1
fi

# Set the ELF interpreter to glibc-runner's ld.so so the kernel can exec
# the binary directly — needed because Claude's bundled grep/find/rg
# re-exec it with argv[0]=ugrep/bfs/rg, and argv[0] only survives a
# kernel-direct exec (`grun` chains via ld.so and breaks it). LD_PRELOAD=
# stops termux-exec from crashing patchelf: its DT_NEEDED `libc.so` is a
# linker script under $PREFIX/glibc/lib, not an ELF.
LD_PRELOAD= "$PATCHELF" --output "$patched" --set-interpreter "$GLIBC_LD" "$tmp"

# Blank the subprocess CLAUDE_CODE_EXECPATH. The grep/find/rg functions
# prefer it for their re-exec; with LD_PRELOAD re-injected (below) that
# re-exec inherits the bionic preload and crashes the glibc binary.
# Emptying it makes them fall back to the wrapper, which clears
# LD_PRELOAD. Replacing `process.execPath` with `""` plus trailing spaces
# keeps the file length fixed; backreferences keep the anchor unique
# across Bun's per-release identifier churn.
python3 - "$patched" <<'PY'
import sys, re
p = sys.argv[1]
data = open(p, "rb").read()
ident = rb"[A-Za-z_$][A-Za-z0-9_$]*"
anchor = re.compile(rb"((" + ident + rb")\[" + ident + rb"\]=)process\.execPath(,(" + ident + rb")\)\2\.TMUX=\4)")
matches = anchor.findall(data)
if len(matches) != 1:
    sys.exit(f"execPath patch: expected 1 match, got {len(matches)} (Bun output changed?)")
replacement = b'""' + b" " * 14   # 16 bytes, same width as "process.execPath"
open(p, "wb").write(anchor.sub(lambda m: m.group(1) + replacement + m.group(3), data))
PY

chmod +x "$patched"
mv "$patched" "$BINARY"

# Compile the launcher wrapper. It must be a real ELF, not a #! script:
# the grep re-exec falls back to it, and a script wrapper would let the
# kernel discard argv[0]=ugrep, whereas execv() preserves it. Being a
# bionic binary, termux-exec loads into it fine; it then clears
# LD_PRELOAD before exec'ing the glibc claude binary so it starts clean.
cc -O2 -DBINARY="\"$BINARY\"" -o "$WRAPPER" -xc - <<'EOF'
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char **argv) {
  (void)unsetenv("LD_PRELOAD");
  execv(BINARY, argv);
  fprintf(stderr, "claude wrapper: execv %s failed: %s\n", BINARY, strerror(errno));
  return 127;
}
EOF

Add to ~/.claude/settings.json:

{
  "autoUpdates": false,
  "env": {
    "LD_PRELOAD": "/data/data/com.termux/files/usr/lib/libtermux-exec-ld-preload.so"
  }
}

autoUpdates: false stops the in-session updater from re-downloading the broken bundle. The LD_PRELOAD entry is applied after the dynamic linker finishes with claude (so it doesn't crash the main process), but is in process.env when claude spawns subprocesses, so #!/usr/bin/env foo shebangs in tools like pnpm and npx resolve correctly inside the Bash tool.

Chezmoi-managed version with Renovate-pinned releases: gtbuchanan/dotfiles.

gtbuchanan avatar May 16 '26 15:05 gtbuchanan