Vagrant, Virtual Box, Cygwin, Rvm, Ruby, Rails4 Installation!

Purpose:


  • Configuration to use vbox and vagrant from both Windows and Cygwin shell.
  • Share VBOXdefaultMachineFolder ("C:\Users\\$USER\VirtualBox VMs") to share the vbox images between Windows and Cygwin.
  • Share folder (C:\Vagrant\.vagrant.d) to keep boxes downloaded by vagrant across multiple users on Windows Host.
  • rvm in vbox for making it possible to use different versions of ruby/rails.
  • From cygwin, start vbox using "vagrant up". Do "vagrant ssh" to get regular shell interface to use Guest vbox.
  • Use a folder (referred as WIN_HOST_PROJ_DIR here) on Host Windows machine that'll keep the project code/data which is built using Guest Virtual Box (such as Ububtu). It also contains Vagrantfile for the project and "id" file for the vbox being used.
  • VAGRANT_HOME contains original .vmdk downloaded by "vagrant up" & a (small) Vagrantfile that defines MAC address.
  • VBOX_USER_HOME contains VirtualBox.xml, which lists available VMs.
  • VBOX_USER_HOME\..\\VirtualBox VMs\ contains actually utilized .vmdk & .vbox definition file.



________________________________________________


Vagrant

Install in folder: C:\Vagrant
    CMD> vagrant -v
         Vagrant 1.6.5
    mkdir: $WIN_HOST_PROJ_DIR
    cd $WIN_HOST_PROJ_DIR
    WIN_HOST_PROJ_DIR> vagrant init hashicorp/precise64
 
 
 

Vagrantfile modifications

  config.vm.box = "hashicorp/precise64"

   # Define Name for the Vagrant VBox to load: "vagrant_ubuntu_rails4"
   config.vm.define "vagrant_ubuntu_rails4" do |t|
   end

   # Don't auto-update!
   config.vm.box_check_update = false
   # To forward port 3000 from Host to VM, change Vagrantfile file:
   config.vm.network "forwarded_port", guest: 3000, host: 3000
   # Show VirtualBox window (Headless doesn't seem to save much memory/processor; in fact invisible vbox could become potentially troublesome)
   vb.gui = true
 
 

Share Vagrant downloaded Boxes (global state) across all Windows Host users

 

Set Windows Environment Variable:

 
  VAGRANT_HOME=C:\Vagrant\.vagrant.d
 

Get vagrant box:

 
  • Following downloads chosen vbox from vagrantcloud to $VAGRANT_HOME
  • Creates configuration $WIN_HOST_PROJ_DIR/.vagrant/machines/
  • Mounts HOST pwd to /vagrant on Guest OS.
 
    WIN_HOST_PROJ_DIR> vagrant up
      Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'hashicorp/precise64' could not be found. Attempting to find and install...
    :
     ==> default: Successfully added box 'hashicorp/precise64' (v1.1.0) for 'virtualbox'!
     :
     ==> default: Forwarding ports...
      default: 3000 => 3000 (adapter 1)
     ==> default: Booting VM...
     :
     ==> default: Machine booted and ready!
     :
     ==> default: Mounting shared folders...
      default: /vagrant => $WIN_HOST_PROJ_DIR
     WIN_HOST_PROJ_DIR> vagrant up
      user: vagrant
      pswd: vagrant
     :
     WIN_HOST_PROJ_DIR> vagrant halt
 

Assign proper name to our Virtual Box machine

    Start Windows VitrualBox application and rename:
 
        VitrualBox -> vagrant_ubuntu_rails4_default_1413560977384_57950
         Rename => vagrant_ubuntu_rails4
 
    This renames C:\Users\$USER\VirtualBox VMs\vagrant_ubuntu_rails4
 

Backup new Vagrant/Virtual Box configuration

Safely save new Virtual Box configuration, just in case it's needed for later restoration. (More info below).
 
rename: $WIN_HOST_PROJ_DIR\.vagrant\machines\vagrant_ubuntu_rails4_1413655330216_72264 $WIN_HOST_PROJ_DIR\.vagrant\machines\vagrant_ubuntu_rails4_RESTORE
copy: $WIN_HOST_PROJ_DIR\.vagrant\machines\vagrant_ubuntu_rails4_RESTORE $WIN_HOST_PROJ_DIR\.vagrant\machines\vagrant_ubuntu_rails4
Now, "vagrant up" should load vbox as "vagrant_ubuntu_rails4".

 
________________________________________________
 

 

Cygwin configuration to run/share Vagrant, VitrualBox

 

Edit Cygwin .bashrc

 
# Cygwin setup to use Vagrant and VirtualBox
############################################
## export PATH=$PATH:"/cygdrive/c/Program Files/Oracle/VirtualBox"
## export VAGRANT_HOME="C:\Vagrant\.vagrant.d" # use value already set in Win Env (DOS format)
# Set VBOX Default machine folder (machinefolder). This does the job of: VBoxManage.exe setproperty machinefolder "C:\\Users\\$USER\\VirtualBox VMs"
# By setting VBOX configuration path, VBoxManager finds its Default machine folder (defaultMachineFolder)
# from VBOX_USER_HOME/VirtualBox.xml. This effectively does the job of:
# VBoxManage.exe setproperty machinefolder "C:\\Users\\$USER\\VirtualBox VMs":
export VBOX_USER_HOME="C:\\Users\\$USER\\.VirtualBox"
##export VAGRANT_LOG=debug
echo "Cygwin setup ready to use Vagrant and VirtualBox from your Vagrantfile folder."
echo "do: cd $WIN_HOST_PROJ_DIR; vagrant status; vagrant up;"
 
 
Restart Cygwin
cd $WIN_HOST_PROJ_DIR
vagrant status
     Current machine states:
     vagrant_ubuntu_rails4 poweroff (virtualbox)
     The VM is powered off. To restart the VM, simply run `vagrant up`
 
vagrant up
     :
vagrant halt

Note: Cygwin $HOME ~/. should not end-up creating any .vagrant or VirtualBox folders/files by mistake.
 

Additional info

"vagrant up" and default Virtual Box loaded:

 
Our Vagrantfile defines the default name VBox to load: "vagrant_ubuntu_rails4". So "vagarant up" uses
$WIN_HOST_PROJ_DIR\.vagrant\machines\vagrant_ubuntu_rails4\virtualbox\id
But when no such name is defined, it uses:
$WIN_HOST_PROJ_DIR\.vagrant\machines\default\virtualbox\id

How does vagrant locate Virtual Box?

 
$WIN_HOST_PROJ_DIR\.vagrant\machines\vagrant_ubuntu_rails4\id file (one-line, 36 bytes, NO CRLF) contains uuid.
<Machine uuid> are in .vbox files under $VBOX_USER_HOME
(C:\Users\$USER\VirtualBox VMs\vagrant_ubuntu_rails4\vagrant_ubuntu_rails4.vbox)
C:\Users\$USER\.VirtualBox\VirtualBox.xml has list of <MachineEntry uuid>
 

Prepare backup copy for restoring vagrant use desired original copy of vbox:

 
For some reason, if/when "vagrant up" creates new VBox image (again) "vagrant_ubuntu_rails4_1413655330216_72264",
follow these steps to restore the original/previously used VBox:
 
delete: C:\Users\$USER\VirtualBox VMs\vagrant_ubuntu_rails4_1413655330216_72264
delete: $WIN_HOST_PROJ_DIR\.vagrant\machines\vagrant_ubuntu_rails4_1413655330216_72264
copy: $WIN_HOST_PROJ_DIR\.vagrant\machines\vagrant_ubuntu_rails4_RESTORE $WIN_HOST_PROJ_DIR\.vagrant\machines\vagrant_ubuntu_rails4
# $WIN_HOST_PROJ_DIR\.vagrant\machines\default\virtualbox leave empty.
 Now "vagrant status" should show:
 
     Current machine states:
     vagrant_ubuntu_rails4 poweroff (virtualbox)
     The VM is powered off. To restart the VM, simply run `vagrant up`

 

Install apt-cyg (equivalent of apt-get), for future needs :)

 
chmod +x apt-cyg
mv apt-cyg /usr/local/bin/

Useful commands

 
vagrant status
vagrant global-status
For debugging info, export VAGRANT_LOG=debug

VBoxManage.exe list vms
VBoxManage.exe setproperty machinefolder "C:\\Users\\$USER\\VirtualBox VMs"
VBoxManage.exe list systemproperties # Default machine folder
VBoxManage.exe showvminfo vm_uuid
 
 
________________________________________________

 
 

Install rvm, ruby rails; Build RoR app in new Virtual box

 
 
ruby -v
     ruby 1.8.7 (2012-02-08 patchlevel 358) [x86_64-linux]
 
curl -L https://get.rvm.io | bash -s stable --ruby
     Installing RVM to /home/vagrant/.rvm/
     Creating alias default for ruby-2.1.3...

rvm -v
     rvm 1.25.33 (stable) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]
rvm list
    rvm rubies
     =* ruby-2.1.3 [ x86_64 ]
ruby -v
     ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux]

rvm use ruby-2.1.3 # already set
 
gem install rails
rails -v
 
rails new /vagrant/railstest
Edit /vagrant/railstest/Gemfile
     gem 'therubyracer', platforms: :ruby

rails server

Now from Windows Host Browser go to http://localhost:3000
 
 
________________________________________________
 
 

Install new vagrant box

  • Create a $WIN_HOST_PROJ_DIR folder to keep project code/data files.
  • Copy a Vagrantfile there and edit:
    • State Vagrant Box to download (discovered here)
      • config.vm.box = "ubuntu/trusty64"
    • Define Name for the Vagrant VBox
      • config.vm.define "vagrant_ubuntu_android" do |t|
  • Run "vagrant up" to Get vagrant box and other relevant configurations.
 
 
________________________________________________
 
 

Removing "inaccessible VMs" from Virtual Box UI

Find GUID of inaccessible VMs with:
    VBoxManage list vms

Use unregister to remove inaccessible VM entry:
    VBoxManage unregistervm {52a91e62-096d-4f8b-96f4-5fa76db3cd76}   # GUID in {}s

________________________________________________
 
 

Restoring vagrant/vbox using backup copy

To Restore a working vagrant/VM Vbox from a backed up copy of "C:\\Users\\$USER\\VirtualBox VMs":
  • Copy folder into "C:\\Users\\$USER\\VirtualBox VMs" from backup.
  • Open VirtualBox Manager. Clone the VM.
  • Take a copy of any "$WIN_HOST_PROJ_DIR\.vagrant\machines\" and update "id" and "action_provision" to use appropriate <Machine uuid>.
  • Review/ensure name & id in "C:\\Users\\$USER\\.VirtualBox\\VirtualBox.xml", "C:\\Users\\$USER\\VirtualBox VMs" and "$WIN_HOST_PROJ_DIR\.vagrant\machines\" are matching with each other.
  • vagrant up

________________________________________________

 
Kindly report any mistakes, failures, and of course, suggestions are most welcome :)

 

No comments:

Post a Comment