简体   繁体   中英

How to move simple ruby script onto another server without knowing if gem is installed on that server?

I have a simple Ruby script that require Mail gem. It works fine on my local laptop. If I want to deploy the script on a server. What to do to make sure the script can run successfully on the server? I'm thinking about "bundle exec ruby sample.rb" but it gives "Could not locate Gemfile or .bundle/ directory error.

New to programming. No Rails just simple Rb script

You need to install the mail gem on the server. Open your shell and type: gem install nameofgem

Your script should work then.

I've also seen that you can copy the entire ruby directory to other computers and that works too. I'm not sure about newer versions of Ruby, but I have done this in the past.

This is a common problem with scripting languages, how to create a release binary that is self sufficient or at least has a minimal number of pre-installed dependencies.

Omnibus package

The chef guys created the very useful omnibus packager to solve this problem. This bundles an application with a specific version of ruby and any associated gems.

Docker

A simpler option might be to deploy your scripts a docker container.

https://www.docker.com/

The following is an example building a container using a ruby script:

rvm

A final option is to use a tool like rvm on your target machine (Although this is really a tool tor managing development machines).

$ rvm use ruby-2.1.5@mygemset --ruby-version --create
ruby-2.1.5 - #gemset created /home/mark/.rvm/gems/ruby-2.1.5@mygemset
ruby-2.1.5 - #generating mygemset wrappers..........
Using /home/mark/.rvm/gems/ruby-2.1.5 with gemset mygemset

$ ls -a
.ruby-version
.ruby-gemset  

Rvm enables you to switch between ruby versons and collections of gems called gemsets.

The simplest way without bundler would be to require the gem then rescue if it is not installed. Note the tick orientation.

begin
  require 'gemname'
rescue LoadError
  `gem install gemname`
end

require 'gemname'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM