Claude Code Setup

環境構築
claude code
Author

Ryo Nakagami

Published

2026-03-31

Modified

2026-05-07

UbuntuにClaude Codeをインストール

$ curl -fsSL https://claude.ai/install.sh | bash
Setting up Claude Code...

 Claude Code successfully installed!        

  Version: 2.1.132

  Location: ~/.local/bin/claude


  Next: Run claude --help to get started

 Installation complete!

上記のコマンドの挙動は

  1. https://claude.ai/install.sh からインストールスクリプトをダウンロード
  2. ダウンロードしたスクリプトをBash経由で実行

curl コマンド

curl はURLからデータを取得するコマンドで,ここでは install.sh というシェルスクリプトをダウンロードしています. オプションの意味は次の通り

オプション 挙動
-f (fail): HTTPエラー (404など) のとき、エラー扱いで終了する
-s (silent): 進捗バーやエラーメッセージを非表示にする
-S (show-error): -s を使っていてもエラー自体は表示する
-L (location): リダイレクトがあれば自動で追跡する

この4つを合わせた -fsSL は「サイレントに動くが,エラーはちゃんと出して,リダイレクトも追う」という,インストールスクリプトの定番オプション

install.sh の解説

install.shの中身を確認したい場合は

$ curl -fsSL https://claude.ai/install.sh | cat

$ curl -fsSL https://claude.ai/install.sh | less

のいずれかを実行すれば確認できます. 以下では重要な箇所のみ解説します

スクリプト内での jq の役割

# Check if jq is available (optional)
HAS_JQ=false
if command -v jq >/dev/null 2>&1; then
    HAS_JQ=true
fi

上記のように jq コマンドの有無を確認するラインがあります.

if [ "$HAS_JQ" = true ]; then
    checksum=$(echo "$manifest_json" | jq -r ".platforms[\"$platform\"].checksum // empty")
else
    checksum=$(get_checksum_from_manifest "$manifest_json" "$platform")
fi

ダウンロードした manifest.json から,自分のプラットフォームに対応するチェックサム (SHA256ハッシュ) を取り出すために使用されています. なお,jq は必須ではなく, get_checksum_from_manifest()jq なしのフォールバックとして定義されています.

Noteチェックサムの意義

インターネット経由でファイルをダウンロードして実行する場合,次のリスクが伴います.

  • 通信エラーで壊れたファイル: パケットロスや TLS 終端のミスで途中までしか落ちてこない / バイトが欠ける
  • 中間者攻撃 (MITM) で改竄されたファイル: 経路上の攻撃者がバイナリを差し替える
  • 配信側の事故: CDN キャッシュの取り違え,誤ったビルド成果物のアップロード,リリースパイプラインのバグ

manifest.json に書かれた SHA-256 ハッシュと,実際にダウンロードしたバイナリから計算したハッシュを比較することで, 配信元が意図したバイナリと 1 ビットも違わないことを受信側で検証できます.

  • ハッシュ関数は衝突困難性を持つため,異なる中身のバイナリが偶然同じ SHA-256 を持つ確率は無視できる
  • 1 バイトでも変わればハッシュが完全に変わるので,転送中の破損も改竄も同じ手段で検出できる
Warningチェックサム照合では解決しないこと
  • マニフェスト自体の改竄: manifest.json も同じサーバから取得するため,攻撃者がサーバを掌握していればマニフェストとバイナリを揃えて差し替えられる.根本的に防ぐには GPG 署名Sigstore など配信元の鍵で署名された別系統の検証が必要

Rosetta 2 検出

if [ "$os" = "darwin" ] && [ "$arch" = "x64" ]; then
    if [ "$(sysctl -n sysctl.proc_translated 2>/dev/null)" = "1" ]; then
        arch="arm64"
    fi
fi

Apple Silicon Mac で x64 にエミュレートされた bash で実行された場合,uname -mx86_64 を返してしまいます. このとき sysctl.proc_translated1 なら現在のシェルは Rosetta 2 管理下で動いていると判断し,ネイティブの arm64 バイナリを取りに行くよう補正しています

Appendix: install.sh ファイル全体

#!/bin/bash

set -e

# Parse command line arguments
TARGET="$1"  # Optional target parameter

# Validate target if provided
if [[ -n "$TARGET" ]] && [[ ! "$TARGET" =~ ^(stable|latest|[0-9]+\.[0-9]+\.[0-9]+(-[^[:space:]]+)?)$ ]]; then
    echo "Usage: $0 [stable|latest|VERSION]" >&2
    exit 1
fi

DOWNLOAD_BASE_URL="https://downloads.claude.ai/claude-code-releases"
DOWNLOAD_DIR="$HOME/.claude/downloads"

# Check for required dependencies
DOWNLOADER=""
if command -v curl >/dev/null 2>&1; then
    DOWNLOADER="curl"
elif command -v wget >/dev/null 2>&1; then
    DOWNLOADER="wget"
else
    echo "Either curl or wget is required but neither is installed" >&2
    exit 1
fi

# Check if jq is available (optional)
HAS_JQ=false
if command -v jq >/dev/null 2>&1; then
    HAS_JQ=true
fi

# Download function that works with both curl and wget
download_file() {
    local url="$1"
    local output="$2"
    
    if [ "$DOWNLOADER" = "curl" ]; then
        if [ -n "$output" ]; then
            curl -fsSL -o "$output" "$url"
        else
            curl -fsSL "$url"
        fi
    elif [ "$DOWNLOADER" = "wget" ]; then
        if [ -n "$output" ]; then
            wget -q -O "$output" "$url"
        else
            wget -q -O - "$url"
        fi
    else
        return 1
    fi
}

# Simple JSON parser for extracting checksum when jq is not available
get_checksum_from_manifest() {
    local json="$1"
    local platform="$2"
    
    # Normalize JSON to single line and extract checksum
    json=$(echo "$json" | tr -d '\n\r\t' | sed 's/ \+/ /g')
    
    # Extract checksum for platform using bash regex
    if [[ $json =~ \"$platform\"[^}]*\"checksum\"[[:space:]]*:[[:space:]]*\"([a-f0-9]{64})\" ]]; then
        echo "${BASH_REMATCH[1]}"
        return 0
    fi
    
    return 1
}

# Detect platform
case "$(uname -s)" in
    Darwin) os="darwin" ;;
    Linux) os="linux" ;;
    MINGW*|MSYS*|CYGWIN*) echo "Windows is not supported by this script. See https://code.claude.com/docs for installation options." >&2; exit 1 ;;
    *) echo "Unsupported operating system: $(uname -s). See https://code.claude.com/docs for supported platforms." >&2; exit 1 ;;
esac

case "$(uname -m)" in
    x86_64|amd64) arch="x64" ;;
    arm64|aarch64) arch="arm64" ;;
    *) echo "Unsupported architecture: $(uname -m)" >&2; exit 1 ;;
esac

# Detect Rosetta 2 on macOS: if the shell is running as x64 under Rosetta on an ARM Mac,
# download the native arm64 binary instead of the x64 one
if [ "$os" = "darwin" ] && [ "$arch" = "x64" ]; then
    if [ "$(sysctl -n sysctl.proc_translated 2>/dev/null)" = "1" ]; then
        arch="arm64"
    fi
fi

# Check for musl on Linux and adjust platform accordingly
if [ "$os" = "linux" ]; then
    if [ -f /lib/libc.musl-x86_64.so.1 ] || [ -f /lib/libc.musl-aarch64.so.1 ] || ldd /bin/ls 2>&1 | grep -q musl; then
        platform="linux-${arch}-musl"
    else
        platform="linux-${arch}"
    fi
else
    platform="${os}-${arch}"
fi
mkdir -p "$DOWNLOAD_DIR"

# Always download latest version (which has the most up-to-date installer)
version=$(download_file "$DOWNLOAD_BASE_URL/latest")

# Download manifest and extract checksum
manifest_json=$(download_file "$DOWNLOAD_BASE_URL/$version/manifest.json")

# Use jq if available, otherwise fall back to pure bash parsing
if [ "$HAS_JQ" = true ]; then
    checksum=$(echo "$manifest_json" | jq -r ".platforms[\"$platform\"].checksum // empty")
else
    checksum=$(get_checksum_from_manifest "$manifest_json" "$platform")
fi

# Validate checksum format (SHA256 = 64 hex characters)
if [ -z "$checksum" ] || [[ ! "$checksum" =~ ^[a-f0-9]{64}$ ]]; then
    echo "Platform $platform not found in manifest" >&2
    exit 1
fi

# Download and verify
binary_path="$DOWNLOAD_DIR/claude-$version-$platform"
if ! download_file "$DOWNLOAD_BASE_URL/$version/$platform/claude" "$binary_path"; then
    echo "Download failed" >&2
    rm -f "$binary_path"
    exit 1
fi

# Pick the right checksum tool
if [ "$os" = "darwin" ]; then
    actual=$(shasum -a 256 "$binary_path" | cut -d' ' -f1)
else
    actual=$(sha256sum "$binary_path" | cut -d' ' -f1)
fi

if [ "$actual" != "$checksum" ]; then
    echo "Checksum verification failed" >&2
    rm -f "$binary_path"
    exit 1
fi

chmod +x "$binary_path"

# Run claude install to set up launcher and shell integration
echo "Setting up Claude Code..."
"$binary_path" install ${TARGET:+"$TARGET"}

# Clean up downloaded file
rm -f "$binary_path"

echo ""
echo "✅ Installation complete!"
echo ""