# Deploying Your Gem To RubyGems

> In part two of the Ruby gem series, we will take the gem we created in the previous part and deploy it to RubyGems.

- Canonical: https://dennisokeeffe.com/blog/2022-03-19-deploying-your-gem-to-rubygems
- Published: 2022-03-19
- Updated: 2022-03-19
- Tags: ruby

In the [previous part](https://blog.dennisokeeffe.com/blog/2022-03-19-creating-your-own-ruby-gem), we created our first Ruby gem.

In this part, we will deploy that gem to the package repository [RubyGems](https://rubygems.org/).

Source code can be found [here](https://github.com/okeeffed/rspec-github-actions)

## Prerequisites

1. [Sign up](https://rubygems.org/sign_up) for an account at RubyGems.
2. Read ["Creating Your Own Ruby Gem"](https://blog.dennisokeeffe.com/blog/2022-03-19-creating-your-own-ruby-gem).
3. Catch up with the previous posts on [Series: Releasing your own Ruby Gem](https://blog.dennisokeeffe.com/blog/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):

```s
$ 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:

```s
$ 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:

```s
$ 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:

```s
$ 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.

```s
$ 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:

```rb
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:

```s
# 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

- [RubyGems - Sign Up](https://rubygems.org/sign_up)
- [RubyGems](https://rubygems.org/)
- [Source code](https://github.com/okeeffed/rspec-github-actions)
- ["Creating Your Own Ruby Gem"](https://blog.dennisokeeffe.com/blog/2022-03-19-creating-your-own-ruby-gem)

_Photo credit: [`rgaleria`](https://unsplash.com/@rgaleria)_
