-
Notifications
You must be signed in to change notification settings - Fork 2
Replace npm Claude Code with native binary installation #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # Claude Code CLI (Native Binary) | ||
|
|
||
| Installs [Claude Code](https://docs.anthropic.com/en/docs/claude-code) as a native binary using Anthropic's official installer. | ||
|
|
||
| Unlike the npm-based installation (`ghcr.io/anthropics/devcontainer-features/claude-code`), this feature installs the native binary directly to `~/.local/bin/claude`. The binary is owned by the container user, so the in-session auto-updater works without permission issues. | ||
|
|
||
| ## Options | ||
|
|
||
| | Option | Default | Description | | ||
| |--------|---------|-------------| | ||
| | `version` | `latest` | `latest`, `stable`, or a specific semver (e.g., `2.1.52`). Set to `none` to skip. | | ||
| | `username` | `automatic` | Container user to install for. `automatic` detects from `$_REMOTE_USER`. | | ||
|
|
||
| ## How it works | ||
|
|
||
| 1. Downloads the official installer from `https://claude.ai/install.sh` | ||
| 2. Runs it as the target user (not root) | ||
| 3. The installer handles platform detection, checksum verification, and binary placement | ||
| 4. Binary is installed to `~/.local/bin/claude` with versions stored in `~/.local/share/claude/versions/` | ||
|
|
||
| ## Usage | ||
|
|
||
| ```json | ||
| { | ||
| "features": { | ||
| "./features/claude-code-native": {} | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| With version pinning: | ||
|
|
||
| ```json | ||
| { | ||
| "features": { | ||
| "./features/claude-code-native": { | ||
| "version": "2.1.52" | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Why native over npm? | ||
|
|
||
| The npm installation (`npm install -g @anthropic-ai/claude-code`) runs as root during the Docker build, creating a package owned by `root`. When the container user tries to auto-update Claude Code in-session, it fails with `EACCES` because it can't write to the root-owned package directory. | ||
|
|
||
| The native binary installs to `~/.local/` under the container user's ownership, so `claude update` works without elevated permissions. |
29 changes: 29 additions & 0 deletions
29
.devcontainer/features/claude-code-native/devcontainer-feature.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| { | ||
| "id": "claude-code-native", | ||
| "version": "1.0.0", | ||
| "name": "Claude Code CLI (Native Binary)", | ||
| "description": "Installs Claude Code CLI as a native binary via the official Anthropic installer", | ||
| "documentationURL": "https://docs.anthropic.com/en/docs/claude-code", | ||
| "options": { | ||
| "version": { | ||
| "type": "string", | ||
| "description": "Version to install: 'latest', 'stable', or a specific semver. Use 'none' to skip.", | ||
| "default": "latest" | ||
| }, | ||
| "username": { | ||
| "type": "string", | ||
| "description": "Container user to install for", | ||
| "default": "automatic" | ||
| } | ||
| }, | ||
| "customizations": { | ||
| "vscode": { | ||
| "extensions": [ | ||
| "anthropic.claude-code" | ||
| ] | ||
| } | ||
| }, | ||
| "installsAfter": [ | ||
| "ghcr.io/devcontainers/features/common-utils:2" | ||
| ] | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| #!/bin/bash | ||
| set -euo pipefail | ||
|
|
||
| VERSION="${VERSION:-latest}" | ||
| USERNAME="${USERNAME:-automatic}" | ||
|
|
||
| # Skip installation if version is "none" | ||
| if [ "${VERSION}" = "none" ]; then | ||
| echo "[claude-code-native] Skipping installation (version=none)" | ||
| exit 0 | ||
| fi | ||
|
|
||
| echo "[claude-code-native] Starting installation..." | ||
| echo "[claude-code-native] Version: ${VERSION}" | ||
|
|
||
| # === VALIDATE DEPENDENCIES === | ||
| # The official installer (claude.ai/install.sh) requires curl internally | ||
| if ! command -v curl >/dev/null 2>&1; then | ||
| echo "[claude-code-native] ERROR: curl is required" | ||
| echo " Ensure common-utils feature is installed first" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # === DETECT USER === | ||
| if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then | ||
| if [ -n "${_REMOTE_USER:-}" ]; then | ||
| USERNAME="${_REMOTE_USER}" | ||
| elif getent passwd vscode >/dev/null 2>&1; then | ||
| USERNAME="vscode" | ||
| elif getent passwd node >/dev/null 2>&1; then | ||
| USERNAME="node" | ||
| elif getent passwd codespace >/dev/null 2>&1; then | ||
| USERNAME="codespace" | ||
| else | ||
| USERNAME="root" | ||
| fi | ||
| fi | ||
|
|
||
| USER_HOME=$(getent passwd "${USERNAME}" | cut -d: -f6) | ||
| if [ -z "${USER_HOME}" ]; then | ||
| echo "[claude-code-native] ERROR: Could not determine home directory for ${USERNAME}" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "[claude-code-native] Installing for user: ${USERNAME} (home: ${USER_HOME})" | ||
|
|
||
| # === PREPARE DIRECTORIES === | ||
| mkdir -p "${USER_HOME}/.local/bin" | ||
| mkdir -p "${USER_HOME}/.local/share/claude" | ||
| mkdir -p "${USER_HOME}/.local/state" | ||
| mkdir -p "${USER_HOME}/.claude" | ||
| chown -R "${USERNAME}:" "${USER_HOME}/.local" "${USER_HOME}/.claude" | ||
|
|
||
| # === DETERMINE TARGET === | ||
| # The official installer accepts: stable, latest, or a specific semver | ||
| TARGET="${VERSION}" | ||
| if [ "${TARGET}" != "latest" ] && [ "${TARGET}" != "stable" ]; then | ||
| if ! echo "${TARGET}" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+$'; then | ||
| echo "[claude-code-native] ERROR: Invalid version '${TARGET}'" | ||
| echo " Use 'latest', 'stable', or a semver (e.g., 2.1.52)" | ||
| exit 1 | ||
| fi | ||
| fi | ||
|
|
||
| # === INSTALL === | ||
| # The official Anthropic installer handles: | ||
| # - Platform detection (linux/darwin, x64/arm64, glibc/musl) | ||
| # - Manifest download and checksum verification | ||
| # - Binary download to ~/.local/bin/claude (symlink to ~/.local/share/claude/versions/*) | ||
| echo "[claude-code-native] Downloading official installer..." | ||
|
|
||
| if [ "${USERNAME}" = "root" ]; then | ||
| curl -fsSL https://claude.ai/install.sh | bash -s -- "${TARGET}" | ||
| else | ||
| su - "${USERNAME}" -c "curl -fsSL https://claude.ai/install.sh | bash -s -- \"${TARGET}\"" | ||
| fi | ||
|
|
||
| # === VERIFICATION === | ||
| CLAUDE_BIN="${USER_HOME}/.local/bin/claude" | ||
|
|
||
| if [ -x "${CLAUDE_BIN}" ]; then | ||
| INSTALLED_VERSION=$(su - "${USERNAME}" -c "${CLAUDE_BIN} --version 2>/dev/null" || echo "unknown") | ||
| echo "[claude-code-native] ✓ Claude Code installed: ${INSTALLED_VERSION}" | ||
| echo "[claude-code-native] Binary: ${CLAUDE_BIN}" | ||
| else | ||
| echo "[claude-code-native] ERROR: Installation failed — ${CLAUDE_BIN} not found or not executable" | ||
| echo "[claude-code-native] Expected binary at: ${CLAUDE_BIN}" | ||
| ls -la "${USER_HOME}/.local/bin/" 2>/dev/null || true | ||
| exit 1 | ||
| fi | ||
|
|
||
| # === POST-START HOOK === | ||
| # Ensures hasCompletedOnboarding is set when token auth is configured. | ||
| # Runs as the LAST post-start hook (99- prefix) to catch overwrites from | ||
| # Claude Code CLI/extension that may race with postStartCommand. | ||
| HOOK_DIR="/usr/local/devcontainer-poststart.d" | ||
| mkdir -p "$HOOK_DIR" | ||
| cat > "$HOOK_DIR/99-claude-onboarding.sh" << 'HOOK_EOF' | ||
| #!/bin/bash | ||
| # Ensure hasCompletedOnboarding: true in .claude.json when token auth exists. | ||
| # Runs after all setup scripts to catch any overwrites by Claude Code CLI/extension. | ||
| _USERNAME="${SUDO_USER:-${USER:-vscode}}" | ||
| _USER_HOME=$(getent passwd "$_USERNAME" 2>/dev/null | cut -d: -f6) | ||
| _USER_HOME="${_USER_HOME:-/home/$_USERNAME}" | ||
| CLAUDE_DIR="${CLAUDE_CONFIG_DIR:-${_USER_HOME}/.claude}" | ||
| CLAUDE_JSON="$CLAUDE_DIR/.claude.json" | ||
| CRED_FILE="$CLAUDE_DIR/.credentials.json" | ||
|
|
||
| # Only act when token auth is configured | ||
| [ -f "$CRED_FILE" ] || exit 0 | ||
|
|
||
| if [ -f "$CLAUDE_JSON" ]; then | ||
| if ! grep -q '"hasCompletedOnboarding"' "$CLAUDE_JSON" 2>/dev/null; then | ||
| if command -v jq >/dev/null 2>&1; then | ||
| jq '. + {"hasCompletedOnboarding": true}' "$CLAUDE_JSON" > "${CLAUDE_JSON}.tmp" && \ | ||
| mv "${CLAUDE_JSON}.tmp" "$CLAUDE_JSON" | ||
| else | ||
| sed -i '$ s/}$/,\n "hasCompletedOnboarding": true\n}/' "$CLAUDE_JSON" | ||
| fi | ||
| echo "[claude-onboarding] Injected hasCompletedOnboarding into .claude.json" | ||
| fi | ||
| else | ||
| printf '{\n "hasCompletedOnboarding": true\n}\n' > "$CLAUDE_JSON" | ||
| echo "[claude-onboarding] Created .claude.json with hasCompletedOnboarding" | ||
| fi | ||
| HOOK_EOF | ||
| chmod +x "$HOOK_DIR/99-claude-onboarding.sh" | ||
|
|
||
| echo "[claude-code-native] Installation complete" | ||
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
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
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
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure onboarding is forced to
true, not just present.Current logic only patches when the key is missing. If
.claude.jsonalready has"hasCompletedOnboarding": false, it will remain false.🔧 Suggested fix
🤖 Prompt for AI Agents