Ruby: Gemfile VS Gemspec
If you have ever written a Rails application, you would be familiar with the Gemfile. But the Gemspec file would be familiar if you have written a Gem. Rails Plugin/Engine are Gem.
These files are different with one big similarity.
Gemspec is closely similar to package.json in the javascript world. It defines the author’s name, email, license, dependencies, and so on. check below block for a sample.
Gem::Specification.new do |s|
s.name = "raile"
s.version = version
s.summary = "Full-stack web application framework."
s.license = "MIT"
s.author = "Mark"
s.email = "mark@raile.com"
s.homepage = "https://raile.org"
s.files = ["README.md", "MIT-LICENSE"]
s.add_dependency "activesupport", version
s.add_dependency "actionpack", version
endGemfile is mostly a dependency manager. Make sure all codes which you depend on are available. From any source. check below for a sample.
source "https://rubygems.org"
gemspec
gem "minitest", ">= 5.15.0", "< 5.22.0"
gem "sprockets-rails", ">= 2.0.0"
gem "propshaft", ">= 0.1.7"
gem "capybara", ">= 3.39"
gem "selenium-webdriver", ">= 4.11.0"
gem "rack-cache", "~> 1.2"
gem "stimulus-rails"
The Problem
With the introduction out of the way, you might ask so what? why are we talking about them?
One thing you should notice is that Gemspec has dependency management (s.add_dependency) as one of its features. And Gemfile’s sole purpose is to manage dependencies. That brings us to the problem. They are doing similar jobs. And it becomes confusing if you have a project that has both files. Where do you put your dependencies? Which one is the right place? Are some dependencies supposed to be in one and others in the other?
The Rationale
This will become clear if you look at it from the installation. Look at it from where is the Gem being installed. Is it being installed on the system or an application?
Get Unegbu Kingsley’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s take an example of a Gem (food_tracker) you are working on that has these two files (Gemfile and Gemspec). After you are done with the Gem you publish the Gem to rubygems.org.
A user wants to use your Gem on a system level and installs your Gem with the following command
gem install food_trackerThis will install the Gem using the dependencies from Gemspec alone.
If the user wants to install it on an application they too are working on, they add your Gem to their application Gem file (gem food_tracker) and run
bundle install Then, this will install your Gem and dependencies from Gemfile. But there is a caveat. In the Gemfile, we are also calling the Gemspec file. We can see this in the second code block of this article a method gemspec.
gemspecWith all this, it means that installation with Gemfile uses dependencies defined in the Gemspec file. But it is not the same the other way round.
So all breaking and important dependencies should be defined in Gemspec file.
Conclusion
With all this I hope I relieve any confusion if you see projects that have Gemfile and Gemspec files with dependencies.
Thanks for your attention and happy coding.







