config.vm.provision "shell",path: "script/provision-script.sh"
提供脚本做了很多事情,但是最终应该安装rbenv Ruby版本控制,然后使用rbenv来安装Ruby 2.2.1.条款脚本的这一部分如下所示:
echo "setting up rbenv" # execute the remaining commands as vagrant user,instead of root sudo -H -u vagrant bash -c "git clone https://github.com/sstephenson/rbenv.git ~vagrant/.rbenv" sudo -H -u vagrant bash -c "git clone https://github.com/sstephenson/ruby-build.git ~vagrant/.rbenv/plugins/ruby-build" sudo -H -u vagrant bash -c "git clone https://github.com/sstephenson/rbenv-gem-rehash.git ~vagrant/.rbenv/plugins/rbenv-gem-rehash" echo "setting up rbenv environment in bash" echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~vagrant/.bashrc echo 'eval "$(rbenv init -)"' >> ~vagrant/.bashrc # start new vagrant shell so rbenv will work echo "building ruby" su vagrant rbenv install 2.2.1 && rbenv global 2.2.1 && rbenv rehash && cd /path/to/my/app && gem install bundler rake && rbenv rehash && bundle && rbenv rehash
一切到rbenv安装…部分正常工作.安装ruby失败,并显示以下错误:
==> default: setting up rbenv ==> default: Cloning into '/home/vagrant/.rbenv'... ==> default: Cloning into '/home/vagrant/.rbenv/plugins/ruby-build'... ==> default: Cloning into '/home/vagrant/.rbenv/plugins/rbenv-gem-rehash'... ==> default: setting up rbenv environment in bash ==> default: building ruby ==> default: /tmp/vagrant-shell: line 73: rbenv: command not found
然后脚本完成.我可以用vagrant ssh打开vm,然后成功运行rbenv install 2.2.1,所以我猜测在配置过程中,一个新的vagrant shell并没有被启动.我的印象是,这应该是在rbenv安装之前发生的su vagrant 2.2.1.
我可以做些什么来确保在此配置期间初始化新的shell,并且rbenv命令将工作?
解决方法
==> default: /tmp/vagrant-shell: line 10: rbenv: command not found
首先,了解vagrant配置脚本是以sudo模式运行非常重要的.
所以,当进入脚本时,我们引用〜/ path,我们引用/ root / path,而不是/ home / vagrant / path.
问题是我正在为root用户安装rbenv,并尝试从vagrant用户调用rbenv命令,当然它没有工作!
所以我做的是指定在无sudo用户添加特权的情况下运行配置者:false:
config.vm.provision :shell,privileged: false,inline: $script
然后进入我的脚本,我正在考虑一切都是从流氓用户呼叫的.
这里@Casper的答案帮了我很多,因为它只能指定:
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.
在我最后的Vagrantfile下面.
# -*- mode: ruby -*- # vi: set ft=ruby : $script = <<SCRIPT sudo apt-get -y update sudo apt-get -y install curl git-core python-software-properties ruby-dev libpq-dev build-essential Nginx libsqlite3-0 libsqlite3-dev libxml2 libxml2-dev libxslt1-dev nodejs postgresql postgresql-contrib imagemagick git clone https://github.com/sstephenson/rbenv.git ~/.rbenv echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc echo 'eval "$(rbenv init -)"' >> ~/.bashrc source ~/.bashrc git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build sudo -H -u vagrant bash -i -c 'rbenv install 2.1.3' sudo -H -u vagrant bash -i -c 'rbenv rehash' sudo -H -u vagrant bash -i -c 'rbenv global 2.1.3' sudo -H -u vagrant bash -i -c 'gem install bundler --no-ri --no-rdoc' sudo -H -u vagrant bash -i -c 'rbenv rehash' sudo -u postgres createdb --locale en_US.utf8 --encoding UTF8 --template template0 development echo "ALTER USER postgres WITH PASSWORD \'develop\';" | sudo -u postgres psql SCRIPT VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.Box = "hashicorp/precise64" config.vm.network "forwarded_port",guest: 3000,host: 3000 # config.vm.provider :virtualBox do |vb| # vb.customize ["modifyvm",:id,"--memory","1024"] # end config.vm.provision :shell,inline: $script end
希望对别人有帮助.