So why would you want to host your own gem server?
Well recently, I had to share some in-house code between different proprietary rails apps. We needed this code and all it’s dependencies to be easily installed on different development machines and we wanted to support multiples versions of the code in the Rails apps.
Does this sound like a job for rubygems and bundler? Yes? Well that’s what I thought too, except for fact that for bundler to find a gem, it needs to be available in one of the sources in your Gemfile.
Like I mentioned, this was all private code, so I couldn’t just push it to rubygems.org or rubyforge (also, it would be of no use to anyone outside our organisation).
But luckily, a bundler source can be any valid gem repository, even one you host yourself.
To host your own Rubygem server you don’t need to install anything, all you need to do is type:
This will host all the gems you have installed. This is all nice and easy, but it can get a cumbersome to maintain in a real development environment. It doesn’t let you ‘push’ gems to the server and it provides no information about the gems it hosts.
So basically I wanted all the convenience of rubygems.org, but without releasing my code to the world. After some searching, it seemed like I had one of two options:
- hosting the opensource rubygems.org server myself. As you can see from their development setup guidelines, this is quite an undertaking. It’s quite a complex app with lots of dependencies and it’s built with a lot of assumptions to how it’s used on the public site. This might be viable for you in a big organisation if you needed some of the nicer features of the web ui, but seemed like total overkill for my situation.
- Using geminabox. It’s a Sinatra app that allows you to host your ruby gems on your own server. It also lets you push gems, host multiple versions and even has a nifty little web ui. Perfect.
Their github page has all the info to get you up and running, but if you don’t feel like clicking the link, I’ll run you through what I did.
First up, I setup a dedicated server vm and installed Ruby 1.9.3, RVM and bundler. After creating a directory, gemset and .rvmrc for my gem server, I installed geminabox:
gem install geminabox
This will install sinatra too. I then created a directory for my gem data:
All that was needed now was a rackup file:
require "rubygems" require "geminabox" Geminabox.data = "./geminabox-data" run Geminabox
And that’s it! I could now easily start my gem server with
This starts up your gem server that should look something like this:
If you need robustness, you’ll probably want to host this using something like passenger and nginx, but I’ll leave that as an exercise to the reader.
To start pushing gems to your server, you also need to install geminabox on your client development machines:
gem install geminabox
gem inabox pkg/my-awesome-gem-1.0.gem
Once your gems are hosted you can consume them using bundler by just adding a source to your Gemfile:
Or add it to your gem sources:
gem sources --add http://mygemserver:9292
As always, questions and comments are welcomed. Let me know what you think!
(Edit) Using git
After some insightful comments, (see below), I have realised that that I have failed the mention one of the easier options available to ‘host’ your own gem, just by using git. Bundler understands git, so in your Gemfile, you can just add a reference to your git repo. You can also point to a branch , ref and tag.
You can get more detail on git in Gemfiles here.