Gemfile 101: All the Ways to Add Gems
Different Ways to Add Gems in a Gemfile
1. From RubyGems (default way)
Fetches from RubyGems.org.
gem "devise" # latest
gem "pg", "1.5" # exact version
gem "rails", "~> 7.1.0" # semver-compatible range
Use when: Stable, official gems.
Note: Prefer semantic versioning (~>
, >=
) to avoid breaking changes.
2. From GitHub (or any Git repo)
For unreleased features, bugfixes, or private gems.
gem "devise", github: "heartcombo/devise"
gem "devise", github: "heartcombo/devise", branch: "main"
gem "devise", github: "heartcombo/devise", ref: "a1b2c3d4"
gem "my_private_gem", git: "git@github.com:myorg/private.git"
Use when: You need unreleased fixes or internal/private gems.
3. From a Local Path
Developing a gem alongside your app.
gem "my_gem", path: "../my_gem"
Use when: Iterating locally without publishing.
4. From a Local .gemspec
Useful when your app is itself a gem/engine.
gemspec
Use when: Building Rails Engines or shared libraries.
5. From a .gem
File
Install a packaged gem artifact.
gem "my_gem", path: "vendor/gems/my_gem-0.1.0.gem"
Use when: Offline installs or internal distribution.
6. From Alternative Sources
Private gem servers or registries.
source "https://rubygems.org"
source "https://gems.mycompany.com" do
gem "my_internal_gem"
end
Use when: Your team/org uses private gem hosting.
⚡ TL;DR
- RubyGems → normal gems
- Pinned version → stability
- GitHub repo → unreleased fixes, private gems
- Local path → developing locally
- Gemspec → Rails Engine or lib
- .gem file → offline/internal use
- Alt sources → private servers
✅ Always commit Gemfile.lock
so the team has identical dependencies.