Ruby & Rails Development Environment with Vagrant
Goals
A development-ready environment includes the following tools:
- Ruby Version Manager (RVM)
- Ruby 1.9.3 and 2.1
- Git
- PostgreSQL 9+
- SublimeText Editor
- VirtualBox
- Vagrant
- Ubuntu Linux
Setup Process
The entire setup process should take less than three hours.
VirtualBox
VirtualBox is an application for running virtual machines. It’s free and available for every major platform. You can learn more about it, or jump straight to the download page here:
https://www.virtualbox.org/wiki/Downloads
Look under "VirtualBox platform packages" for the binary distribution appropriate for your platform. Run the installer and follow the instructions.
Vagrant
Vagrant is a system for easily managing and interactive with VirtualBox-based virtual machines. Using vagrant we can do everything from the command line and rarely if ever need to open the actual VirtualBox application. You can learn more on the Vagrant website.
First, download the appropriate binary (matching your primary OS) from http://www.vagrantup.com/downloads.html and run the installer. The later steps here are based on the Vagrant Getting Started Guide.
Setting Up Ubuntu
The easiest way to get going is to use an Ubuntu image preconfigured and vetted by the Vagrant project team. Drop into a terminal and change to a directory where you’ll build your project (such a ~/projects/starting_rails
or C:\projects\starting_rails
) and store the configuration for your virtual machine.
Then, from that directory:
Terminal
$
|
|
That’ll generate a Vagrantfile
. Before starting the virtual machine, we want to setup a bridged port so we can later access a web server running in Vagrant from our host operating system.
Open that Vagrantfile
in a text editor and uncomment/modify line 22 so it looks like this:
1
|
|
Save the file and close your editor. Return to the terminal and start the VM:
Terminal
$
|
|
When you run up
it’ll try and boot that image, see that it’s not available on the local system, then fetch an image of Ubuntu 12.04 "Precise Pangolin". Once downloaded and setup, it’ll be started.
Other operating system "boxes" can be found at https://vagrantcloud.com/discover/featured and you can build your own.
Entering the Virtual Machine
You can now SSH into the running virtual machine:
Terminal
$
|
|
You’re now inside the fully-functioning operating system.
Synched Folders
You can share files seamlessly between your host operating system and your virtual machine. This is useful if, for instance, you’d like to use a graphical editor in the host operating system, but run the code inside the VM.
The folder that you used to store the vagrant configuration is automatically shared with the virtual machine. So…
- Say you’re working in the directory
~/projects/starting_rails
- It currently contains the config file
~/projects/starting_rails/Vagrantfile
- You can create a file
~/projects/starting_rails/README.md
using your host OS and any editor - Within the VM’s SSH session, you can interact with that file, like
cat /vagrant/README.md
Check out http://docs.vagrantup.com/v2/synced-folders/ for more complex folder synching, but this setup will be good enough for now.
Local Editor
Because of the transparent folder sharing you have two options for editing code:
- SSH into your virtual machine and use a text-based editor like Vim or Emacs
- Run a graphical editor in the host operating system
If you go with option (2), we recommend downloading SublimeText which is available for OS X, Windows, and Linux. It’s quite popular in the Ruby community.
Git
You’ll of course need Git for source control. Install it within the SSH session:
Terminal
$ $ |
|
And respond y
to the prompt. You might notice that the sudo
didn’t ask for a password. Your Vagrant VM is setup to "trust" you. No one can login to the VM unless they’re an authenticated user of your host operating system, so this is safe.
PostgreSQL
Locale
Postgres uses information from the operating system to determine the language and encoding of databases. Let’s set that default locale before install postgres :
Terminal
$
|
|
PostgreSQL installation
PostgreSQL is the database of choice in the Ruby community. Let’s get it installed with apt:
Terminal
$
|
|
Creating the Database Instance & Adding a User
Once installed, we need to create the database instance. Within the SSH session:
Terminal
$ $ $ $ $ |
|
Respond "Y" to Shall the new role be a superuser?
Then you can exit the su
subshell:
Terminal
$
|
|
Now you’re back to your Vagrant user session.
Verifying Install and Permissions
You should now be back to the normal vagrant@precise32:~$
prompt. Let’s create a database and connect to it:
Terminal
$ $ |
|
You should see the following:
Terminal
$ |
|
RVM
There are several options for managing Ruby versions, but we’ll use RVM with the standard "single user" method.
Initial Setup
From your SSH session, we first need to install the curl
tool for fetching files, then can use a script provided by the RVM team for easy setup:
Terminal
$ $ |
|
As it says in the post-install instructions, we need to load RVM into the current environment by running:
Terminal
$
|
|
Note that there will be no output from this command, but you can now see RVM:
Terminal
$ |
|
Requirements
The RVM tool has an awesome tool for installing all the various compilers and packages you’ll need to build Ruby and common libraries. Run it like this:
Terminal
$
|
|
Ruby 1.9.3 and 2.1
You can see all the Rubies available through RVM with this command:
Terminal
$
|
|
Then install both Ruby 1.9.3 and 2.1:
Terminal
$ $ |
|
It’ll take awhile to compile.
Default Ruby
You can set either as your default Ruby. For 2.1, run this:
Terminal
$
|
|
And verify it:
Terminal
$ $ |
|
Bundler
Just about every project now uses Bundler, so let’s install it:
Terminal
$
|
|
JavaScript Runtime
Rails’ Asset Pipeline needs a JavaScript runtime. There are several options, but let’s install NodeJS:
Terminal
$
|
|
Verification
Let’s clone and run a sample Rails application to make sure everything is setup correctly.
Clone the Project
Within the SSH session:
Terminal
$ $ $ |
|
Rails Setup
Next we need to install dependencies and setup the database:
Terminal
$ $ |
|
Rails Console
Check that the console is working properly:
Terminal
$ |
|
Run the Server
Terminal
$ |
|
Then, in your host operating system, open http://localhost:3000 in a browser. You should see the Welcome abord page – you’re done!
Cloning
Once you’ve got a VM ready to go, you might want to back it up or copy it to other computers.
Creating the Image
Start within the same folder as the Vagrantfile
and:
Terminal
$
|
|
It’ll shutdown the VM if it’s running, then export a movable image named package.box
which is about 650mb.
Move the file by any normal means (SCP, flash drive, network share, etc).
Setup the Second Machine
Now on the machine where you want to run the VM you’ll need to install VirtualBox and Vagrant using the same steps as above.
Setup the Box
In a terminal from the same directory where the package.box
file is, run the following:
Terminal
$
|
|
That will "download" the box file to the local Vagrant install’s set of known boxes.
Provision and Start the Box
Now move to the project directory where the Vagrantfile
and your application code will live. Then:
Terminal
$ $ |
|
It’ll clone the box then boot. Now you can vagrant ssh
and you’re ready to go!