# Creating ruby script exe

**URL:** <https://www.ruby-forum.com/t/creating-ruby-script-exe/235378>\
**Category:** Ruby\
**Created:** [June 18, 2013, 1:27pm UTC](https://www.ruby-forum.com/t/creating-ruby-script-exe/235378 "2013-06-18T13:27:00Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@alcina](https://www.ruby-forum.com/u/alcina)\
**Post date:** [June 18, 2013, 1:27pm UTC](https://www.ruby-forum.com/t/creating-ruby-script-exe/235378/1 "2013-06-18T13:27:00Z")

</div>

Hi All,

I have been using the gem ‘ocra’ to create .exe files for my ruby  
scripts. I run the following command:

ocra --console foo.rb

This creates the .exe but also runs the script once to create the .exe.  
I am facing an issue where i have a script which uses ‘file-tail’ to  
tail a log file. In this case when i run the above ‘ocra’ command, it  
does not create the .exe as the script runs and starts to watch the log  
file as it would normally do and hence ocra .exe creation does not  
complete.

Any thoughts how to avoid the above?  
Thanks

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [June 19, 2013, 12:56am UTC](https://www.ruby-forum.com/t/creating-ruby-script-exe/235378/2 "2013-06-19T00:56:07Z")

</div>

Looks like there’s an example in the docs:

You can detect whether OCRA is currently building your script by looking  
for the Ocra constant. If it is defined, OCRA is currenly building the  
executable from your script. For example, you can use this to avoid  
opening  
a GUI window when compiling executables:

app = MyApp.new  
if not defined?(Ocra)  
app.main\_loop  
end

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [June 19, 2013, 11:19am UTC](https://www.ruby-forum.com/t/creating-ruby-script-exe/235378/3 "2013-06-19T11:19:13Z")

</div>

Devin Christensen wrote in post #1112819:

> Looks like there’s an example in the docs:
> 
> You can detect whether OCRA is currently building your script by looking  
> for the Ocra constant. If it is defined, OCRA is currenly building the  
> executable from your script. For example, you can use this to avoid  
> opening  
> a GUI window when compiling executables:
> 
> app = MyApp.new  
> if not defined?(Ocra)  
> app.main\_loop  
> end

Hi David,  
I will write the above at the beginning of my script? after the require  
statements? or at the end?

Thanks

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [June 19, 2013, 11:56am UTC](https://www.ruby-forum.com/t/creating-ruby-script-exe/235378/4 "2013-06-19T11:56:55Z")

</div>

Ocra scans your script before it executes it anyway, but you might find  
you’ll need to actively use some gems for them to picked up properly.

Here’s an excerpt from a class I use with Ocra builds sometimes:

* * *

def initialize

# Allow EXE build without running.

if defined?(Ocra)  
# Let OCRA pick up the cookies from Mechanize.  
Mechanize.new.cookies  
# Quit so Ocra can do its thing.  
exit  
end

# Normal stuff goes here…

@agent = Mechanize.new

end

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [June 21, 2013, 4:08pm UTC](https://www.ruby-forum.com/t/creating-ruby-script-exe/235378/5 "2013-06-21T16:08:03Z")

</div>

That’s probably because you’re using an old version of Ruby: version  
1.9.2.

I noticed this message on a related error when googling the problem:  
“NOTE: Gem::Specification#installation\_path is deprecated, use base\_dir.  
It will be removed on or after 2011-10-01.”

You should probably install Ruby 1.9.3 or 2.0 and make sure you’re using  
the latest version of Ocra.

If you can’t upgrade your Ruby version for some reason, you’ll probably  
have to downgrade Ocra until you find a compatible version. Although  
that’ll undo all the bugfixes since that version was released…

FYI, --console is the default when building an “.rb” file. You don’t  
need to specify that.

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [June 21, 2013, 8:40pm UTC](https://www.ruby-forum.com/t/creating-ruby-script-exe/235378/6 "2013-06-21T20:40:22Z")

</div>

Joel P. wrote in post #1113153:

> That’s probably because you’re using an old version of Ruby: version  
> 1.9.2.
> 
> I noticed this message on a related error when googling the problem:  
> “NOTE: Gem::Specification#installation\_path is deprecated, use base\_dir.  
> It will be removed on or after 2011-10-01.”
> 
> You should probably install Ruby 1.9.3 or 2.0 and make sure you’re using  
> the latest version of Ocra.
> 
> If you can’t upgrade your Ruby version for some reason, you’ll probably  
> have to downgrade Ocra until you find a compatible version. Although  
> that’ll undo all the bugfixes since that version was released…
> 
> FYI, --console is the default when building an “.rb” file. You don’t  
> need to specify that.

Thanks for the info Joel.  
I upgraded ruby to 1.9.3p429. But now my gems dont work as they are in  
the /lib folder of old ruby installation. Could I import those with a  
command or i would need to re-install all of those gems again?

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [June 21, 2013, 12:24pm UTC](https://www.ruby-forum.com/t/creating-ruby-script-exe/235378/7 "2013-06-21T12:24:35Z")

</div>

Joel P. wrote in post #1112855:

> Ocra scans your script before it executes it anyway, but you might find  
> you’ll need to actively use some gems for them to picked up properly.
> 
> Here’s an excerpt from a class I use with Ocra builds sometimes:
> 
> * * *
> 
> def initialize
> 
> # Allow EXE build without running.
> 
> if defined?(Ocra)  
> # Let OCRA pick up the cookies from Mechanize.  
> Mechanize.new.cookies  
> # Quit so Ocra can do its thing.  
> exit  
> end
> 
> # Normal stuff goes here…
> 
> @agent = Mechanize.new
> 
> end

Hi Joel,All

I found a option for ocra:  
–no-dep-run using this ocra does not run my script and created .exe.  
This part is resolved.  
Now my script has some require statements as well for gems. So i created  
a bundler file using:  
bundle init  
Then added the following in my Gemfile:  
source “[https://rubygems.org](https://rubygems.org)”  
gem “file-tail”  
gem “pony”

I then ran the: bundle install which created a Gemfile.lock

Then finally i ran the following ocra command:  
C:code\>ocra --console --gemfile Gemfile --no-dep-run tailtest.rb

## Upon running it scans the Gemfile and then gives me error as below:

## === Scanning Gemfile C:/Ruby192/lib/ruby/site\_ruby/1.9.1/rubygems/specification.rb:1879:in `method_mi ssing': undefined method `installation\_path’ for #\<Gem::Specification:0x97b0e4 t ins-0.8.2\> (NoMethodError) from C:/Ruby192/lib/ruby/gems/1.9.1/gems/ocra-1.3.0/bin/ocra:557:in `blo ck in find_gem_files' from C:/Ruby192/lib/ruby/gems/1.9.1/gems/ocra-1.3.0/bin/ocra:556:in `fin d\_gem\_files’ from C:/Ruby192/lib/ruby/gems/1.9.1/gems/ocra-1.3.0/bin/ocra:701:in `bui ld_exe' from C:/Ruby192/lib/ruby/gems/1.9.1/gems/ocra-1.3.0/bin/ocra:1138:in `bl ock in \<top (required)\>’

My script has the following statements in the beginning:

require ‘rubygems’  
require ‘bundler/setup’  
Bundler.require

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [June 22, 2013, 5:01pm UTC](https://www.ruby-forum.com/t/creating-ruby-script-exe/235378/8 "2013-06-22T17:01:53Z")

</div>

Joel P. wrote in post #1113211:

> You could install them from the old gems (I think they’re in the cache  
> folder near the extracted gem folders), but it would be best to just  
> install the ones you need, thereby ensuring that you have the latest  
> versions.
> 
> Anyway, it’s easy to install them together:  
> gem install this\_gem this\_gem\_too and\_this\_gem

Hi Joel.  
Thanks for your help. Issue resolved.

Cheers

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [June 21, 2013, 10:17pm UTC](https://www.ruby-forum.com/t/creating-ruby-script-exe/235378/9 "2013-06-21T22:17:54Z")

</div>

You could install them from the old gems (I think they’re in the cache  
folder near the extracted gem folders), but it would be best to just  
install the ones you need, thereby ensuring that you have the latest  
versions.

Anyway, it’s easy to install them together:  
gem install this\_gem this\_gem\_too and\_this\_gem

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [June 25, 2013, 8:38pm UTC](https://www.ruby-forum.com/t/creating-ruby-script-exe/235378/10 "2013-06-25T20:38:09Z")

</div>

That’s a side-effect of using the command --no-dep-run

Here’s the code sample I suggested before, with an extra note:

def initialize

# Allow EXE build without running.

if defined?(Ocra)  
# Let OCRA pick up the cookies from Mechanize.  
Mechanize.new.cookies  
# Let OCRA pick up file-tail  
… something that uses file-tail here…  
# Quit so Ocra can do its thing.  
exit  
end

# Normal stuff goes here…

@agent = Mechanize.new

end

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [June 26, 2013, 7:04pm UTC](https://www.ruby-forum.com/t/creating-ruby-script-exe/235378/11 "2013-06-26T19:04:10Z")

</div>

Joel P. wrote in post #1113564:

> That’s a side-effect of using the command --no-dep-run
> 
> Here’s the code sample I suggested before, with an extra note:
> 
> def initialize
> 
> # Allow EXE build without running.
> 
> if defined?(Ocra)  
> # Let OCRA pick up the cookies from Mechanize.  
> Mechanize.new.cookies  
> # Let OCRA pick up file-tail  
> … something that uses file-tail here…  
> # Quit so Ocra can do its thing.  
> exit  
> end
> 
> # Normal stuff goes here…
> 
> @agent = Mechanize.new
> 
> end

Hi Joel,  
Thanks. your suggestion worked. But when i run the exe i am getting  
error:  
“uninitialized constant yaml” and the exe exits.  
I am using the YAML.load\_file method in my script to read from a yaml  
file

Any thoughts?

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [June 25, 2013, 8:20pm UTC](https://www.ruby-forum.com/t/creating-ruby-script-exe/235378/12 "2013-06-25T20:20:36Z")

</div>

Rochit S. wrote in post #1113269:

> Joel P. wrote in post #1113211:
> 
> > You could install them from the old gems (I think they’re in the cache  
> > folder near the extracted gem folders), but it would be best to just  
> > install the ones you need, thereby ensuring that you have the latest  
> > versions.
> > 
> > Anyway, it’s easy to install them together:  
> > gem install this\_gem this\_gem\_too and\_this\_gem
> 
> Hi Joel.  
> Thanks for your help. Issue resolved.
> 
> Cheers

Hi Joel,

Its seems there is till some issue. I had created an exe using the  
following command:

ocra --gemfile Gemfile --no-dep-run script.rb

This creates a script.exe file, also while creating script i successful  
messaging that the gems are being read from the Gemfile. When in run the  
exe, i get a error in loading one of the gems. In this case its  
file-tail gem. I have attached a screenshot of the error.  
What could be the issue?

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [June 26, 2013, 7:35pm UTC](https://www.ruby-forum.com/t/creating-ruby-script-exe/235378/13 "2013-06-26T19:35:50Z")

</div>

Am 26.06.2013 19:04, schrieb Rochit S.:

> > ```
> > Mechanize.new.cookies
> > 
> > ```
> 
> Hi Joel,  
> Thanks. your suggestion worked. But when i run the exe i am getting  
> error:  
> “uninitialized constant yaml” and the exe exits.  
> I am using the YAML.load\_file method in my script to read from a yaml  
> file

That would rather be

NameError: uninitialized constant YAML

You probably forgot to require ‘yaml’.

Regards,  
Marcus

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [June 26, 2013, 7:45pm UTC](https://www.ruby-forum.com/t/creating-ruby-script-exe/235378/14 "2013-06-26T19:45:47Z")

</div>

Am 26.06.2013 19:35, schrieb Rochit S.:

> Please ignore my previous email. Issue is resolved 🙂
> 
> Thanks for your help.

For future posts: good netiquette would be to also post  
the solution in your “never mind, I figured it out” email.

Regards,  
Marcus

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [June 26, 2013, 7:35pm UTC](https://www.ruby-forum.com/t/creating-ruby-script-exe/235378/15 "2013-06-26T19:35:30Z")

</div>

Rochit S. wrote in post #1113647:

> Joel P. wrote in post #1113564:
> 
> > That’s a side-effect of using the command --no-dep-run
> > 
> > Here’s the code sample I suggested before, with an extra note:
> > 
> > def initialize
> > 
> > # Allow EXE build without running.
> > 
> > if defined?(Ocra)  
> > # Let OCRA pick up the cookies from Mechanize.  
> > Mechanize.new.cookies  
> > # Let OCRA pick up file-tail  
> > … something that uses file-tail here…  
> > # Quit so Ocra can do its thing.  
> > exit  
> > end
> > 
> > # Normal stuff goes here…
> > 
> > @agent = Mechanize.new
> > 
> > end
> 
> Hi Joel,  
> Thanks. your suggestion worked. But when i run the exe i am getting  
> error:  
> “uninitialized constant yaml” and the exe exits.  
> I am using the YAML.load\_file method in my script to read from a yaml  
> file
> 
> Any thoughts?

Hi Joel,  
Please ignore my previous email. Issue is resolved 🙂

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![rochitsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rochitsen](https://www.ruby-forum.com/u/rochitsen)\
**Post date:** [June 26, 2013, 7:47pm UTC](https://www.ruby-forum.com/t/creating-ruby-script-exe/235378/16 "2013-06-26T19:47:26Z")

</div>

unknown wrote in post #1113652:

> Am 26.06.2013 19:35, schrieb Rochit S.:
> 
> > Please ignore my previous email. Issue is resolved 🙂
> > 
> > Thanks for your help.
> 
> For future posts: good netiquette would be to also post  
> the solution in your “never mind, I figured it out” email.
> 
> Regards,  
> Marcus

Agreed.. just being lazy @ midnight
