470

I would like to install gem from the latest GitHub source.

How do I do this?

Anthony Geoghegan
  • 11,533
  • 5
  • 49
  • 56
Vojto
  • 6,901
  • 4
  • 27
  • 33

11 Answers11

413

That depends on the project in question. Some projects have a *.gemspec file in their root directory. In that case, it would be:

gem build GEMNAME.gemspec
gem install gemname-version.gem

Other projects have a rake task, called gem or build or something like that. In that case you have to invoke rake <taskname>, but that depends on the project.

In both cases you have to download the source.

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
Dominik Honnef
  • 17,937
  • 7
  • 41
  • 43
  • 21
    Just a tip to let people know what it's really happening. The `gemname-version.gem` file is created when invoking `gem build` – Waiting for Dev... May 14 '12 at 10:42
  • Where does `gem install gemname-version.gem` command installs the git gem locally? I cannot find anywhere in my local machine an engine gem installed this way. Where does bundler hides it? – Green Jun 17 '13 at 19:11
  • 3
    I think the `gem install gemname-version.gem` line should be `gem install --local gemname-version.gem` – JonB Jun 04 '14 at 13:24
  • @Green - `gem which gemname` should tell you where a specific gem is, does that not work for you? – sameers Aug 27 '14 at 05:17
  • Hi, I have only Rakefile and I have no clue how to install it. Any help? – eldi Mar 10 '16 at 09:42
345

In case you are using bundler, you need to add something like this to your Gemfile:

gem 'redcarpet', :git => 'git://github.com/tanoku/redcarpet.git'

And in case there is .gemspec file, it should be able to fetch and install the gem when running bundle install.

UPD. As indicated in comments, for Bundler to function properly you also need to add the following to config.ru:

require "bundler" 
Bundler.setup(:default)
Misha Reyzlin
  • 13,736
  • 4
  • 54
  • 63
  • 9
    I also needed to add the following (added to my config.ru): `require "bundler" Bundler.setup(:default)` See [bundler docs](http://gembundler.com/bundler_setup.html) for more details – Louis Sayers Jul 18 '12 at 10:15
  • 44
    Additionally one can specify ref, branch or tag options for example `gem 'redcarpet', :git => 'git://github.com/tanoku/redcarpet.git', :branch => 'yourbranch'` – Amit Patel Jul 20 '12 at 11:57
  • 26
    there's also: `gem 'redcarpet', github: 'tanoku/redcarpet'`. http://akash.im/2012/06/05/bundler-new-github-option.html – Andrew Oct 31 '12 at 17:08
  • @AmitPatel Thank you so so so much !!! :branch => 'yourbranch', your this line has solved a great problem for me just now. So much Grateful to you. – K M Rakibul Islam Mar 14 '13 at 15:00
  • @gryzzly, how to specify tag for git? – gaussblurinc Mar 31 '14 at 09:41
  • 4
    @gaussblurinc `gem 'redcarpet', :git => 'git://github.com/tanoku/redcarpet.git', :tag => 'v2.3.5'` <- the `:tag => ''` part – Misha Reyzlin Apr 26 '14 at 14:12
280

Try the specific_install gem it allows you you to install a gem from its github repository (like 'edge'), or from an arbitrary URL. Very usefull for forking gems and hacking on them on multiple machines and such.

gem install specific_install
gem specific_install -l <url to a github gem>

e.g.

gem specific_install https://github.com/githubsvnclone/rdoc.git 
Kamek
  • 3,383
  • 1
  • 16
  • 9
  • 3
    could you add more explanation on the `specific_install` gem? – microspino Aug 04 '12 at 14:08
  • 2
    This is exactly what I was looking for, similar to Python's pip git support. gem specific_install -l worked like a charm! – Murph Nov 19 '12 at 10:26
  • 1
    `ERROR: While executing gem ... (NoMethodError) undefined method 'build' for Gem::Package:Module` Sounds very cool but I won't be looking into it further. Just wanted to post that it didn't work for me in case someone else is about to give it a whirl based on SO recommendation. – isomorphismes Jun 18 '14 at 04:49
  • 1
    @isomorphismes +1 to your comment. I created a separate question about that error here: http://stackoverflow.com/questions/27045874/error-when-running-gem-specific-install – La-comadreja Nov 20 '14 at 17:31
  • Is there a way to specify branch to use? - Edit responding to myself, from the doc: "$ gem specific_install http://github.com/githubsvnclone/rdoc.git edge" or "$ gem specific_install -l http://github.com/githubsvnclone/rdoc.git -b edge" – KumZ Apr 08 '21 at 18:05
43

Bundler allows you to use gems directly from git repositories. In your Gemfile:

# Use the http(s), ssh, or git protocol
gem 'foo', git: 'https://github.com/dideler/foo.git'
gem 'foo', git: 'git@github.com:dideler/foo.git'
gem 'foo', git: 'git://github.com/dideler/foo.git'

# Specify a tag, ref, or branch to use
gem 'foo', git: 'git@github.com:dideler/foo.git', tag: 'v2.1.0'
gem 'foo', git: 'git@github.com:dideler/foo.git', ref: '4aded'
gem 'foo', git: 'git@github.com:dideler/foo.git', branch: 'development'

# Shorthand for public repos on GitHub (supports all the :git options)
gem 'foo', github: 'dideler/foo'

For more info, see https://bundler.io/v2.0/guides/git.html

mmoya
  • 1,901
  • 1
  • 21
  • 30
Dennis
  • 56,821
  • 26
  • 143
  • 139
  • Please note that you might get in trouble if you use this approach with passenger and apache / ngix. When running `bundle`, such git-gem- dependencies will not be installed globally but in the current user's home directory instead. Passenger will run ruby as your web-server's user (e.g. `www-data`) which has no access to this directory and therefore this "git-gem" won't be loaded. You will get an error `... is not yet checked out. Run bundle install first`. – fishbone Feb 01 '19 at 11:42
16

OBSOLETE (see comments)

If the project is from github, and contained in the list on http://gems.github.com/list.html, then you can just add the github repo to the gems sources to install it :

$ gem sources -a http://gems.github.com
$ sudo gem install username-projectname
Christian Schlensker
  • 21,708
  • 19
  • 73
  • 121
Michael Pereira
  • 1,263
  • 1
  • 17
  • 28
  • 1
    o rly? i just now did this and it DID work... go to gems.github.com before adding it to yr sources i guess? (but don't use sudo) – esharp Jul 21 '12 at 21:50
  • 7
    @esharp, they host the ones they built, but they don't build them anymore. If the gem got updated [since 2009](https://github.com/blog/500-state-of-the-hub-rackspace-day-0), a gems.github.com copy will be obsolete. – Tobu Oct 08 '12 at 17:06
12

If you are getting your gems from a public GitHub repository, you can use the shorthand

gem 'nokogiri', github: 'tenderlove/nokogiri'
austinheiman
  • 939
  • 1
  • 12
  • 17
Rajeev Sharma
  • 1,527
  • 2
  • 13
  • 16
5

You can also use rdp/specific_install gem:

gem install specific_install
gem specific_install https://github.com/capistrano/drupal-deploy.git
OwilliwO
  • 51
  • 1
  • 5
4

Also you can do gem install username-projectname -s http://gems.github.com

Chuck Vose
  • 4,560
  • 24
  • 31
3

In your Gemfile, add the following:

gem 'example', :git => 'git://github.com/example.git'

You can also add ref, branch and tag options,

For example if you want to download from a particular branch:

gem 'example', :git => "git://github.com/example.git", :branch => "my-branch"

Then run:

bundle install
slal
  • 2,657
  • 18
  • 29
3

If you install using bundler as suggested by gryzzly and the gem creates a binary then make sure you run it with bundle exec mygembinary as the gem is stored in a bundler directory which is not visible on the normal gem path.

Mark Cheverton
  • 1,202
  • 10
  • 8
2

On a fresh Linux machine you also need to install git. Bundle uses it behind the scenes.

Community
  • 1
  • 1
user1208639
  • 303
  • 3
  • 4