Build and run ruby without installing it to system directories - ruby

I've cloned the ruby 1.8.7 source tree. I can build ruby. But I can't figure out how to run it without installing it in system directories. How can I do it?
Background: I want to use "git bisect" to figure out which build of Ruby introduced a new behavior in my code. I need to build and run ruby against a test program, multiple times, but I don't want to clobber the ruby that the Debian package installed.
Here's what I get if I try to run the ruby I built from source:
$ ./ruby -e 'puts RUBY_VERSION'
ÀÇ ÀÇ : ÀÇ ÀÇ : cannot open shared object file: No such file or directory - ÀÇ ÀÇ (LoadError)
I've also tried installing it, but not to the system directories, and got a stack trace:
$ ./configure --prefix=/home/wayne/tmp/ruby/installed --exec-prefix=/home/wayne/tmp/ruby/installed
$ make
$ make install
$ /home/wayne/tmp/ruby/installed/bin/ruby -e 'puts RUBY_VERSION'
/home/wayne/tmp/ruby/installed/lib/ruby/1.8/openssl/ssl.rb:26: uninitialized constant OpenSSL::SSL::VERIFY_PEER (NameError)
from /home/wayne/tmp/ruby/installed/lib/ruby/1.8/openssl.rb:23:in `require'
from /home/wayne/tmp/ruby/installed/lib/ruby/1.8/openssl.rb:23
I've got the feeling that I'm close, but a miss is as good as a mile.

rvm is a great tool. and should be able to take care of the heavy lifting for switching between different ruby version (or even sets of gems).
Installation is very easy:
$ gem install rvm && rvm-install
$ echo "if [[ ! -z $HOME/.rvm ]] ; then source $HOME/.rvm ; fi" >> ~/.bash_profile
Then to install a specific version & patch level:
rvm install ruby-1.8.7-p160
Then to switch between versions:
$ rvm 1.8.7-p160
$ ruby -v
ruby 1.8.7 (2009-06-12 patchlevel 160) [i686-darwin10.0.0]
$ rvm 1.8.7-p174
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin10.0.0]

When I need to do something like that, I use the chroot command. Create a temporary directory, install ruby into a sbin subdirectory, and chroot into the temp folder. Depending on what you are testing, you may also have to copy some system libraries into the temporary directory tree (before you chroot in).

It's probably easier to use rvm to test your app against different rubies.

Related

Homebrew install Ruby keg-only can't find gem

How do I get irb to work after installing Ruby with Homebrew?
When I try to run irb, I get an error:
$ irb
Traceback (most recent call last):
2: from /usr/local/opt/ruby/bin/irb:23:in `<main>'
1: from /usr/local/lib/ruby/site_ruby/2.6.0/rubygems.rb:302:in `activate_bin_path'
/usr/local/lib/ruby/site_ruby/2.6.0/rubygems.rb:283:in `find_spec_for_exe': can't find gem irb (>= 0.a) with executable irb (Gem::GemNotFoundException)
I tried:
$ brew link ruby
Warning: Refusing to link macOS-provided software: ruby
If you need to have ruby first in your PATH run:
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.bash_profile
For compilers to find ruby you may need to set:
export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"
I have the lines below at the top of my /etc/paths file:
/usr/local/bin
/usr/local/opt/ruby/bin
/usr/local/lib/ruby/gems/2.6.0/bin
irb doesn't show up in the output of gem list, but:
$ find /usr/local -name irb
/usr/local/lib/ruby/2.6.0/irb
/usr/local/Cellar/ruby/2.6.0_1/bin/irb
/usr/local/Cellar/ruby/2.6.0_1/lib/ruby/2.6.0/irb
/usr/local/Cellar/ruby/2.6.0_1/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb
/usr/local/Cellar/ruby/2.6.0_1/share/ri/2.6.0/system/lib/irb
I'm also having a similar issue with ri & rdoc.
Run: gem install irb and you now good to go.
Assuming you're using Homebrew Ruby...
The irb executable is located at:
/usr/local/opt/ruby/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb
You can use that line directly, symlink it into your $PATH, alias it, or whatever.
Alternatively, you may patch /usr/local/opt/ruby/bin/irb around line 22.
# patch
class Gem::BasicSpecification
def self.default_specifications_dir
File.join(Gem.private_dir, "specifications", "default")
end
end
# /patch
# Next line looks like this. Don't change this.
# if Gem.respond_to?(:activate_bin_path)
You may do the same in /usr/local/opt/ruby/bin/ri and /usr/local/opt/ruby/bin/rdoc to patch those commands as well.
Why?
See https://github.com/Homebrew/homebrew-core/blob/955497722b9bf65069957b0e7c903b96939cdd99/Formula/ruby.rb#L112
The Homebrew Ruby formula assumes all gems will be installed in the "global gem directory" /usr/local/lib/ruby/gems/2.6.0/. So when you uninstall-reinstall Homebrew Ruby, the gems stick around - you don't have to re-install them as well (kinda annoying since I have gems installed for Ruby versions I don't even have installed anymore, but that's another issue).
But Ruby's default gems aren't in the global gem dir. They're inside the Ruby installation dir (what the Homebrew formula refers to as the private_dir) : /usr/local/opt/ruby/lib/ruby/gems/2.6.0/.
So Homebrew Ruby can't find them.
Homebrew patches Rubygems, so this snippet patches Rubygems again, but deeper. You can also patch-patch like this:
module Gem
def self.default_dir
private_dir
end
end
But default_dir is used in other places and I didn't want to break anything.

How to run logstash from exploded jar with JRuby

I'm not very comfortable with jruby and rvm environments, so I have trouble trying to run logstash with it (it is said to be the fastest way to do so).
What I've tryed :
I installed rvm environement (as a side note, the standard install scripts create a new .bash_profile, which disable the standard .bashrc script when you're on Ubuntu. This can confuse new users like me... I was wondering for several hours why my 'll' alias won't work !)
I uncompressed logstash (jar xvf /logstash-1.1.4-monolithic.jar) in a new directory
I've followed the instructions in https://rvm.io/rvm/install/ as rvm install 1.9.2 (albeit I've also made some try/error/redo manipulations, so I can't remember the exact listing of commands)
Then I tried several commands, but none of them worked :
$ ruby logstash.rb
/home/orabig/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- logstash/agent (LoadError)
from /home/orabig/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from logstash.rb:1:in `<main>'
whereas the following line is returning after some seconds :
jruby-1.7.0 logstash.rb agent -f ../etc/inout.config -v
(the inout.config file is working when invoked with the java commandline as in :)
java -jar (...)/logstash-1.1.4-monolithic.jar agent -f ../etc/inout.config -v
So I'm basically stuck now, because the more I will try random things, the more I'm afraid to mess up my configuration...
Update : On the logstash page (https://github.com/logstash/logstash), it looks like it's working with 1.6.8 ruby version (however it's not clear, and I'm not sure if it makes any difference...)
So I also tried :
$ rvm install 1.6.8
(...)
$ rvm use 1.6.8
$ export JRUBY_OPTS=--1.9
$ ruby logstash.rb agent -f ../etc/inout.config -v
$ jruby-1.6.8 logstash.rb agent -f ../etc/inout.config -v
but nothing happens when I run either of the last two lines...
As the rvm tool will let you know: You have to be in a login shell to use rvm like that.
So, either run bash -login or use rvm to select and run your version of ruby like done in the following:
rvm install 1.7.0 # note that 1.7.0 will also work
export JRUBY_OPTS=--1.9
rvm 1.7.0 do bundle install
rvm 1.7.0 do ruby lib/logstash/runner.rb agent -f ../etc/inout.config -v
This will tell rvm to use jruby-1.7.0 to run your logstash agent.

How to use Bundler with path specified Ruby version

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

Trouble on uninstalling RVM

I am running Ubuntu 10.04 Lucid and I was probably wrong to uninstall RVM and now it seams to be not completely removed from the system. Now I would like to install that again but I am in trouble.
What happens is that I have strange outputs generated when I try to execute some command lines:
# Note that following command lines don't output the version
# (between the word "version" and the word "is") as it should be.
$ rvm
$ rvm -v
> -bash: /usr/local/rvm/scripts/base: No such file or directory
> A RVM version is installed yet 1.10.3 is loaded.
> Please do one of the following:
> * 'rvm reload'
> * open a new shell
# Even if I use the 'cd' command I have problems...
$ cd ..
> -bash: /usr/local/rvm/scripts/initialize: No such file or directory
> -bash: /usr/local/rvm/scripts/hook: No such file or directory
That probably means that RVM is not correctly removed even if Ruby seams to be running as well:
$ which ruby
/usr/local/bin/ruby
$ ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]
So, what I can do to completely remove/uninstall RVM?
UPDATE
I solved part of the problem (or, maybe, all problems): that (those) was related to Phusion Passenger that was still loading the old path related to the Ruby version managed by RVM.
first try echo $rvm_path - if that shows anything - rm -rf it
second:
sudo rm -rf /ust/local/rvm /etc/rvmrc /etc/profile.d/rvm.sh ~/.rvm*
next check ls -l /usr/local/bin | grep rvm this will show links to rvm - rm -rf them
and last which rvm - if it shows anything try to check this path and remove at least this file.
RVM installs everything into ~/.rvm. To remove RVM from your system run 'rm -rf ~/.rvm'.
Refer this. and where is everything installed to?
From rvm help:
implode - (seppuku) removes the rvm installation completely.
This means everything in $rvm_path (~/.rvm || /usr/local/rvm).
This does not touch your profiles. However, this means that you
must manually clean up your profiles and remove the lines which source RVM.
Judging from your $PATH you can also (alternatively) remove all of the following files and directories:
/usr/local/rvm
/etc/rvmrc
/etc/profile.d/rvm.sh

Ruby cannot find required libraries even though gem is installed

I have spent literally days trying to install ruby 1.9.2 and get it working with gems :-/ I eventually gave up on my Mac OSX 10.6 machine and below is the current state on my Ubuntu machine. Any advice would be greatly appreciated!
# ruby test.rb
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- mongo (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from test.rb:1:in `<main>'
# cat test.rb
require 'mongo'
db = Mongo::Connection.new.db("mydb")
# gem which mongo
/usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongo-1.1.2/lib/mongo.rb
# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.10
DISTRIB_CODENAME=maverick
DISTRIB_DESCRIPTION="Ubuntu 10.10"
According to this page: http://docs.rubygems.org/read/chapter/19
I symlinked which ruby I was using to match that which gem is using:
# which ruby
/usr/local/rvm/bin/ruby
# ls -l `which ruby`
lrwxrwxrwx 1 root root 44 2010-11-17 13:25 /usr/local/rvm/bin/ruby -> /usr/local/rvm/rubies/ruby-1.9.2-p0/bin/ruby
# gem env | grep 'RUBY EXECUTABLE'
- RUBY EXECUTABLE: /usr/local/rvm/rubies/ruby-1.9.2-p0/bin/ruby
# which gem
/usr/local/rvm/bin/gem
# gem -v
1.3.7
# ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]
Try putting the following line at the beginning
require "rubygems"
Why is "rvm" displaying in your /usr/local/rvm/ path? Did you do a system-wide install, as a system administrator using administering Ruby system wide for multiple users?
Did you add [[ -s '/usr/local/lib/rvm' ]] && source '/usr/local/lib/rvm' to your ~/.bashrc, ~/.bash_profile or ~/.profile (whichever you have configured)?
For normal, every day use, I recommend RVM's default setup:
RVM installation, RVM gems management.
Note to self: Buy stock in RVM. It's too cool.
Does it work under Ruby 1.8.7, which is pre-installed by default on OS X?
If so, one difference between 1.9.1 and 1.9.2 is that "." isn't part of $:'s path any more.
I recommend that you do rvm implode and delete the current setup. Then use the railsready script to setup RVM and Ruby properly for you on Ubuntu. It's important to understand that until you know what you are doing you should run the script as a user. Hope that helps.
On linux and OS X, I have always had to put require "rubygems" in the beginning. However it has always worked fine without this line on windows.

Resources