skip to content

In a Rails 8.1 app, what does the generated `bin/setup` script do, and why can you run it again safely?

level: juniorimportance: should knowfreq 45%

answer

  1. one command from clone to server
  2. bundle check before bundle install
  3. bin/rails db:prepare, not db:setup
  4. --reset and --skip-server
  5. ends with exec bin/dev

basics

~20 s

bin/setup installs gems, prepares the database with bin/rails db:prepare, clears logs and tmp files, then execs bin/dev; each step converges on a state rather than repeating work, so reruns are safe, and --skip-server or --reset adjust it.

solid answer

~40 s

`bin/setup` is the one-command onboarding script `rails new` writes. It changes into the app root and runs `bundle check || bundle install`, then the JavaScript install if the app uses Node or Bun, then `bin/rails db:prepare`, which creates and loads the database only when needed. With `--reset` (new in 8.1) it also runs `bin/rails db:reset`. It then runs `bin/rails log:clear tmp:clear` and finally `exec "bin/dev"` to start the development server, unless you pass `--skip-server`. It is idempotent because every step converges rather than repeats: `bundle check` skips installed gems, `db:prepare` leaves an existing database alone, and clearing logs twice is harmless. The generated dev container and `config/ci.rb` both call it as `bin/setup --skip-server`.

code

ruby · 17 lines
ruby
FileUtils.chdir APP_ROOT do
  puts "== Installing dependencies =="
  system("bundle check") || system!("bundle install")

  puts "\n== Preparing database =="
  system! "bin/rails db:prepare"
  system! "bin/rails db:reset" if ARGV.include?("--reset")

  puts "\n== Removing old logs and tempfiles =="
  system! "bin/rails log:clear tmp:clear"

  unless ARGV.include?("--skip-server")
    puts "\n== Starting development server =="
    STDOUT.flush
    exec "bin/dev"
  end
end

go deeper

for a junior

Recall the order: gems, database, clear logs and tmp, start bin/dev. Know the two flags, --skip-server and --reset.

for a middle

Explain why each step is idempotent, what db:prepare does differently from a reset, and why system! stops on the first failure.

for a senior

Treat bin/setup as the team's onboarding contract: extend it with guarded steps, keep destructive actions behind flags, and reuse it in CI and dev containers.

for a principal

Decide how much environment setup belongs in bin/setup versus a dev container or image, and how to keep one path working for every developer.

## What `bin/setup` is for Every Rails app generated by `rails new` contains **`bin/setup`**, a plain Ruby script whose job is to take a fresh checkout to a running development server in one command. Its own comment states the contract: the script "is idempotent, so that you can run it at any time and get an expectable outcome". A new teammate runs it on day one; everyone else runs it after pulling a change that added a gem or a migration. ## The steps, in order 1. `FileUtils.chdir APP_ROOT`, so it works from any directory. 2. `system("bundle check") || system!("bundle install")`: install gems only if the lockfile is not already satisfied. 3. For apps with a Node or Bun toolchain, `yarn install --check-files` or `bun install`. A default import-map app has no such step. 4. `system! "bin/rails db:prepare"`: create the database if it does not exist and bring it up to date; skipped entirely in apps generated with `--skip-active-record`. 5. `system! "bin/rails db:reset"` only when you pass **`--reset`**, a Rails 8.1 addition for when you want a clean database. 6. `system! "bin/rails log:clear tmp:clear"`: truncate logs and empty `tmp/` caches. 7. `exec "bin/dev"` unless you pass **`--skip-server`**: the script replaces itself with the development server process. `system!` is a small helper defined at the top of the script that calls `system(*args, exception: true)`, so any failing step stops the script with an error instead of carrying on. ## Why reruns are safe | Step | Second run | |---|---| | `bundle check` | succeeds, so `bundle install` is skipped | | `db:prepare` | finds the database and only applies pending migrations | | `log:clear tmp:clear` | clears again, which costs nothing | | `exec bin/dev` | starts the server as before | What would make it unsafe is a destructive database task, which is why `db:reset` sits behind an explicit flag rather than running by default. ## Who else calls it - The generated **dev container** runs `bin/setup --skip-server` as its `postCreateCommand`, so the container is ready but the server is not started inside the build. - The generated **`config/ci.rb`** starts with a `"Setup"` step that runs `bin/setup --skip-server`, so local CI and the onboarding path use the same commands. - People use it after `git pull`, because it picks up new gems and migrations in one step. ## Extending it The file is yours. Typical additions: - copying a sample config file (the template ships a commented `database.yml.sample` block for exactly this); - installing system dependencies the app needs, such as image libraries for Active Storage variants; - seeding development data with a guard so it stays idempotent. Keep the contract when you extend it: every added step should check before it changes anything, and destructive steps belong behind a flag like `--reset`. ## How it fails The script mixes plain `system` and `system!` on purpose. `system("bundle check")` may fail without stopping anything, because its failure is the signal to run `system!("bundle install")`. Every other command goes through `system!`, so a failed install, a database that cannot be created or a broken migration raises an exception and stops the script with a non-zero exit. That matters in the dev container and in `bin/ci`: a half-prepared environment fails loudly instead of starting a server against a database that was never migrated. ## What changed recently In Rails 8.1 `bin/setup` gained `--reset`, and new apps no longer get a `bin/bundle` binstub, so the script calls `bundle` directly. An app generated by an older release keeps the `bin/setup` it was born with until someone regenerates it, which `bin/rails app:update` offers to do during an upgrade; compare it with the current template rather than assuming the two match.

  • Why does `bin/setup` use `exec` for `bin/dev` instead of `system`?
    `exec` replaces the setup process with the server process, so the terminal ends up attached to `bin/dev` directly and Ctrl-C stops the server cleanly. With `system` the setup script would sit as a parent process waiting for the server to exit. The template flushes STDOUT first so the last messages are printed before the process image changes.

bin/setup works like a thermostat rather than a heater switch: each run brings the checkout to the target state, installing or migrating only what is missing, instead of adding the same work again.

  • bin/setup runs db:setup, which wipes the development database every time.
  • bin/setup only installs gems; you create the database by hand.
  • Running bin/setup twice duplicates data or reinstalls every gem.
  • bin/setup always starts the server, so CI cannot use it.
  • The first step is bin/bundle install, using the generated bin/bundle.

Assumes Rails 8.1.4; the --reset flag is new in 8.1.0, and 8.1 apps have no bin/bundle for the script to call.