Back to home

Deploying Your Gem To RubyGems

Published: Mar 19, 2022

Last updated: Mar 19, 2022

In the previous part, we created our first Ruby gem.

In this part, we will deploy that gem to the package repository RubyGems.

Source code can be found here

Prerequisites

  1. Sign up for an account at RubyGems.
  2. Read "Creating Your Own Ruby Gem".
  3. Catch up with the previous posts on Series: Releasing your own Ruby Gem

Getting started

We will clone the project rspec-github-actions (if you haven't been keeping up with the previous work):

$ git clone https://github.com/okeeffed/rspec-github-actions.git $ cd rspec-github-actions # Checkout the starting point $ git checkout 2-gemspec

At this stage, our project is now ready to continue working on.

Logging into your RubyGems account

If you have not done so yet, we need to grab your credentials from RubyGems and save it to your computer:

$ curl -u <your-username> https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials; chmod 0600 ~/.gem/credentials # It will request a password

Once your password has been provided, your credentials will be write to the ~/.gem/credentials file.

Pushing the gem

We can use the gem push command in order to deploy a gem.

Note: The output from building the gem is ignored by Git, so if you skipped the previous part then ensure you run gem build contrived_math.gemspec.

In the terminal, run the following to push the gem:

$ gem push contrived_math-0.0.0.gem Pushing gem to https://rubygems.org... Successfully registered gem: contrived_math (0.0.0)

We can see the list of Gems that we have deployed like so:

$ gem list -r contrived_math *** REMOTE GEMS *** contrived_math (0.0.0)

Trialling our new gem

Let's create another subfolder example and initialize a new project with Bundler.

$ mkdir example $ cd example # Initialize a new project $ bundle init # Add our gem $ bundle add contrived_math Fetching gem metadata from https://rubygems.org/. Resolving dependencies... Fetching gem metadata from https://rubygems.org/. Resolving dependencies... Using bundler 2.1.4 Using contrived_math 0.0.0 # Create a file to test it out $ touch main.rb

Inside of example/main.rb, add the following:

require 'contrived_math' def main puts ContrivedMath.add(1, 2) puts ContrivedMath.subtract(2, 1) end main

Assuming that you are still in the example folder, we can run our code like so:

# From example/ $ ruby main.rb 3 1

It works!

Summary

Today's post demonstrated how to deploy your Ruby Gem to the RubyGems repository and demonstrated the usage of installation with Bundler in another example folder.

The next post in this series will look to automate the process as part of a GitHub Actions workflow.

Resources and further reading

Photo credit: rgaleria

Personal image

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on Visibuild.

1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.

Deploying Your Gem To RubyGems

Introduction

Share this post