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/railsreturnsbash: bin/rails: No such file or directorybin/bundlenot found after bundle installbundle exec railsworks butbin/railsfails- Git shows
bin/directory as deleted or missing - Spring cannot start because
bin/springstub 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 installrun without--binstubsflagbin/directory added to.gitignoreaccidentally- Bundler 2.x changed default binstub behavior
- Manual deletion of
bin/directory - Running
bundle cleanremoving gems with binstubs
Step-by-Step Fix
- 1.Regenerate all binstubs:
- 2.```bash
- 3.# Generate binstubs for all gems that provide executables
- 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.Configure Bundler to always generate binstubs:
- 2.```bash
- 3.# Set Bundler config to always create binstubs
- 4.bundle config set --local bin bin
# This creates .bundle/config with: # --- # BIN: "bin"
# Now bundle install will automatically generate binstubs bundle install ```
- 1.Regenerate Rails binstubs specifically:
- 2.```bash
- 3.# Rails provides a rake task to regenerate binstubs
- 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.Fix binstub shebang lines:
- 2.```bash
- 3.# Check binstub content
- 4.head -1 bin/rails
- 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.Add binstub verification to CI:
- 2.```yaml
- 3.# .github/workflows/ci.yml
- 4.- name: Check binstubs
- 5.run: |
- 6.for cmd in bin/rails bin/rake bin/bundle; do
- 7.if [ ! -x "$cmd" ]; then
- 8.echo "ERROR: $cmd is missing or not executable"
- 9.echo "Run: bundle binstubs --all && chmod +x bin/*"
- 10.exit 1
- 11.fi
- 12.done
- 13.
`
Prevention
- Commit
bin/directory to version control (Rails default since 4.0) - Set
bundle config set --local bin binin project configuration - Add binstub check to pre-commit hooks
- Use
bundle execas a fallback in scripts and Makefiles - Document binstub regeneration in project onboarding guide
- Never delete
bin/without runningbundle binstubs --allafterward