Ruby Gems are the default way to share code among Ruby projects and developers. While public Gems can be found and used via RubyGems.org, there are occasions when you might need to create a private gem, perhaps for proprietary tools or internal use within your organization or an end customer’s. Here’s a a brief info on how to create a private Ruby gem.
1. Setting Up Your Gem Environment
The first step in creating a private gem is setting up the right environment for development. You’ll need a basic understanding of Ruby and the command line. Start by installing Ruby if you haven’t already, and ensure that you have Bundler installed (gem install bundler
). Bundler will manage your gem’s dependencies and environment. I strongly recommend you use something like Rbenv or RVM to manage multiple Ruby versions on your local machine.
2. Initializing Your Gem
To create your Gem, run bundle gem your_gem_name.
This command scaffolds a new Gem, creating a directory with the same name as your Gem, populated with the necessary files for your Gem’s development, including a .gemspec
file which is crucial for defining your Gem’s properties and dependencies.
3. Coding Your Gem
For lack of a better term your main (or rather entry) file for your gem will be lib/your_gem_name.rb
. From here it’s just Ruby coding. You can add tests and other dependencies if needed just like you would in any other Ruby project. Remember to update your .gemspec
file with the correct information about your Gem, including name, version, authors, and dependencies; that last bit is extremely important, as it’s very easy to forget the exact versions of your gem’s dependencies, especially if you’ll be maintaining the package for a period of years.
4. Testing Locally
Run gem build your_gem_name.gemspec
to build your Gem. Then, install it using gem install ./your_gem_name-0.0.1.gem
, adjusting the version number as necessary. This isn’t strictly necessary, but I prefer testing locally before bothering to pull an in development gem to source control. With that said, you 100% should absolutely be using source control!
5. Hosting Your Private Gem
For hosting your private Gem, there are a few options:
- Git repositories: You can host your Gem in a private Git repository. Users can then add the Gem to their
Gemfile
using thegit
option, pointing to the repository URL. You will need to ensure that your deployment targets have whatever required credentials your Git repo requires. If you’re using Github or Gitlab, you can setup individual access credentials per project per repo; the benefit there is that you are not giving your deployment server your SSH key which presumably has access to all the repos that you have access to. - Gem servers: There are tools like Geminabox or Gemfury that are specialized hosting providers for gems and other packages. While these services provide some additional features like version control and dependency management, they are not strictly necessary and I’d recommend for someone just getting started that they try to host their first gem in a plain old Git repo; if for no other reason, simply to learn how it’s done.
6. Primtime
Once your gem is hosted, you can include it in your projects by adding it to your Gemfile
. If you’re using a Git repository, for example, it would look something like this: gem 'jar_jar', git: 'https://yourprivategitrepo.com/jar_jar.git'
. That’s it! You’re good to go! If you enjoyed this or found it useful, please checkout Coder Radio and if you have any automation or data management needs, take a look at Alice.