Skip to main content

How to Create a Gemfile with bundle init

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A Ruby project lists the gems it uses in a file named Gemfile, and Bundler, which comes with Ruby, installs and loads exactly those gems. bundle init creates the Gemfile for a new project in one command.

Syntax​

Syntax
bundle init
PartWhat it does
bundleRuns Bundler, Ruby's gem manager for projects.
initCreates a Gemfile in the folder you're in, unless one is already there.

The Example​

The example creates a folder for a small project named number-words and gives it a Gemfile.

Prerequisites​

  • Ruby, which comes with Bundler. Check them with ruby --version and bundle --version. This guide uses Ruby 4.0.10 and Bundler 4.1.0.

Step 1: Create the Gemfile​

Terminal
mkdir number-words
cd number-words
bundle init
Output
Writing new Gemfile to /Users/Shared/you/number-words/Gemfile

A zsh terminal where mkdir and cd create and enter number-words, and bundle init prints Writing new Gemfile to the folder's Gemfile.

The commands are the same in zsh, bash and PowerShell.

Step 2: Look at the Gemfile​

The Gemfile bundle init writes: a frozen_string_literal comment, source "https://rubygems.org" and a commented-out gem "rails" line.

Gemfile
# frozen_string_literal: true

source "https://rubygems.org"

# gem "rails"
LineWhat it means
# frozen_string_literal: trueA magic comment that makes the file's text values unchangeable. It's a common Ruby setting and doesn't affect which gems you get.
source "https://rubygems.org"Where Bundler downloads gems from: RubyGems.org, Ruby's public gem registry.
# gem "rails"An example, commented out with #, of how a gem is listed.

The Gemfile is Ruby code, which is why its comments start with #. Each gem the project uses gets a gem line, which bundle add writes for you.

Common Mistakes​

MistakeWhat happensDo this instead
Running bundle init where a Gemfile already existsBundler refuses and keeps the existing fileEdit the existing Gemfile, or add gems with bundle add
Running it in the wrong folderThe Gemfile lands therecd into the project first
Installing a project's gems one by one with gem installThe project doesn't record which gems and versions it needsList them in the Gemfile with bundle add

Troubleshooting​

SymptomLikely causeFix
Gemfile already exists at …/GemfileThe folder already has oneUse the existing file
command not found: bundleRuby, which includes Bundler, isn't installed or isn't on your PATHInstall Ruby and open a new terminal

FAQ​

What's the difference between a Gemfile and a gemspec?​

A Gemfile lists the gems an application uses. A .gemspec describes a gem you publish yourself, including the gems it depends on.

Is Bundler a separate install?​

No. Bundler has come with Ruby since Ruby 2.6, so bundle works as soon as Ruby does.

Conclusion​

bundle init creates a Gemfile that points at RubyGems.org and is ready for your project's gems. From there, bundle add lists gems in it and installs them, and bundle exec runs your code with exactly those gems.

Additional Resources​