使用Vagrant安装rails环境(rails-dev-box)

1. 什么是Vagrant

简单来说,Vagrant是一个虚拟机管理软件,你可以使用它脱离虚拟机容器(类似VirtualBox之类的软件)来管理(启动、停止、配置等)虚拟机(镜像文件)。使用它,你可以快速的进行在干净的环境下进行测试,部署等。(有点类似Docker,不过我感觉Docker比传统的虚拟机更高一层,灵活性更高,效率也会高很多。)

2. 安装

2.1. 安装VirtualBox

首先,需要安装VirtualBox,这个不多说,从官方(https://www.virtualbox.org/wiki/Downloads )下载即可。
我下载的VirtualBox-4.3.6-91406-OSX.dmg有118.6M。
除了Mac版的,还有Windows和Linux,以及Solaris版本的。

2.2. 安装Vagrant

接着,安装Vagrant。
从官方选择一个(100%的人会选择最新版吧)版本下载:
http://downloads.vagrantup.com/

我下的Vagrant-1.4.2.dmg有33.9M

2.3. 安装rails-dev-box

rails-dev-box(https://github.com/rails/rails-dev-box )是一个为了开发Rails 本身而准备的环境,而不是为了开发普通的Rails应用程序

安装方法见项目主页的说明,基本就是这样:

$ git clone https://github.com/rails/rails-dev-box.git
Cloning into ‘rails-dev-box’…
remote: Reusing existing pack: 275, done.
remote: Counting objects: 1, done.
remote: Total 276 (delta 0), reused 1 (delta 0)
Receiving objects: 100% (276/276), 90.55 KiB | 64.00 KiB/s, done.
Resolving deltas: 100% (66/66), done.
Checking connectivity… done
$ cd rails-dev-box
$ ls
MIT-LICENSE README.md   Vagrantfile puppet

之后,执行vagrant up命令,即可下载并启动虚拟机了。

$ vagrant up

Bringing machine ‘default’ up with ‘virtualbox’ provider…
[default] Box ‘precise32’ was not found. Fetching box from specified URL for
the provider ‘virtualbox’. Note that if the URL does not have
a box for this provider, you should interrupt Vagrant now and add
the box yourself. Otherwise Vagrant will attempt to download the
full box prior to discovering this error.
Downloading box from URL: http://files.vagrantup.com/precise32.box
Progress: 61% (Rate: 53264/s, Estimated time remaining: 0:30:22)
Extracting box…te: 46914/s, Estimated time remaining: 0:00:01)
Successfully added box ‘precise32’ with provider ‘virtualbox’!
[default] Importing base box ‘precise32’…
[default] Matching MAC address for NAT networking…
[default] Setting the name of the VM…
[default] Clearing any previously set forwarded ports…
[default] Clearing any previously set network interfaces…
[default] Preparing network interfaces based on configuration…
[default] Forwarding ports…
[default] — 22 => 2222 (adapter 1)
[default] — 3000 => 3000 (adapter 1)
[default] Booting VM…
[default] Waiting for machine to boot. This may take a few minutes…
[default] Machine booted and ready!

不过不幸的是,vagrant的官网被封了,需要翻墙才行。

启动之后,我们可以用ps看一下。

$ ps -ef | grep -i vbox

501 2542 1 0 5:18下午 ?? 0:02.18 /Applications/VirtualBox.app/Contents/MacOS/VBoxXPCOMIPCD
501 2545 1 0 5:18下午 ?? 0:11.93 /Applications/VirtualBox.app/Contents/MacOS/VBoxSVC –auto-shutdown
501 2662 2545 0 5:19下午 ?? 2:33.37 /Applications/VirtualBox.app/Contents/MacOS/VBoxHeadless –comment rails-dev-box_default_1389431972122_33529 –startvm 2bda400a-24de-4f84-a5b5-d96bdd67c969 –vrde config

虚拟机已经启动了,我们就可以通过vagrant ssh登录到虚拟机

$ vagrant ssh

Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)

* Documentation: https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:22:31 2012 from 10.0.2.2

vagrant@rails-dev-box:~$

根据官方文档,这个虚拟机里预装了如下软件。

  • Git
  • RVM
  • Ruby 2.0.0 (binary RVM install)
  • Bundler
  • SQLite3, MySQL, and Postgres
  • System dependencies for nokogiri, sqlite3, mysql, mysql2, and pg
  • Databases and users needed to run the Active Record test suite
  • Node.js for the asset pipeline
  • Memcached

我们可以简单确认下

vagrant@rails-dev-box:~$ rails -v
The program ‘rails’ is currently not installed. You can install it by typing:
sudo apt-get install rails
vagrant@rails-dev-box:~$ ruby -v
ruby 2.0.0p353 (2013-11-22 revision 43784) [i686-linux]

vagrant@rails-dev-box:~$

所以,如果你想测试Rails应用的话,应该自己安装rails:

$ gem install rails

这之后,我们就可以新建一个项目来测试了。这步比较简单,这里就略过了。

3. 总结

这里我们虽然使用了rails-dev-box,但从零开始自己建一个虚拟机也是挺容易的。具体可以参考这个网址:
Setup a simple Ruby 2 on Rails 4 environment with Vagrant and Virtual Box  



Posted in Rails, Ruby, Tech Tagged with: , ,

无觅相关文章插件,快速提升流量