TL;DR
This post covers how to run a simple gem server to host your ruby gems. If you require the ability to push packages and host them, you can use something like the Gem in a Box Project.
packagecloud.io is a hosted solution which allows you to push, host and manage gems in a secure package repository.
Reasons
There are many reasons to host your own gem server. It could be to easily host packages offline for development purposes, to ensure the security of intellectual property over a trusted network or to remove a dependency on a third-party service (like RubyGems.org) when deploying software.
Whatever your specific case calls for, running a gem server doesn’t come without some technical overhead. This post will try to outline some ways to help you manage your gem installation and distribution requirements.
Local Gem Servers
Using RubyGems Default Server
RubyGems is bundled with a command - aptly named gem server
- used for serving installed rubygems.
The gem server
command starts a server on a port (default is 8808
- can be specified with -p
or --port
) and will, by default, serve up all packages installed on the local system. You can visit http://localhost:8808 in a browser to see an index of all the packages installed on the system.
In order to use the local gem server as a source for installing gems, you must ensure that the cache files for the gems exist on the system running the server. You can also specify a directory (or directories) to search for installed gems by passing the -d
or --dir
options to the gem server
command.
To make new packages available, just add the desired gems into the specified gem directory.
To see all available options, run:
Using Gem in a Box
If you need to be able to push gems, as well as host them, take a look at the Gem in a Box Project. geminabox
allows you to push gems and host them from your local machine.
First, install geminabox
:
As per the project README, create a config.ru
with the following:
Next, make sure the /path/to/your/gem/repo
folder exists. This is where your packages will end up when pushing them via the geminabox
command.
Then, run the server from your config.ru
:
Once the server is up and running, you can push packages using the command-line utility gem inabox
Or visit the page http://localhost:9292 in a browser and use the web-upload function provided by the geminabox
server. This page also lists all the packages pushed to your geminabox
server.
Hosted Gem Server
Using packagecloud.io as a hosted gem server
packagecloud.io provides pubic and private package repositories that you can use to host rubygems, debian and redhat packages. To start using packagecloud as a gem server, first create an account and repository on packagecloud.io.
Once you have an account, a repository can be created by either using the web interface or by using the packagecloud.io cli.
After creating a repo, install the gem:
Next, push your gem file to your newly created repo:
When running the push command for the first time, you will be prompted to enter your email address and password for the packagecloud.io site.
Installing gems from your gem server
To install packages from your gem server of choice (packagecloud.io, geminabox
or gem server
), you must set the source for gems on your system. This can be done by using the gem sources
command and passing it the path to your server:
Then install gems as you normally would:
Or if you’re using Bundler, set the source in the Gemfile
directly:
Conclusion
Using your own gem server has many advantages, such as decoupling your deployment process from a third-party service like RubyGems.org and protecting private libraries from being shared over the internet. Taking granular control of your packaging system gives you the advantage of ensuring your packages are available when you need them and are accessible in a safe and secure way.
For a more full-featured setup including a token-authentication system, automation tooling and a robust API, packagecloud.io makes it easy to create and manage package repositories for ruby gems.