Vagrant/VirtualBox VM provisioning: rbenv installs successfully but subsequent uses in script fail

前端 未结 3 971
南笙
南笙 2020-12-25 08:56

I am using Vagrant + VirtualBox to set up a virtual machine for my Rails app. I am working on cleaning up a .sh provisioning script that is referenced in

3条回答
  •  -上瘾入骨i
    2020-12-25 09:21

    I had a similar problem because I was trying to install rbenv and the vagrant provisioning was giving me the error:

    ==> default: /tmp/vagrant-shell: line 10: rbenv: command not found
    

    First of all, it is very important to understand that vagrant provisioning script is running in sudo mode. So, when in the script we refer to ~/ path, we are referring to /root/ path and not to /home/vagrant/ path. The problem is that I was installing rbenv for the root user and after trying to call rbenv command from a vagrant user and, of course, it didn't work!

    So, what I did is specify the vagrant to run the provisioner NOT in sudo user, adding privileged: false:

    config.vm.provision :shell, privileged: false, inline: $script
    

    Then in my script I considered everything as being called from the vagrant user. Here @Casper answer helped me a lot, because it works only specifying: sudo -H -u vagrant bash -i -c '......'

    Since you just updated .bashrc with a new path and other settings, you will want to run "sudo bash" with the -i option. This will force bash to simulate an interactive login shell, and therefore read .bashrc and load the correct path for rbenv.

    Below is my final Vagrantfile.

    # -*- mode: ruby -*-
    # vi: set ft=ruby :
    
    $script = <
    
                                     
                  
提交回复
热议问题