# Command line utilities in rubygems?

**URL:** <https://www.ruby-forum.com/t/command-line-utilities-in-rubygems/155592>\
**Category:** Ruby\
**Created:** [December 14, 2008, 7:04am UTC](https://www.ruby-forum.com/t/command-line-utilities-in-rubygems/155592 "2008-12-14T07:04:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![navouri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@navouri](https://www.ruby-forum.com/u/navouri)\
**Post date:** [December 14, 2008, 7:04am UTC](https://www.ruby-forum.com/t/command-line-utilities-in-rubygems/155592/1 "2008-12-14T07:04:55Z")

</div>

I have a small project that I’m working on and I would like the  
executable to be put in the usr bin path and setup so that it can be  
used as a command line utility automatically when installed using gem  
install. How would I go about setting it up that way? I’ve been trying  
to access the rubygems reference but it seems to be down.

---

<div class="post-metadata">

**Author:** ![navouri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@navouri](https://www.ruby-forum.com/u/navouri)\
**Post date:** [December 14, 2008, 8:09am UTC](https://www.ruby-forum.com/t/command-line-utilities-in-rubygems/155592/2 "2008-12-14T08:09:04Z")

</div>

On Dec 14, 12:57 am, Ch Ba [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## I have a small project that I’m working on and I would like the executable to be put in the usr bin path and setup so that it can be used as a command line utility automatically when installed using gem install. How would I go about setting it up that way? I’ve been trying to access the rubygems reference but it seems to be down.
> 
> Posted viahttp://www.ruby-forum.com/.

You can simply add it to the Rakefile. Here is an example Rakefile  
for a gem.

require ‘rubygems’  
Gem::manage\_gems  
require ‘rake/gempackagetask’

spec = Gem::Specification.new do |s|  
s.name = “some\_gem”  
s.version = “0.0.1”  
s.author = “The Author”  
s.email = “removed\_email\_address@domain.invalid”  
s.homepage = “[http://www.author.com](http://www.author.com)”  
s.platform = Gem::Platform::RUBY  
s.summary = “Some Gem”  
s.files = FileList[“{bin,tests,lib}/\*\*/\*”].to\_a  
s.require\_path = “lib”  
s.autorequire = “some\_gem”  
s.has\_rdoc = true  
s.extra\_rdoc\_files = [“README.txt”]  
s.executables = [‘the\_executable’]  
s.add\_dependency(‘dep1’)  
s.add\_dependency(‘dep2’)  
end

Rake::GemPackageTask.new(spec) do |pkg|  
pkg.need\_tar = true  
end

The part that says s.executables lists the files that will be  
installed into the path. In this case, its calling the file named  
the\_executable, which would be in the bin subdirectory of the gem.

So, my directory structure would be

$ ls  
README.txt bin/ pkg/  
Rakefile lib/ test/

$ ls bin/  
the\_executable\*

$ ls lib/  
some\_gem.rb

$ ls test/  
test\_some\_gem.rb

Then to package it up, run rake gem. It will create a directory  
called pkg, and place the gem in there.

Hope that is helpful.

Dusty Doris

---

<div class="post-metadata">

**Author:** ![navouri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@navouri](https://www.ruby-forum.com/u/navouri)\
**Post date:** [December 14, 2008, 8:21am UTC](https://www.ruby-forum.com/t/command-line-utilities-in-rubygems/155592/3 "2008-12-14T08:21:41Z")

</div>

One more quick question if you don’t mind, I had been executing the  
executable from the base directory of the project, and using "require  
‘lib/foo.rb’. Obviously this no longer works, how would I access  
“foo.rb” in it’s new location after it is installed through a gem?  
Sorry, I’m new to this whole gem thing =)

---

<div class="post-metadata">

**Author:** ![navouri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@navouri](https://www.ruby-forum.com/u/navouri)\
**Post date:** [December 14, 2008, 8:17am UTC](https://www.ruby-forum.com/t/command-line-utilities-in-rubygems/155592/4 "2008-12-14T08:17:45Z")

</div>

That is exactly what I needed! Thanks a million Dusty. I was using a  
.gemspec file and gem build foo.gemspec in order to gem it up, and even  
though I included the executable it wasn’t putting it in the bin. This  
Rakefile is much more useful though, I need to learn more about them.

---

<div class="post-metadata">

**Author:** ![navouri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@navouri](https://www.ruby-forum.com/u/navouri)\
**Post date:** [December 14, 2008, 8:55pm UTC](https://www.ruby-forum.com/t/command-line-utilities-in-rubygems/155592/5 "2008-12-14T20:55:41Z")

</div>

On Dec 14, 2:14 am, Ch Ba [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## One more quick question if you don’t mind, I had been executing the executable from the base directory of the project, and using "require ‘lib/foo.rb’. Obviously this no longer works, how would I access “foo.rb” in it’s new location after it is installed through a gem? Sorry, I’m new to this whole gem thing =)
> 
> Posted viahttp://www.ruby-forum.com/.

In the spec we have:

s.require\_path = “lib”  
s.autorequire = “some\_gem”

So, when you require rubygems, it will know how to find your new gem.  
Then, when you require ‘some\_gem’, it will look in the require\_path  
based off the root of your gem and automatically require some\_gem.rb.  
In this case it would be lib/some\_gem.rb.

So, in the executable, you can simply require your gem.

Here is an example of what the executable could look like, assuming  
you have a class method called do\_something that is doing what you  
need.

#!/usr/bin/env ruby

require ‘rubygems’  
require ‘some\_gem’

SomeGem.do\_something
