Dennis
Dennis

Reputation: 1156

Ruby 2.2.1 in Vagrant VM using Chef

I want to setup my new development environment for a rails app using vagrant and chef.

Goal

Clone repository (see below). Then run:

vagrant up
vagrant ssh

Finally running bundle install in app folder should work using installed ruby 2.1.2.

I already have gone through several tutorials that describe how to to this using various cookbooks and different strategies (rbenv, rvm, ruby-install, ruby-build). But although sometimes the machine could be provisioned correctly, I was never able to ssh into the machine and run bundle install without the following error message:

The program 'bundle' is currently not installed. To run 'bundle' please ask your
administrator to install the package 'bundler'

I will now present my most recent attempt based on this tutorial: http://www.gotealeaf.com/blog/chef-basics-for-rails-developers/

I have uploaded it to github here: https://github.com/denniske/vagrant-chef-ruby

After creating vagrant machine and provisioning and ssh into machine, I get the following:

vagrant@vagrant-ubuntu-trusty-64:~$ ruby -v
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]

vagrant@vagrant-ubuntu-trusty-64:~$ which ruby
/usr/bin/ruby

vagrant@vagrant-ubuntu-trusty-64:~$ bundle
Could not locate Gemfile or .bundle/ directory

vagrant@vagrant-ubuntu-trusty-64:~$ cd /usr/local/ruby/2.1.2/bin

vagrant@vagrant-ubuntu-trusty-64:/usr/local/ruby/2.1.2/bin$ ls
erb  gem  irb  rake  rdoc  ri  ruby  testrb

Problem 1: It seems the default ruby is the ruby used by chef for provisioning (1.9.3) which I do not want to use for my rails application.

bundler is successfully installed, but connected to default (wrong) ruby version.

Ruby 2.2.1 was installed into /usr/local/ruby/2.1.2.

Problem 2: By using some code like this in default.rb I seem to be able to make my ruby version the default, but then bundler does not work.

link "/usr/bin/ruby" do
  to "/usr/local/ruby/2.1.2/bin/ruby"
end

link "/usr/bin/gem" do
  to "/usr/local/ruby/2.1.2/bin/gem"
end

Does anybody has an idea how to fix this / can create a pull request to get this working?

Upvotes: 1

Views: 689

Answers (3)

Mr.
Mr.

Reputation: 10122

i will not specify the cookbook implementation itself, though what you will need to achieve is the following:

  1. install your favorite ruby version (including gem).
  2. update default ruby and gem symlinks, see: /usr/bin/ruby and /usr/bin/gem.
  3. install your favorite bundler version.

you can achieve what you want by implementing minitests (see minitest, and chef-minitest-handler)

Upvotes: 0

Dennis
Dennis

Reputation: 1156

I am sorry this is not really a direct answer to my original question, but I solved the problem another way and wanted to share it with you.

I found the website rove.io and chose Languages > Ruby > rbenv > 2.1.2, downloaded the package. Then I added the bundler gem to the chef.json config in the downloaded Vagrantfile:

chef.json = {
      :rbenv => {
        :user_installs => [
          {
            :user   => "vagrant",
            :rubies => [
              "2.0.0-p647"
            ],
            :global => "2.0.0-p647",
            'gems'    => {
              '2.0.0-p647'    => [
                {
                  'name'    => 'bundler',
                  'version' => '1.10'
                }
              ]
            }
          }
        ]
      }
    }

Then I created the vagrant machine and everything just worked as it should.

Note: I already tried using rvm and chef before with ubuntu 14. That did not work (due to some strange errors). But the ubuntu version used by the rove.io Vagrantfile works.

Upvotes: 1

Frederic Henri
Frederic Henri

Reputation: 53793

Seems there is an issue with the gem_package command.

I was able to make it work by replacing with an execute resource as

execute "install gem" do
  command "gem install bundler --no-rdoc --no-ri"
end

and remove

gem_package 'bundler' do
  options '--no-ri --no-rdoc'
#  gem_binary "/usr/local/ruby/2.1.2/bin/gem"
end

Upvotes: 0

Related Questions