Skip to content

Guide · Open source

How to use Faker: generate fake data in Ruby tests and seeds

Updated · written with AI from the project's own documentation and checked against our catalogue data

Faker is a Ruby library that generates made-up but realistic-looking data such as names, addresses, email addresses and phone numbers. It is useful for filling automated tests, demos and development databases without using real personal information. This guide shows you how to add it to a Ruby project, call its generators, and use the options for unique values, seeding and locales.

Fakerfaker-ruby/faker · ★ 11.9k · MIT

Quick start

  1. 1Add the gem. Add gem 'faker' to your Gemfile.
  2. 2Install dependencies. Run bundle install to fetch Faker and its dependencies.
  3. 3Require Faker. Add require 'faker' where you need fake data.
  4. 4Call a generator. Use a namespace such as Faker::Name.name or Faker::Internet.email to get a value.
  5. 5Request unique values if needed. Prefix the call with unique, and clear the record between tests if values repeat across runs.
  6. 6Set a locale if needed. Assign Faker::Config.locale to a locale code such as 'es' for localized names and addresses.

Install Faker in your Ruby project

Faker is a Ruby gem, so you need Ruby and Bundler installed first. Add the gem to your Gemfile, then install dependencies.

  • Prerequisites: Ruby and Bundler
gem 'faker' bundle install

Generate your first fake values

Require Faker, then call a generator from its namespace. Each generator groups related data, such as Name, Internet or Address. The README notes that values are random, so your output will differ from the examples shown.

require 'faker' Faker::Name.name Faker::Internet.email Faker::Address.full_address

Get unique values

Faker does not guarantee uniqueness by default. Prefix a call with unique to ask for values that have not been returned yet. For generators with a limited set of possible values, Faker may raise Faker::UniqueGenerator::RetryLimitExceeded when too many unique values are requested. You can clear the record of used values, for example between tests.

  • Faker::Name.unique.clear clears used values for one generator
  • Faker::UniqueGenerator.clear clears used values for all generators
Faker::Name.unique.name Faker::Name.unique.clear Faker::UniqueGenerator.clear

Make output repeatable with a seed

Faker can seed its pseudo-random number generator so repeated runs produce the same output. Set Faker::Config.random to a Random object with a fixed seed. Setting it to nil uses the default entropy sources again.

The README also notes that for Minitest with Faker 2.22 or later, you may need to set Faker::Config.random = Random.new in test_helper.rb or rails_helper.rb to avoid duplicate values.

Faker::Config.random = Random.new(42) Faker::Lorem.word Faker::Config.random = nil

Use other locales

Faker uses the I18n gem to store strings and formats for names and postal codes. Set the locale and Faker uses it for the data it generates. The README says Faker offers over 40 locales for names, addresses and phone numbers. To override locales or set them on threaded servers, see the locales README in the repository.

Faker::Config.locale = 'es'

What people use it for

  • Filling a test suite with sample user records
  • Seeding a Rails development database with placeholder users
  • Building a demo or prototype with believable-looking names and addresses
  • Generating sample transactions or crypto addresses from the Finance generators
  • Producing localized sample names and phone numbers for a non-English demo

Frequently asked questions

How do I install Faker?

Add gem 'faker' to your Gemfile, then run bundle install. The README lists Ruby and Bundler as the prerequisites.

Is Faker free to use?

Yes. Faker is released under the MIT license, and the README says the code is free to use under its terms.

Does Faker generate real personal data?

No, values are random. The README warns that generated names, addresses, emails and phone numbers might sometimes match valid real-world information, so take care in tests.

Can I use Faker outside Ruby?

Faker is a Ruby gem, so it is meant for Ruby projects. The README names Perl's Data::Faker as its inspiration, but describes it as an inspiration rather than a direct equivalent.

Why do I get duplicate values in my tests?

By default values are not guaranteed to be unique. Use the unique prefix for values that must differ, and see the README note on Minitest with Faker 2.22 or later about setting Faker::Config.random = Random.new.

How do I get the same fake data every run?

Set Faker::Config.random to Random.new with a fixed seed, such as Random.new(42). The README shows that repeated seeded calls return the same values.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

Faker suits Ruby developers and Rails teams who need realistic sample records for tests, seeds and demos, with localization for over 40 locales. Do not use it for real or verified personal data, or in production systems that must never produce real-looking personal details, because the README warns its output can occasionally be valid.

Install options & downloads