A development-ready environment includes the following tools:
- Ruby Version Manager (RVM)
- Ruby 1.9.3 and 2.1
- PostgreSQL 9+
- SublimeText Editor
- Ubuntu Linux
The entire setup process should take less than three hours.
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:
Look under "VirtualBox platform packages" for the binary distribution appropriate for your platform. Run the installer and follow the instructions.
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
C:\projects\starting_rails) and store the configuration for your virtual machine.
Then, from that directory:
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.
Vagrantfile in a text editor and uncomment/modify line 22 so it looks like this:
Save the file and close your editor. Return to the terminal and start the VM:
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:
You’re now inside the fully-functioning operating system.
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
- It currently contains the config file
- You can create a file
~/projects/starting_rails/README.mdusing your host OS and any editor
- Within the VM’s SSH session, you can interact with that file, like
Check out http://docs.vagrantup.com/v2/synced-folders/ for more complex folder synching, but this setup will be good enough for now.
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.
You’ll of course need Git for source control. Install it within the SSH session:
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.
Postgres uses information from the operating system to determine the language and encoding of databases. Let’s set that default locale before install postgres :
PostgreSQL is the database of choice in the Ruby community. Let’s get it installed with apt:
Creating the Database Instance & Adding a User
Once installed, we need to create the database instance. Within the SSH session:
$ $ $ $ $
Respond "Y" to
Shall the new role be a superuser? Then you can exit the
Now you’re back to your Vagrant user session.
Add privilege for Vagrant to create database.
vagrant@vagrant-ubuntu-trusty-64:~$ psql postgres psql (9.3.5) Type "help" for help.
postgres=> ALTER ROLE vagrant CREATEDB; postgres-> \q vagrant@vagrant-ubuntu-trusty-64:~$
Verifying Install and Permissions
You should now be back to the normal
vagrant@precise32:~$ prompt. Let’s create a database and connect to it:
You should see the following:
There are several options for managing Ruby versions, but we’ll use RVM with the standard "single user" method.
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:
As it says in the post-install instructions, we need to load RVM into the current environment by running:
Note that there will be no output from this command, but you can now see RVM:
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:
Ruby 1.9.3 and 2.1
You can see all the Rubies available through RVM with this command:
Then install both Ruby 1.9.3 and 2.1:
It’ll take awhile to compile.
You can set either as your default Ruby. For 2.1, run this:
And verify it:
Just about every project now uses Bundler, so let’s install it:
Let’s clone and run a sample Rails application to make sure everything is setup correctly.
Clone the Project
Within the SSH session:
$ $ $
Next we need to install dependencies and setup the database:
Check that the console is working properly:
Run the Server
Then, in your host operating system, open http://localhost:3000 in a browser. You should see the Welcome abord page – you’re done!
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
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:
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:
It’ll clone the box then boot. Now you can
vagrant ssh and you’re ready to go!