Making a Ruby Gem With Bundler

David Brennan
2 min readDec 12, 2017

--

Making a ruby gem is a simple task — the hard part is finding a piece of code you think is important enough to share with the world.

For more detailed guides check out the rubygems.org guide or the bundler guide.

To get started, make sure you have the bundler gem installed by running this in your terminal:

gem install bundler

You can make gems without bundler, but it makes the process a whole lot easier and will be the method I outline here.

The next command we run is:

bundle gem YOUR-GEM-NAME

Check out the rubygems.org guide on naming.

By running that command bundler will have created a new directory in the active directory.

Open up the .gemspec file to get started. It should look something like this:

Fill these out with your information and information about your gem. Underneath this is a section of code that will prevent you from pushing your gem to unverified sources. It may cause issues later on, so you can delete it, but be careful!

Make sure your files are included in the specification with:

spec.files ['FILE-PATH']

Right now that’s probably ‘lib/YOUR-GEM-NAME.rb’.

If you have any other gem dependencies, make sure to include them by adding this in the specification:

spec.add_dependency "GEM-NAME"

Add your class or module to the generated file in the lib directory. This is the functionality that people will get when they use your gem!

Once everything is set up run this in your terminal:

gem build YOUR-GEM-NAME.gemspec

This will create a gem file in the current directory.

Now, you might be tempted to publish now, but there is still some work to do. In terminal run:

gem install ./YOUR-GEM-NAME-0.1.0.gem

The numbers at the end may vary– that’s your version number, which should have defaulted to 0.1.0.

Now open IRB in your terminal and type:

require 'YOUR-GEM-NAME'

If it returns true, that means it’s been successfully installed!

At this point you should make an account on rubygems.org.

From there, set up your ~/.gem/credentials file by running the command found at the bottom of your rubygems.org edit profile section under “API access”.

After that, just run:

gem push YOUR-GEM-NAME-0.1.0.gem

And your gem will be available for the world to use!

--

--