19

I have been trying to find a good solution to this for a long time:

Is there a sure-fire way to install ruby to a folder which is portable on that platform? I want to have a folder which I can easily copy into a distribution I am making so I can have a ruby environment "on the go". It is fine if I need to compile the source and stuff, as long as I end up with a portable ruby install.

I have found a few resources which tries to solve this, but none are satisfactory for me.

Portable Ruby on Rails environment

http://hcettech.blogspot.pt/2012/05/windows-portable-rails-development.html

For me this is a major pain-point of Ruby. If it is not possible to get a Ruby script to work without having the clients install Ruby first, then ruby is essentially no good in my case. I truly hope I/we can figure this out so it is possible to have Ruby be lots more useful than what it already is.


Note: I know things like Releasy exists, but it's still not a simple ruby executable.

My goal is to be able to make a folder with a .bat/.sh script, which does this:

#some bat/sh
./bin/ruby ./my_script.rb

And yes, shoot me down if there is something about this problem I have not understood, as it would be a relief for me.

Community
  • 1
  • 1
Automatico
  • 12,420
  • 9
  • 82
  • 110

3 Answers3

22

I got so tired of this that I decided to make my own solution to it. After a couple of days I finally got this working.


I present: Ruby Ship

It's just what I needed, and hopefully someone else will get the benefit of it too!

It's really simple: Download the repo (which is quite big because of the binaries). Put it on a usb-drive, or wherever you want it, then do:

path/to/ruby_ship/bin/ruby_ship.sh -v
=> ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]

That's for linux and mac, or on windows:

path\to\ruby_ship\bin\ruby_ship.bat -v
=> ruby 2.1.2p95 (2014-05-08 revision 45877) [i386-mswin32_100]

The ruby_ship wrapper acts just as your normal ruby, so you can pass in a script, or do anything you would use the normal ruby command for.

If you don't like the version that comes bundled with Ruby Ship, then simply use the compiling-script that is in the tools directory. Download the source code of you favourite version of Ruby, say X.Y.Z, then do this:

# *nix (osx/linux/...)
path/to/ruby_ship/tools/ruby_ship_build.sh path/to/ruby-X.Y.Z.tar.gz

# windows
path\to\ruby_ship\tools\ruby_ship_build.bat path\to\ruby-X.Y.Z.tar.gz

This will build your flavour of ruby and set up the wrappers correctly. Now you Ruby version X.Y.Z is portable! Copy paste this folder where you want it and you are ready to go! Remember though, it will only compile for your current platform, so if you need other platforms, go compile it on those platforms as well.

The compiling process has a few requirements. Look at the github site.

Anyways, hope that others might find this useful! If things are not quite working for you, please make an issue and I will look into it!

Automatico
  • 12,420
  • 9
  • 82
  • 110
  • 2
    This is totally awesome Stephan! Have been looking for this exact thing for a ruby project I've built (and would like to distribute) - thanks for putting it together (it works perfectly). I've a bunch of thoughts on things to put on the to-do list, but I'll comment on Github. Thanks again. – nlh Aug 15 '14 at 21:06
  • @nlh Good to hear that others have had the same need! Feel free to help with the development. Currently the windows version is bugged because I have been away from my windows machine for months now, so I have not been able to implement the newest things there. – Automatico Aug 19 '14 at 08:38
2

To clarify my understanding of your problem, you want to be able to package Ruby alongside your script, all contained within a single directory. Something like:

.
├── bin
│   └── ruby
├── my_script.bat
├── my_script.sh
└── my_script.rb

You want this to be work on multiple platforms - you cite batch and shell scripts as possibilities, so I'm assuming both *NIX and Windows are potential targets. This would mean shipping multiple platform-specific Ruby binaries.

Also, you mention gems that don't support JRuby, so it's possible you're using gems with native extensions.

There is no tool that satisfies all of those requirements :(

If you were willing to forgo the use of native extensions, and build a package per target platform, you could:

  • build a self-contained Windows distributable with Ocra
  • build a Debian package with pkgr
  • build a standard OSX installer that relies upon the system Ruby in OSX (although some question that idea)

Needless to say this will involve a fair amount of overhead, especially if you want to fully automate those steps and test them as part of CI (which I'd recommend doing as a smoke test at the end of a full CI build).

Duncan Bayne
  • 3,870
  • 4
  • 39
  • 64
  • Maybe I explained it a bit cumbersome. Your understanding is correct though. It would be ideal to have one Ruby install, but I was actually thinking that this folder had a compiled ruby install for each platform. So it would be quite big, but would be truly portable. It could then have a win32.bat for windows, unix.sh etc. What I really need is some way to invoke a ruby script from the command line without installing ruby first, and have it be MRI. – Automatico May 26 '14 at 10:36
  • @Cort3z - can you share some more information about the project? What Gems do you require? What version of Ruby? – Duncan Bayne May 27 '14 at 05:08
  • I am working on a project where I want to have a html5 front-end for a rubygem I am developing. It uses very many gems for many different things. The gem is essentially the controller of my application. I am currently using TideSDK, but as I am having all kinds of problems with that, I am considering migrating to atom-shell or node_webkit. If I do that I need a ruby environment I can bundle with the project so I can do a system call from the framework (atom/node) and start my gem, which will run on it's own. I just found that on windows you can simply copy the install dir, and it totaly works! – Automatico May 27 '14 at 09:51
  • 1
    So I am determined to fix this problem. I found some temporary hacks that will work. You can check it out [ruby_ship](https://github.com/stephan-nordnes-eriksen/ruby_ship). My plan is to have a install script that compiles whatever source you want into a folder and have a small script as a layer between the ruby install and the actual binaries so it is truly portable. – Automatico May 27 '14 at 10:53
1

Old post, but you can also check my solution rb2exe. It turns ruby applications into single, portable, stand-alone executables.

It supports Rails and Gemfile.

Install / usage:

gem install rb2exe

echo "puts 'Hello world'" > test.rb
rb2exe test.rb
./test

I explain what it does, step-by-step, here:

http://www.learnwithdaniel.com/2016/08/ruby-to-portable-exe-app/

Daniel Loureiro
  • 4,595
  • 34
  • 48