Skip to content

Instantly share code, notes, and snippets.

@arafatm
Last active December 31, 2015 23:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save arafatm/ea2d52f61d43befe9fc3 to your computer and use it in GitHub Desktop.
Save arafatm/ea2d52f61d43befe9fc3 to your computer and use it in GitHub Desktop.
Set up a vagrant environment to learn Rails 4.

Stack

The technology stack we're going to use for the next section is:

  • VirtualBox
  • Vagrant
  • Ubuntu Precise
  • Ruby/Rails

Go ahead and download the following installers:

Virtual Machine

A Virtual Machine (VM) allows us to run an entire OS on top of the one we are currently running. The OS you're current running is called the Host OS. The OS you run in the VM is called the Guest OS.

The benefit of a VM is that it allows you to run multiple OSes simultaneously.

In our case we're going to use VM technology to run a linux Guest OS on top of our Host OS (most probably Windows in your case).

VirtualBox

The VM product we're going to use is VirtualBox. While there are other VM products we could use (such as VMWare), VirtualBox plays nice with Vagrant which we will use.

So go ahead and install Virtualbox.

Vagrant

While we could manually create VMs in VirtualBox, it's easier to write a script that handles creation and management of the VM images.

For more benefits of using Vagrant, take a look at Why Vagrant?

So go ahead and install vagrant.

Basebox

So we now have VirtualBox and Vagrant installed to manage our VMs. Only thing missing is an actual OS image that we can run.

Vagrant expects boxes that are images with additional configuration for vagrant to manage.

The box isn't used directly, but is a base image that is used to create new images for you to run. The benefit is that you can create an image per project you work on from the template basebox.

To create a new image for a new project you're working on:

  • vagrant box add /path/to/basebox.box Add the basebox to vagrant
  • Create a project folder
  • $ vagrant init precise32 will set up the vagrant configuration
  • When you start the rails server on vagrant, it will run on port 3000 by default. So we need to set up port forwarding to the VM on that port. Find the line that starts with # config.vm.network :forwarded_port ... and modify it to read config.vm.network :forwarded_port, guest: 3000, host: 3000
  • spend some time looking at the rest of the Vagrantfile. Any line that starts with # is commented out. But this will give you an idea of some of the options you can configure
  • $ vagrant up will create the virtual machine for you based on the basebox template. Look at your project directory and you should notice a .vagrant/ directory created. That's where your VM is located. It will also start the VM so you can use it.
  • $ vagrant ssh will connect you to the machine. You are now in the ubuntu machine.
  • $ exit to exit the vagrant box
  • $ vagrant halt will shut off the VM. Since a VM uses up system resources, it's a good idea to halt the vagrant box when you're done using it.

Note: In the first step, you could have $ vagrant init precise32 http://files.vagrantup.com/precise32.box which would have downloaded and installed the box for you. However, since you already had downlaoded the box (which you only need to do once), you could use it by pointing to it's location on the filesystem.

When you add a box via vagrant box add or vagrant init baseboxname http://url/to/basebox.box the basebox is installed in %HOMEPATH%/.vagrant.d\boxes.

Now that you have "installed" the basebox, it is reusable. Try creating a new project folder and running:

  • $ vagrant init precise32 This will use the basebox installed the first time in %HOMEPATH%\.vagrand.d\boxes.
  • $ vagrant up will start the virtual machine.
  • $ vagrant halt to shut down the machine.

Open up Oracle VM VirtualBox from your start menu. You should see at least 2 VMs listed.

vagrant plugin vbguest

Vagrant also has a plugin system. One plugin we are interested in is vagrant-vbguest which allows us to automate updating the VirtualBox Guest Additions.

The Virtualbox Guest Additions gives us extra functionality such as shared folders.

Unfortunately, every time we update the linux kernel in the guest we also have to update the guest additions. This plugin will automate the process for us a little.

Run the following:

  • vagrant plugin install vagrant-vbguest
  • vagrant reload

Ubuntu

The Guest OS we're going to run is based on Ubuntu Server. Ubuntu server is a popular linux distro while having no desktop components installed. What we end up with is a lightweight linux os that can run in a VM with minimal resources.

The basebox you downloaded is based on Ubuntu Precise 32-bit.

Right now, you have a bare Ubuntu system with not much installed. I recommend you vagrant ssh and reread the bash section of this gist to refresh your knowledge of getting around the command line.

APT

Ubuntu uses apt-get as a package manager. This allows you to install/delete/manage additional software on your Ubuntu system.

Some useful command apt commands:

  • apt-cache search xxx search for a software package with xxx in the name
  • apt-get install xxx install software package xxx
  • apt-get update update the software packages databases so you can upgrade to the latest versions
  • apt-get upgrade upgrade all currently installed software

Note: Linux has a sane privilege escalation model. This means that your user account cannot make potentially breaking changes to the system. In order to do something like install a software package, you have to request admin privilege. In this case you use sudo. So for the apt-get commands, you would prepend sudo. e.g. sudo apt-get install xxx. however, you don't need sudo apt-cache since searching can be run by normal users.

Install vim/git/ruby/rails

  • What is the output of whoami
  • What is the output of ruby -v
  • What is the output of which ruby
  • Execute sudo rm -f /usr/bin/ruby. The precise32 basebox comes with a default ruby 1.8.7 install. We want to use the latest version of ruby available so we're going to disable 1.8.7 and install the latest version
  • Execute apt-get update. What does this command do? You probably got an error. How do you fix it?
  • sudo apt-get dist-upgrade
  • apt-cache show vim
  • apt-cache show git
  • sudo apt-get install git vim
  • sudo apt-get install ruby1.9.1
  • sudo apt-get install build-essential ruby1.9.1-dev nodejs libxslt-dev libxml2-dev libsqlite3-dev install some packages that will be required by rails
  • What is the output of ruby -v. You'll probably have to exit vagrant and ssh back in order to update your environment to use the new version of ruby.
  • install the rails gem
  • What is the output of rails -v
  • Exit out of vagrant then reconnect.

Learn VIM

Learn to use Vim since it's your best option for a command-line text editor.

Shared Folders

So one of the benefits of using vagrant to manage your virtual machines is that it automatically sets up shared folders between your local and virtual machines.

In windows take a look at your project directory. You should see a Vagrantfile and a .vagrant directory. Make sure you git commit these.

In vagrant, cd /vagrant. When you ls -a you will notice the same Vagrantfile and .vagrant directory as above. This /vagrant directory is shared between your windows machine and the Virtual Machine. ie Any changes you make in windows will reflect in vagrant and vice versa.

Create a README in /vagrant and git commit. Now create a github repo so you can push all your changes.

Exercise: Push the '/vagrant` directory to github.

Provisioning

There is a way in Vagrant to automate the installation and configuration of your base system.

I'd like you to get comfortable doing it manually and I will introduce how to automate at a later date.

Learn Rails

Skip Chapter 1 http://ruby.railstutorial.org/ruby-on-rails-tutorial-book

Also, DO NOT USE RVM. Ignore any RVM specific instructions. The vagrant box has ruby/rails installed already.

Ignore this line

gist -u ea2d52f61d43befe9fc3 furaha_04_rails_vagrant.mkd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment