vagrant will not use rbenv ruby version - ruby

I'm using the Vagrant box:
https://dl.dropbox.com/u/14292474/vagrantboxes/precise64-ruby-1.9.3-p194.box
It has ruby1.9.3 installed under rbenv. I'm trying to test a rails app and in the chef-solo provisioner I have (among other things), this block:
execute 'install' do
action :run
command "bundle install --path #{node.app_name.lib_path}/vendor/bundle --binstubs"
cwd node.app_name.path
end
rbenv is installed under the vagrant user and instructed to use 1.9.3 globally. For some reason, when bundler is run during vagrant up it still uses 1.8.7 which is only found in /opt/vagrant_ruby.
How can I tell vagrant that I want to use the rbenv ruby to perform all tasks. Do I need to pass something in the env option of the execute block?

First start to get the path of bundle in your server:
$ which bundle
/opt/rbenv/shims/bundle
Then, knowing that in chef when you included the rbenv cookbook, the constant RBENV_ROOT is equal to /opt/rbenv, modify your command like this:
command "#{RBENV_ROOT}/shims/bundle install --path #{node.app_name.lib_path}/vendor/bundle --binstubs"
So you ensure to use the correct version of bundle when installing.
A second thing you can do is to add the path for rbenv (which can be located in /etc/profile.d/rbenv but that depends on your installation) on top of the ~/.bashrc of the user you use with chef-solo
For example, if you are using a use "deployer", edit /home/deployer/.bashrc to have on top of the file:
#Rbenv path for remote shells
source /etc/profile.d/rbenv.sh # <--- adapt this to your installation
# If not running interactively, don't do anything
[ -z "$PS1" ] && return # <--- remote shell (ssh connection) will stop at this line

Related

Why does ruby 2.4.0 always try to install when i enter this directory?

Every time I enter into this specific directory, I get a message that ruby-2.4.0 is not installed. I'm wondering if I set something to run a command when I enter this directory, but I'm having trouble locating where it may be doing this so that I can not have it run anything when i go into this folder.
% cd omnibus
Required ruby-2.4.0 is not installed.
To install do: 'rvm install "ruby-2.4.0"'
It looks like you have RVM installed on that machine. RVM adds a hook to your shell that detects when you change into a directory with a configuration file specifying what Ruby version to use.
You can run ls -la in your omnibus directory to look for that configuration file. It is probably named .ruby-version.

How to use Ruby CGI scripts on Uberspace

I want to use Ruby CGI scripts on an Uberspace 7, but ran into several issues with permissions and security settings, in particular when using gems. How do I install CGI scripts with custom gems?
First, note that Uberspace 7 runs on SELinux. This means that CGI script files in ~/html/ not only have to be executable but also need the correct SELinux context. In this case, the type must be httpd_sys_content_t.
You can view the SELinux context with ls -lZ:
$ ls -Z file1
-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1
If some files have the wrong context, the context can be restored with the restorecon command, e.g. restorecon -R ~/html/.
The user installation directory for Ruby gems is ~/.gem/. On Uberspace, gem install installs into that directory by default:
$ cat /etc/gemrc
gem: --no-document --user-install
As the home directory cannot be accessed by the apache process, gems installed there cannot be executed from CGI scripts. You can install gems in /var/www/virtual/$USER/gem instead, create the directory with
$ mkdir /var/www/virtual/$USER/gem
You cannot use the --install-dir parameter for gem install directly as this conflicts with the default parameters mentioned above:
$ gem install mygem --install-dir /var/www/virtual/$USER/gem
ERROR: Use --install-dir or --user-install but not both
Instead, create ~/.gemrc with the following content to override the default parameters (replace <USERNAME> with your actual user name):
gem: --install-dir /var/www/virtual/<USERNAME>/gem
Now the installation of gems should work:
$ gem install mygem
To use the gems in CGI scripts, set the Gem.paths variable before requiring gems:
#!/usr/bin/ruby
Gem.paths = { 'GEM_PATH' => '/var/www/virtual/<USERNAME>/gem' }
require 'mygem'
(... rest of the script)
This is needed as we cannot modify the environment variables (i.e. set GEM_PATH) for the apache process.

Run shell command from Ruby without it prefixing gem directory to my $PATH

I'm attempting to run Vagrant via a Ruby script by shelling out via backticks
`vagrant up`
This doesn't work when run through Ruby because the shell has a modified $PATH env var prefixed with the path to my gems (/opt/local/lib/ruby1.9/gems/1.9.1/bin). This means that its trying to invoke vagrant from the installed gem instead of the installed version in /usr/bin.
My Ruby runtime has the system gem path prepended because my rakefile is requiring 'bundler/setup'. It turns out Bundler is modifying my PATH environment variable.
it's not pretty, but set Path=#{ENV['Path']}&&vagrant up

Not able to install a gem file?

Hi People I am trying to install a gem file through command prompt on MAC using the following command "gem install filename" but whenever I try to execute the command I get the following error
WARNING: Installing to ~/.gem since /Library/Ruby/Gems/1.8 and
/usr/bin aren't both writable.
WARNING: You don't have /Users/hemmanshu/.gem/ruby/1.8/bin in your PATH,
gem executables will not run.
ERROR: Error installing frank-cucumber-0.8.14:
cucumber requires gherkin (~> 2.10.0, runtime)
every time I try to execute this command the same thing pops up.
You need to install gems with superuser privileges so you should to use
sudo gem install [gemname]
instead of
gem install [gemname]
U try with sudo. like "sudo gem install filename"
...Most of the time this is a PATH issue, Suppose you are using zshell as me, then add this line at the end of ~/.zshrc file
export PATH="$PATH:$HOME/.gem/ruby/2.1.0/bin"
If you are using bash as default shell then add this line to end of ~/.bashrc file
export PATH="$PATH:$HOME/.gem/ruby/2.1.0/bin"
For your case it will be like this:
export PATH="$PATH:$HOME/.gem/ruby/1.8/bin
Lastly don't forget to reload the shell by source ~/.zshrc incase of z-shell or source ~/.bashrc incase of bash-shell. I think this helps someone.

Unable to run sh script from remote computer

I'm trying to run the following script from a remote computer on a Linux computer with RVM:
plink.exe -pw root#< Linux ip> /root/script.sh
ERROR: Error installing /usr/local/rvm/gems/ruby-1.9.2-p290/cache/bundler-1.0.22.gem: bundler requires RubyGems version >= 1.3.6
script.sh is a script that runs ruby script.
I think that the problem is caused by some collusion with the RVM or bundler gems on the remote Linux.
Can someone advise?
You need to make sure that the script you are running is using the correct rvm environment. It might be running under the System ruby. Try running a much simpler script that outputs "ruby ---version" and/or "which ruby" first
Edit:
In your bash script, you might need to do something like:
[[ -s "/usr/local/rvm/scripts/rvm" ]] && . "/usr/local/rvm/scripts/rvm" # Load RVM function
rvm use 1.9.2

Resources