Skip to content

Performance: Enable 5-10x Faster Grep with System Ripgrep in Claude Code #72

Description

@terrylica

Summary

Claude Code's "Grep" tool is actually powered by ripgrep (rg), not GNU grep. By default, it uses a bundled version via @vscode/ripgrep npm package, which incurs Node.js wrapper overhead. Configuring USE_BUILTIN_RIPGREP=0 enables the system ripgrep for 5-10x faster searches in large codebases.

The Misconception

Many users assume Claude Code's "Grep" tool uses GNU grep because of its name. This is incorrect:

Aspect Reality
Tool Name "Grep" (UX-friendly branding)
Actual Backend ripgrep (rg)
Package @vscode/ripgrep npm module
Evidence Tool description says "A powerful search tool built on ripgrep"

Why System Ripgrep is Faster

Invocation Path Comparison

┌─────────────────────────────────────────────────────────────────┐
│                    BUNDLED RIPGREP PATH                         │
├─────────────────────────────────────────────────────────────────┤
│  Claude Code Binary                                             │
│       │                                                         │
│       ▼                                                         │
│  Node.js Runtime Layer                                          │
│       │                                                         │
│       ▼                                                         │
│  @vscode/ripgrep Native Module (ripgrep.node)                   │
│       │                                                         │
│       ▼                                                         │
│  Embedded rg binary extraction + execution                      │
│       │                                                         │
│       ▼                                                         │
│  Results back through Node.js layer                             │
└─────────────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────────────┐
│                    SYSTEM RIPGREP PATH                          │
├─────────────────────────────────────────────────────────────────┤
│  Claude Code Binary                                             │
│       │                                                         │
│       ▼                                                         │
│  Direct spawn: /usr/local/bin/rg (or /opt/homebrew/bin/rg)      │
│       │                                                         │
│       ▼                                                         │
│  Results (stdout)                                               │
└─────────────────────────────────────────────────────────────────┘

Performance Numbers

Scenario Bundled System Improvement
Large codebase search Baseline 5-10x faster Significant
Linux kernel grep ~0.67s ~0.06s ~11x faster
With line numbers ~9.48s ~1.66s ~6x faster

Configuration

Step 1: Install System Ripgrep

# macOS (Homebrew)
brew install ripgrep

# Ubuntu/Debian
sudo apt install ripgrep

# Arch Linux
pacman -S ripgrep

# Windows
winget install BurntSushi.ripgrep.MSVC

# Alpine Linux
apk add ripgrep

Step 2: Configure Shell

Add to your shell configuration file with detailed comments:

For Zsh (~/.zshrc):

# ========================================
# Claude Code CLI Configuration
# ========================================
# USE_BUILTIN_RIPGREP: Controls which ripgrep binary Claude Code uses
#
# Background:
#   - Claude Code's "Grep" tool is actually powered by ripgrep (rg), not GNU grep
#   - By default, Claude Code uses a BUNDLED ripgrep via @vscode/ripgrep npm package
#   - The bundled version goes through a Node.js wrapper layer, adding overhead
#
# Performance Impact:
#   - System ripgrep: Direct binary execution (faster)
#   - Bundled ripgrep: Node.js wrapper → native module → execution (5-10x slower)
#   - In large codebases, this overhead becomes significant
#
# Setting:
#   - USE_BUILTIN_RIPGREP=0 → Use system ripgrep (recommended for performance)
#   - USE_BUILTIN_RIPGREP=1 → Use bundled ripgrep (default)
#
# Known Issue: GitHub Issue #6415 reports this setting is sometimes ignored
# Verify with: ps aux | grep rg (while Claude Code is searching)
#
# Reference: https://nikiforovall.blog/claude-code-rules/tips-and-tricks/install-ripgrep/
export USE_BUILTIN_RIPGREP=0

For Bash (~/.bashrc):

Same configuration block as above.

Step 3: Apply Configuration

# Option 1: Restart shell
exec zsh  # or exec bash

# Option 2: Source config
source ~/.zshrc  # or source ~/.bashrc

# Option 3: Open new terminal

Step 4: Verify

# Check environment variable
echo $USE_BUILTIN_RIPGREP
# Expected: 0

# Check system ripgrep
which rg
rg --version

# Expected output (example):
# ripgrep 15.1.0
# features:+pcre2
# simd(compile):+NEON
# simd(runtime):+NEON

Platform-Specific Considerations

Platform Bundled PCRE2 System PCRE2 Notes
macOS ARM64 Full feature parity
macOS x64 Full feature parity
Linux x64 Full feature parity
Linux ARM ⚠️ Missing Bundled lacks PCRE2
Alpine/musl ⚠️ May fail Requires apk add libgcc libstdc++ ripgrep
WSL ⚠️ Slower Cross-filesystem penalties

Linux ARM Limitation

On ARM Linux, the bundled @vscode/ripgrep lacks PCRE2 support:

  • ❌ No look-around patterns ((?=...), (?!...))
  • ❌ No backreferences (\1, \2)
  • --pcre2 flag fails with "PCRE2 is not available"

Solution: Always use system ripgrep on ARM Linux.

Known Issues & Pitfalls

1. Setting Sometimes Ignored (Issue #6415)

Problem: USE_BUILTIN_RIPGREP=0 is sometimes ignored despite being set.

Verification:

# While Claude Code is running a search, check which rg is being used:
ps aux | grep rg

# Should show /usr/local/bin/rg or /opt/homebrew/bin/rg
# NOT a path inside claude-code installation

Workaround: Ensure the variable is exported in your shell's rc file, not just set in the current session.

2. WSL Performance

Problem: Disk read penalties when working across file systems on WSL.

Diagnosis: Run /doctor in Claude Code - shows Search as OK but may still be slow.

Solution: Keep projects on the Linux filesystem (/home/...), not Windows mounts (/mnt/c/...).

3. Alpine/musl Systems

Problem: Bundled ripgrep won't run on musl-based distributions.

Solution:

apk add libgcc libstdc++ ripgrep
export USE_BUILTIN_RIPGREP=0

Technical Deep Dive

Claude Code Binary Architecture

  • Type: Mach-O 64-bit executable (platform-specific)
  • Contains: Node.js modules compiled into binary
  • Ripgrep Package: @vscode/ripgrep (Microsoft's npm wrapper)
  • Bundled Path: vendor/ripgrep/{arch}-{os}/rg

Environment Variable Control

# In Claude Code's internal logic:
USE_BUILTIN_RIPGREP=1  # Default: Use bundled ripgrep
USE_BUILTIN_RIPGREP=0  # Override: Use system ripgrep from PATH

Feature Comparison (When Both Are Same Version)

Feature Bundled System
PCRE2 regex ✅ (except ARM Linux)
SIMD acceleration
.gitignore respect
Unicode support
Invocation overhead Higher (Node.js wrapper) Lower (direct exec)

References

Official Documentation

GitHub Issues

Performance Benchmarks

Related Projects

TL;DR

# Install ripgrep
brew install ripgrep  # or apt/pacman/winget

# Add to ~/.zshrc or ~/.bashrc
export USE_BUILTIN_RIPGREP=0

# Apply
exec zsh

# Verify
echo $USE_BUILTIN_RIPGREP  # Should be: 0
rg --version               # Should show system ripgrep

Result: 5-10x faster Grep operations in Claude Code! 🚀

Metadata

Metadata

Assignees

No one assigned

    Labels

    claude-codeClaude Code tips and trickshow-toHow-to guidesperformancePerformance improvementsterminalTerminal setup and usagetipsTips and best practices

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions