title: Git gems description: > How to get Ruby gems from any git server. categories: posts
We can install Gems from any git repo with:
# gems.rb
gem "gem_name",
git: "https://git-server.com/user/repo.git",
branch: "5-stable",
ref: "4aded",
tag: "v5.0.0"
If we only specify the git repo, ref will default to HEAD
, and branch to master
.
For security reasons it's better not to use http
, nor git
as protocols. Same
goes for bundler's built-in git sources, at least for versions <= 1.16.
When we need several gems from the same source we can DRY our gems.rb
file with
git_source(:not_a_bug){ |repo_details| "https://notabug.org/#{repo_details}.git" }
gem "some_gem", :not_a_bug => "user/repo"
gem "another_gem", :not_a_bug => "team/repo"
We can even use blocks to group different gems from a single repo:
not_a_bug "user/repo", branch: "master" do
gem "gem_name-plugin"
gem "gem_name-extension"
end
If we need to work with the local git repo of a gem
$ bundle config local.gem_name /path/to/local/git/repo.git
which overrides the gems.rb
file behaviour. Specifically,
gem "gem_name", :git => "https://git.com/user/repo.git", :branch => "fix-or-feature"
Since new commits in the local git repo update the revision in the gems.locked
file, bundler requires a branch.
To reset gems.rb
behaviour back to normal,
$ bundle config --delete local.gem_name
Whilst possible to save git-server
's credentials using bundle config
$ bundle config --global https://git.com user:password
$ bundle config --local https://git.com/project/repo.git user:password
Is better to do so through an env var
$ export BUNDLE_NOTABUG__ORG=user:password
Notice how any periods in the configuration keys must be replaced with two underscores when setting it via environment variables. The configuration key local.rack becomes the environment variable BUNDLE_LOCAL__RACK.
For private repos in hosts where we can use personal OAuth tokens we can
$ export BUNDLE_GIT__COM=abcd0123generatedtoken:x-oauth-basic