#292 Virtual Machines with Vagrant
Oct 24, 2011 | 11 minutes | Tools
Vagrant allows you to run your Rails application and all of its dependencies in a portable, sharable environment. Use for development, set it up as a staging server, or experiment with a production setup.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
I've been using Vagrant since it's very beginning. Helped it's author test the early versions and contributed to it's underlining 'virtualbox' gem. A brilliant tool! Thanks for making making a screencast about it.
Personally I setup the environment and repackage it rather than using Chef (because Chef can take a while depending on your internet connection).
Also, you can create custom plugins to help you save time. Things like 'vagrant rails_server', which could load up the box, start up all background servers, and load the rails application server, all in one command. It will speed up development.
This is awesome, I didn't realize that something like Vagrant even existed. The most painful part of development for me has been getting other developer machines up and running. Can't wait to try this out, thanks for covering this :)
Virtualbox has some bugs with SSH dhclient to get a proper IP address (https://github.com/mitchellh/vagrant/issues/455)
anyway, Vagrant is a pretty good interface to build distributed local environment and to provide production mirror servers setup
This bug is a #1 priority to fix, if I can get a hold of what is causing it. Unfortunately, finding the exact cause has been difficult. :(
yes, I'm actually using Vagrant, so "config.ssh.max_tries = 150" can be helpful sometimes
Thanks for the screencast! This was a fantastic introduction to Vagrant. Some suggestions for next things to look into on Vagrant after this screencast:
Instead of manually using shell commands to setup your environment, but also to avoid the complexities of Chef, check out the Shell provisioner. You just pass it a shell script on your local machine, and it runs it on the remote machine.
Host-only networking is very useful, as it allows you to set a static IP on your machine. Rather than forwarding each port you need, you can access any port (assuming you have no firewalls on the machine). Also, you can use something like Ghost to assign hostnames to the VM.
For Rails especially, NFS shared folders become very important, since there is a bug in Virtualbox which causes performance to degrade with many files in the shared folder. NFS avoid this issue.
Thanks again Ryan! I'm a big fan of RailsCasts and a happy previous viewer (I no longer do much rails dev :().
Thanks for this great tutorial! But, isn't it easier and more efficient to use rvm instead of rbenv?
I found it much easier to use rvm
yeah, I was thinking the same... There is an advantage of using rbenv instead of rvm in a production environment? perhaps it's something about performance
Take a look at what rbenv doesn't do in comparison to RVM in the Readme: https://github.com/sstephenson/rbenv
rbenv is trying to be more, specific, as to what it does and not interfere with other existing software. (Apparently RVM rewrites functions like 'cd').
I used Virtual box to set up a staging environment on my dev machine and I used port forwarding on my router to connect the incoming requests to the virtual machine's internal IP address. So to access the staging server from the web I used something like www.my.web.address:1234. This work fine except with PayPal IPN. Paypal IPN does not like to send IPNs to non-standard ports. I am already using Port 80 for another VM so that's not an option.
Anyone know how to fix this?
I resolved this by getting a mico-instance on Amazon, then everything worked, but I would really prefer to do it locally if possible.
Vagrant has some really nice features - thanks for the tutorial.
AMAZING! Loved it. Will definitely use soon.
install nodejs as easy as:
much faster than therubyracer
+1 on this suggestion
At first I was afraid that the free episodes' quality might suffer from the pro and revisited episodes. That is definitely not the case, I am, well, impressed. Thx for all these efforts.
Fantastic video, as usual.
Barely related question: Why the need for adding "bundle exec" to "rails s"? I think I somehow glossed over the "bundle exec" movement as I see it everywhere now, but don't fully understand why.
From what I just read it executes the command in the context of the bundle, but I've never needed to do this in the past (or rather, I never knew I needed to :-) Is this related the use of rbenv and lack of gemsets?
I just wanted to ask the same question. I just set up a vagrant box as shown in this episode but I installed rvm and I don't need this bundle exec to run the server.
You need to do "bundle exec" especially when doing rake tasks
Yes, but why? :-)
To use the proper settings. Maybe rake command starts the wrong version of ruby. Maybe it sets up a wrong configuration... it is written nicely already: http://yehudakatz.com/2011/05/30/gem-versioning-and-bundler-doing-it-right/
Sure, you don't have to use bundle exec. In this case, however, you have to use binstubs, and you have to run commands from there.
Thanks for the link! That cleared it up for me.
Good question, needing to run
bundle exec
depends on how your system is setup. Here I'm only installing gems through bundler (there is no Rails gem installed on the system outside of Bundler) so I need to usebundle exec
. It's also a good idea to always use this to ensure you're working with the proper version of the gem.On my development machine I am using Oh My ZSH with the Bundler plugin which sets up aliases for these commands so I don't need to type in "bundle exec" in development.
I think ruby users on win32 (RubyInstaller & DevKit) will have to do "gem install ffi -v=1.0.9" before "gem install vagrant". There are issues with the current version of this virtualbox - dependency: https://github.com/ffi/ffi/issues/167
On the other hand the setup of vagrant on win64 runs well through jruby: http://vagrantup.com/docs/getting-started/setup/windows_x64.html
hm, that wasn't Ryan Bates, right?. Screencast outsourcing? :-)
Haha, had a bad cold that really messed with my voice.
Is Ubuntu the only supported VM for vagrant? It would be nice to have support for Fedora/RH as well.
There is a list of available vagrant box packages which is not including Fedora yet: http://www.vagrantbox.es/
I think you have to create your own through VirtualBox (select your operating system and version at the Create Virtual Machine Wizard, Fedora available?) in consideration of vagrant's guidelines for virtual boxes (http://vagrantup.com/docs/base_boxes.html): https://www.virtualbox.org/manual/ch01.html#gui-createvm
Then you have to package the box through vagrant: http://vagrantup.com/docs/base_boxes.html
I would be nice to see a Rails setup using Chef and/or Puppet.
+1 for chef-solo or puppet
Agreed. I hear so much about Chef and want to use it, but can't find any good resources.
thanks a bunch, can't wait for vagrant + chef screencast
Just running through the ascii cast and it seems that you are missing curl from this list of things that need to be installed before rbenv install will run:
sudo apt-get install build-essential zlib1g-dev git-core sqlite3 libsqlite3-dev curl
Thank you Sherman and thank you Ryan!
After running gem install vagrant, I attempted to add a box, but it can't find vagrant?
$> vagrant box add lucid64 http://files.vagrantup.com/lucid64.box
-bash: vagrant: command not found
Anyone else run in to this?
I've a Rails3 App with MySQL DB. I set vagrant and I arrange all gems for running my Rails3 app on vagrant but I faced with this error message when I want to run server,
/usr/lib/ruby/1.8/rubygems/dependency.rb:52:in `initialize': Valid types are [:development, :runtime], not nil (ArgumentError)
Do you know the solution of this?
Instead of replicating my database in the VM, I'd prefer to connect to MySQL on my "host". I've been reading through the documentation, and while I see how to forward ports from "host" to "guest", I don't see how to do it in the other direction?
I read about the Host-only Networking, but I don't see where/how to set the host's IP?
Is this possible?
SOLUTION:
If you're using port forwarding, you can reference your "host" machine by it's regular network address (for example, 192.168.1.111). Incidentally, this is the requesting IP that is used when trying to connect to mySQL, so be sure to allow it in your "host" mySQL configuration.
If you're using Host-Only networking, your "host" is at x.x.x.1. for example, if you follow the default and set vagrant to use 33.33.33.10, then your "host" machine is at 33.33.33.1. You'll need to ensure that connections from the vagrant box IP are allowed for the user you're using.
I would love to see a Chef + Vagrant episode.
Vagrant-Snap is an add-on for taking disk snapshots. Very fast and simple to use - much faster than rebuilding machines from scratch.
https://github.com/t9md/vagrant-snap
Awesome screencast!!!! very useful !!
Anyway, I wondering about how to setup a best
development / Production environment using Vagrant + puppet ?
Thanks
Holy crap, why have I not been using this!
I'm confused. Virtualbox already allows you to package up servers:
http://grok.lsu.edu/article.aspx?articleid=13838
And do port forwarding or even give the server it's own IP address:
http://www.virtualbox.org/manual/ch06.html
So the only thing I see Vagrant as providing is some very basic premade packages and a prettier command line interface to Virtualbox. Am I correct or is there something big I'm missing here??? Seriously want to know as I don't wish to miss something significantly better.
Side note: I wish it used Xen PV instead as that is actually difficult to configure, unlike VB.
I have a brand new MBP with the most memory it can hold and for some reason lucid32 on vagrant is very very slow to forward the port.
me too
same here..I've followed the steps to install vagrant with rails but when I try to connect with localhost:3000 on windows it takes 1-2minutes to load the page.
If I execute a curl command on the vbox though I get the page in no time.
I rant into this recently and it turned out that Webrick was the culprit. On every request it appears Webrick was trying to do DNS reverse resolution. Our solution was to switch it to using thin but you can also modify webrick/config.rb.
Ryan, I guess you are missing rbenv global 1.9.2-p290 in instructions. Thanks for this awesome screencast!
If you have to setup vagrant on windows (for whatever reason), I recommend to install chocolatey and then install vagrant with:
This downloads and install putty, virtualbox and vagrant in place.
This way I setuped a machine for our frontend developer.
So Im a bit outside the rails community as a network admin. I was a developer primarily years ago, but mostly PHP/Python/C/shell scripts (and before that dBase and Foxpro :).
So simple question - what's the benefit over just doign a chroot into a debootstrap'd image?
Hi Ryan, thanks for another awesome tutorial! Great! :) Just wanted to add… if anyone's having trouble getting terminal colors working after creating ~/.bash_profile, you need to run
source ~/.bashrc
each time you login. Or you can add the line to ~/.bash_profile by typing the following in terminal:Hi Ryan,
What should I put it to add mongodb support ?
Do I need to fully install it with apt-get on the VM ?
Does it also require Port Forwarding ?
Thanks
Another Awesome screencast! Very nice Mr. Bates.
Howdy,
Today (10/18/2013) I went to play the video and it will not load
"Can't Play video Media Source loading has failed"
Could someone look into this please? Your other railscasts work fine.
Thanks,
Bill
Great railscast, thanks!
However
gem install vagrant
only resulted in downloading vagrant version 1.0.7, which is way out of date
and gave me many mysterious errors.
the best way to install vagrant is from binary installer from:
http://downloads.vagrantup.com/
If you need to setup test stages for testing concrete features, you can try https://teatro.io – it provides personal stage server for each feature
Im guessing for the current version of virtualbox and vagrant this line
'config.vm.forward_port "rails", 3000, 3000'
would instead be this line
'config.vm.network "forwarded_port", guest: 3000, host: 3000'
'exit'
'vagrant reload'
'vagrant ssh'
cd ..to rails app folder
'rails s'
but when i visit http://localhost:3000 with the WeBrick server running, i get nothing (can't find server at localhost:3000)
Any idea what I am missing?
Got the same problem as you
Here is a solution I've found in http://stackoverflow.com/questions/27627286/cant-connect-localhost3000-ruby-on-rails-in-vagrant
start rails with 'rails s -b 0.0.0.0' instead of 'rails s'
Brilliant screencast!
+1