How to use Bundler with path specified Ruby version - ruby

I am on a VM (Lucid 64b) with a system Ruby version of 1.9.3p0.
I have a Ruby script that creates a .deb file -- The script needs to use Ruby 1.8.7 which I have installed in /foo/ruby/1.8.7.
There is an existing Gemfile to be used with Bundler
I can't use RVM and I can't install gems at the system level.
My .bashrc includes (and has been sourced)
export PATH=$PATH:/foo/ruby/1.8.7/bin
but ruby -v still gives me
ruby 1.9.3p0 (2011-10-30) [x86_64-linux]
Questions
How can I change the Ruby version for my user to use Ruby 1.8.7?
I've run: bundle install --path vendor/bundle
So in that directory (actually ./vendor/bundle/ruby/1.8/cache/gems) are all the gems I need but, when I run the Ruby script it doesn't find the required gems. I run the script like so /foo/ruby/1.8.7 script_to_gen_deb_file.rb
How can I get ruby to see/use the bundled gems?
Update
I was able to solve it. I needed to use
/foo/ruby1.8.7/bundle exec /foo/ruby1.8.7/ruby script_to_gen_deb_file.rb
I had tried this before, but I got an unrelated error and believed there was an environment problem.

Change your path so the special ruby gets precedence?
export PATH=/foo/ruby/1.8.7/bin:$PATH

Related

Local gem repository? Install a ruby gem without 'gem'?

Is it possibile to have something like a 'local' gem repository for Ruby?
I'm working on a custom Linux distribution without admin rights. Ruby is installed on the machine (v.1.8.7) but apparently no 'gem' or 'bundle' or whatever are installed. I need to use some Ruby gems like Nokogiri.
Is it possible to use Ruby gems without installing them through gem install?
Yes. Any gem can be used standalone. You just have to either download the source from github, or download the gem and extract its contents manually.
After you've done that you have to add the lib folder of the gem into the load path ($:) of Ruby. For example:
$:.unshift(File.expand_path("nokogiri-1.6.1/lib"))
require 'nokogiri'
Assuming you are running Ruby in the current directory and the Nokogiri source is in the folder nokogiri-1.6.1.
But remember that first you have to do the same with all Nokogiri prerequisites. I.e. all the libraries Nokogiri depends on.
Another option, which is what I would do, is to install RubyGems in your home directory. That can get a little bit tricky though, but it's possible by downloading the RubyGems package and running something like:
ruby setup.rb --prefix=/home/my_user_name/rubygems
Then you need to set up the ENV variables GEM_HOME and GEM_PATH to point to a location in your home directory where you want all your gems to be installed. See "What's the difference between GEM_HOME and GEM_PATH?" for a description of what they do.
You will also need to add something like /home/my_user_name/rubygems/bin to your PATH so that the gem command can be found by the shell.

How do I uninstall an old version of Ruby, and is it a wise thing to do?

When logged in as root and I type ruby -v centOS server reports 'ruby 1.9.3p392'. This is what I want.
But if I put rvmsudo ruby -v then I get 'ruby 1.8.7'. I do not want to use this older version, it is causing problems for my gitlab install.
I would like to remove it and make sure only v1.9 is used, how can I do this? Or should I leave it there but try and require certain users to use a different version?
Also, in case doing this messes anything up, is it possible to either;
a.see if anything on the server requires v1.8?
b.reverse the uninstall if it causes a problem?
Before uninstalling, take note of the patch level of 1.8.7 just in case. rvmsudo ruby -v should return something like ruby 1.8.7p234. The p#{num} is your patch level.
You should be able to uninstall 1.8.7 with rvm uninstall 1.8.7 (or possibly rvmsudo uninstall 1.8.7). This version of Ruby shouldn't be used by anything on the system other than code you've written, so it should only affect your applications and scripts.
The best way to tell what else would be using 1.8.7 is to look for scripts and crontabs that are owned by the user that rvm is running under. I'm not sure there's a tool that can evaluate it for you.
The best way to roll back in case of an emergency is rvm install 1.8.7-p#{num_from_above}. Alternatively, if you're on a platform like AWS or have rsync backups enabled, you might consider taking a snapshot that you can roll back to if you get in over your head.
Hope that's helpful!
Try setting default
$ rvm --default use 1.9.2
$ ruby -v
#ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0]
to remove you can use:
sudo apt-get remove ruby 1.8.7
Docs here & here
As you mentioned the ruby version output for root is ruby 1.9.3p392 in my understanding you will not have any problems.
Sure, you can have more than one version of ruby installed and find them under ~/.rvm/rubies. Also there you can find out the default version which is used. For more information you can have a look here: set default ruby where it explains how to set a default ruby and how to reset to the systems default.
You can uninstall cocoapods and install cocapods in the right directory.
sudo gem uninstall cocoapods
sudo gem install -n /usr/local/bin cocoapods

Why bundle exec rake does not run to proper version of ruby

I am currently having a strange issue with bundler and ruby.
if I type:
$ which ruby
I get:
/home/martinos/.rubies/1.8.7-p370/bin/ruby
And when I type:
$ which bundle
I get
/home/martinos/.gem/ruby/1.8.7/bin/bundle
But for some reason when I run
$ bundle exec rake db:migrate
The task is run with ruby 1.9.3 (I have written a puts RUBY_VERSION in environment.rb)
Any one as an idea why this happens?
Here is more infos:
When I type:
$ which rake
I get:
/home/martinos/.gem/ruby/1.8.7/bin/rake
But if I
$ head -1 `which rake`
I get:
#!/usr/bin/env ruby1.9.1
There are a variety of pieces that could be in play. The first is that it could be a conflict between your Ruby version management tools and your global gems. Meaning, I suppose it is possible that you only have a Rake version that can work on Ruby 1.9.1 that is in your global set. So when you fire up Rake it is forced to run in Ruby 1.9.1.
What you may want to do is create a directory specific gemset. If you're using RVM you can see the documentation on how to do that by looking at their Gemset documentation. Once that is in place with the Ruby version you want to test with, then do a gem install of Rake at the version that will work with that Ruby version. At that point you should find that the Ruby version being used to run Rake in that directory will be the same as the version you have running.
I apologize if this does not answer your question, or if you have thought of this approach already. Trying to wrap my head around this without the ability to reproduce the problem is a tricky deal.

custom_require.rb:36:in `require': no such file to load -- myapp(LoadError)

i get the above error, all i did was install rvm and update to ruby 1.9.2 from the default ruby that you get with mac.
all my gems appear in the gem list but do i need to specify a path somewhere?
cheers,
glenno.
Check that you have the gems installed gem list If not, install as usual gem install whatever or if you want a specific version gem install whatever -v 1.2.3 Or if you have bundler in your project (ie Rails), it's just bundle install
If you have all the gems, then this is probably a local file, and you're probably coming from Ruby version < 1.9, you need to either fix your $LOAD_PATH (which will depend on too many factors for me to explain here), or specify the full path to the file you want File.dirname(__FILE__) + '/myapp'
Otherwise, you need to give more info, like what version you used to be running, what myapp is, and where it is in relation to your file.
In my case it was rvm issue, I was using rvm 1.9 when i changed it to 1.8, migration worked.
rvm use 1.8
In my case I was converting to Cygwin64. The Cygwin directory name changed and Ruby upgraded to 1.9. After I fixed the Cygwin directory name, the second solution worked. Or at least got past this problem:
require File.dirname(FILE) + '/myapp.rb'

Ruby: How to include/install .bundle?

I'm new to Ruby. I have a .bundle file. I put it in the source folder and did
require('my.bundle')
But when I call the methods in the bundle, the definition is not found. Do I have to install them or include them in some other way to access them?
I am on Ruby version 1.8.7 (latest version on Mac).
I highly recommend using RVM to manage your Ruby installation, including your gems, so if you don't already have that, get it and follow the instructions for installing it. Make sure you do the part about modifying your bash startup script or you'll see weird behavior, like the wrong Ruby being called. Also, use the steps in "RVM and RubyGems" to install your gems or you can run into weird behavior with gems being installed under the wrong or an unexpected Ruby.
Second, use the gem command to install gems:
gem install gem_to_install
replacing "gem_to_install" with the name of the gem you want, and it will be installed into the appropriate gem folder for your Ruby.
If you are on Ruby 1.92, and trying to require a gem to use as a module in your code, use:
require 'gemname'
if it is installed via the gem command. And, if it is a module you wrote or have in your program's directory or below it, use:
require_relative 'path/to/gem/gemname'
If you are on a Ruby < 1.9 you'll also need to add require 'rubygems' above your other require lines, then use require './path/to/gem/gemname'.
Thanks, but my .bundle is not in gems. How do I install/require a .bundle file I already have?
If you wrote it look into rubygems/gemcutter or bundler for info on bundling and managing gems.
You can install a gem without using the app by going into the directory containing the gem and running setup.rb. See http://i.loveruby.net/en/projects/setup/doc/usage.html for a decent writeup or the official docs at: http://docs.rubygems.org/read/chapter/3

Resources