Introduction

Bundler generates binstubs (executable wrapper scripts) in the bin/ directory for gem executables. When binstubs are missing, not generated, or corrupted, running bin/rails, bin/rake, or other commands fails with "No such file or directory." This commonly happens after bundle install without the --binstubs flag, after deleting the bin/ directory, or when upgrading Bundler versions.

Symptoms

  • bin/rails returns bash: bin/rails: No such file or directory
  • bin/bundle not found after bundle install
  • bundle exec rails works but bin/rails fails
  • Git shows bin/ directory as deleted or missing
  • Spring cannot start because bin/spring stub is missing

Check binstub status: ```bash ls -la bin/ # Empty or missing expected files: rails, rake, bundle, spring

# Compare with what should exist bundle binstubs --list # Should show all gem executables with binstubs ```

Common Causes

  • bundle install run without --binstubs flag
  • bin/ directory added to .gitignore accidentally
  • Bundler 2.x changed default binstub behavior
  • Manual deletion of bin/ directory
  • Running bundle clean removing gems with binstubs

Step-by-Step Fix

  1. 1.Regenerate all binstubs:
  2. 2.```bash
  3. 3.# Generate binstubs for all gems that provide executables
  4. 4.bundle binstubs --all

# Or generate specific binstubs bundle binstubs rails bundle binstubs rake bundle binstubs rspec-core

# Verify ls -la bin/ # Should show: bundle, rails, rake, spring, etc. ```

  1. 1.Configure Bundler to always generate binstubs:
  2. 2.```bash
  3. 3.# Set Bundler config to always create binstubs
  4. 4.bundle config set --local bin bin

# This creates .bundle/config with: # --- # BIN: "bin"

# Now bundle install will automatically generate binstubs bundle install ```

  1. 1.Regenerate Rails binstubs specifically:
  2. 2.```bash
  3. 3.# Rails provides a rake task to regenerate binstubs
  4. 4.bundle exec rake rails:update:bin

# Or use the app:update task bundle exec rake app:update:bin

# This recreates: # - bin/rails # - bin/rake # - bin/setup # - bin/spring (if spring is installed) ```

  1. 1.Fix binstub shebang lines:
  2. 2.```bash
  3. 3.# Check binstub content
  4. 4.head -1 bin/rails
  5. 5.# Should show: #!/usr/bin/env ruby

# If shebang points to wrong Ruby path, regenerate: rm bin/rails bundle binstubs rails

# Or fix manually: sed -i '1s|^#!/.*ruby|#!/usr/bin/env ruby|' bin/rails chmod +x bin/rails ```

  1. 1.Add binstub verification to CI:
  2. 2.```yaml
  3. 3.# .github/workflows/ci.yml
  4. 4.- name: Check binstubs
  5. 5.run: |
  6. 6.for cmd in bin/rails bin/rake bin/bundle; do
  7. 7.if [ ! -x "$cmd" ]; then
  8. 8.echo "ERROR: $cmd is missing or not executable"
  9. 9.echo "Run: bundle binstubs --all && chmod +x bin/*"
  10. 10.exit 1
  11. 11.fi
  12. 12.done
  13. 13.`

Prevention

  • Commit bin/ directory to version control (Rails default since 4.0)
  • Set bundle config set --local bin bin in project configuration
  • Add binstub check to pre-commit hooks
  • Use bundle exec as a fallback in scripts and Makefiles
  • Document binstub regeneration in project onboarding guide
  • Never delete bin/ without running bundle binstubs --all afterward