Vagrant / VirtualBox VM配置:rbenv安装成功但后续脚本使用失败

时间:2015-03-18 21:18:50

标签: ruby-on-rails ruby ubuntu vagrant virtualbox

我正在使用Vagrant + VirtualBox为我的Rails应用程序设置虚拟机。我正在努力清理.shVagrantfile中引用的config.vm.provision "shell", path: "script/provision-script.sh" 配置脚本,如下所示:

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版本,然后使用rbenv来安装Ruby 2.2.1。配置脚本的那一部分如下所示:

rbenv install...

==> 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 部分之前的所有内容都能正常运行。安装ruby失败,并出现以下错误:

vagrant ssh

然后脚本完成。我可以使用rbenv install 2.2.1打开vm然后成功运行su vagrant,因此我猜测在配置期间实际上没有启动新的流浪汉shell。我认为这应该在rbenv install 2.2.1之前rbenv发生。

如何确保在此配置期间初始化新shell并且{{1}}命令可以正常工作?

3 个答案:

答案 0 :(得分:23)

我有类似的问题,因为我试图安装rbenv 并且流浪汉配置给了我错误:

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

首先要了解vagrant配置脚本是否以sudo模式运行非常重要。 因此,当进入脚本时我们引用〜/ path,我们引用/ root / path而不是/ home / vagrant / path。 问题是我正在为root用户安装rbenv,并且在尝试从vagrant用户调用rbenv命令后,当然它没有用!

所以我所做的是指定vagrant在NOT sudo用户中运行配置器添加特权:false

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

然后在我的脚本中,我正在考虑从流浪者用户调用所有内容。 这里@Casper的答案帮助了我很多,因为它只能指定: sudo -H -u vagrant bash -i -c'......'

  

由于您刚刚使用新路径和其他设置更新了.bashrc,所以   将要使用-i选项运行“sudo bash”。这将迫使bash   模拟交互式登录shell,因此读取.bashrc和   加载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, privileged: false, inline: $script
end

希望它对其他人有所帮助。

答案 1 :(得分:3)

我对配置脚本退出感到惊讶,因为运行su vagrant理论上应该在此时挂起脚本(您正在运行通常不会退出的命令su本身)。

问题是你不能改变正在运行shell脚本的用户&#34;在运行中&#34;通过运行su
您唯一的选择是使用sudo

由于您刚刚使用新路径和其他设置更新了.bashrc,因此您需要使用"sudo bash"选项运行-i。这将强制bash模拟交互式登录shell,因此请阅读.bashrc并为rbenv加载正确的路径。

所以,这样的事情应该有用:

echo "building ruby"
sudo -H -u vagrant bash -i -c 'rbenv install 2.2.1 ...'

答案 2 :(得分:0)

还有另一种解决方案,可以在Vagrant Provisioning流程中安装rbenv,而不是@Casper@Diego D解决方案。

在使用rbenv命令之前使用下一步命令

export PATH="$HOME/.rbenv/bin:$PATH"
export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"
eval "$(rbenv init -)"

然后在没有rbenv的情况下运行sudo -H -u vagrant bash -i -c安装命令

Bootstrap.sh文件

#!/usr/bin/env bash

sudo apt-get update
echo "========================= install dependencies for install rbenv ==========================="
sudo apt-get install -y autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev
echo "========================= install rbenv =========================================="
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >>  ~/.bashrc
echo 'eval "$(rbenv init -)"' >>  ~/.bashrc
echo "========================= install ruby build plugin for rbenv ======================="
git clone https://github.com/rbenv/ruby-build.git  ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
echo "========================= install ruby v2.5.0 =========================================="
export PATH="$HOME/.rbenv/bin:$PATH"
export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"
eval "$(rbenv init -)"
rbenv install 2.5.0
rbenv global 2.5.0
ruby -v
gem -v
echo "========================= install bundler dependencies manager for ruby ====================="
gem install bundler
rbenv rehash

然后,VagrantFile文件将包含流浪者配置行

deploy_config.vm.provision :shell, privileged: false, path: "bootstrap.sh"

Gits by @creisor的答案来源