Skip to content

Instantly share code, notes, and snippets.

@bergantine
Last active August 29, 2015 13:55
Show Gist options
  • Save bergantine/241bd3635419dbbc86f4 to your computer and use it in GitHub Desktop.
Save bergantine/241bd3635419dbbc86f4 to your computer and use it in GitHub Desktop.
Vagrant config for a new Ruby on Rails project

Initial setup of Vagrant Base

This step only ever needs to be done once. Once the precise64 box is installed on a system the remaining steps refer to that same box regardless of the project.

Download and install Xcode from the Apple App Store.

Download virtualbox from http://www.virtualbox.org/wiki/Downloads, install dmg.

Download vagrant from http://downloads.vagrantup.com/, install dmg.

Launch a terminal window, check that it installed:

(host) $ which vagrant

Add a vagrant box (we'll be using Ubuntu Precise Pangolin (12.04 LTS) 64-bit):

(host) $ vagrant box add precise64 http://files.vagrantup.com/precise64.box

Starting a New Project

Make a directory for the project and change to it, replacing <path_to> with the path to the project and <project_name> with the name of the project.

(host) $ mkdir <path_to>/<project_name> && cd $_

For example, to create a project called 'website' in your home directory:

(host) $ mkdir ~/website && cd $_

When you're all done, within this directory will be a directory named vagrant/ which will match up with /home/vagrant/ in the virtual envirionment. Virtualbox keeps the two directories in sync so changes to one will be made in the other.

Copy in the Vagrantfile.

(host) $ curl https://gist.github.com/jbergantine/241bd3635419dbbc86f4/raw/Vagrantfile > Vagrantfile

Copy in the provisioning files.

(host) $ curl https://gist.github.com/jbergantine/241bd3635419dbbc86f4/raw/bootstrap.sh > bootstrap.sh
(host) $ curl https://gist.github.com/jbergantine/241bd3635419dbbc86f4/raw/install-rvm.sh > install-rvm.sh
(host) $ curl https://gist.github.com/jbergantine/241bd3635419dbbc86f4/raw/install-ruby.sh > install-ruby.sh
(host) $ curl https://gist.github.com/jbergantine/241bd3635419dbbc86f4/raw/set-default-ruby.sh > set-default-ruby.sh
(host) $ curl https://gist.github.com/jbergantine/241bd3635419dbbc86f4/raw/install-nodejs.sh > install-nodejs.sh

Startup Vagrant and provision the Virtual Machine:

(host) $ vagrant up

SSH in to the virtualbox:

(host) $ vagrant ssh 

Start a project

(vm) $ rails new project
(vm) $ cd project
(vm) $ rails server

Open a Web browser and navigate to http://localhost:3000. The defualt Ruby on Rails page should show up.

From here: Getting Started with Rails

#!/usr/bin/env bash
apt-get update
# install curl without prompts
apt-get install curl -q -y
#!/usr/bin/env bash
apt-get install nodejs -q -y
#!/usr/bin/env bash
source /usr/local/rvm/scripts/rvm
rvm use --install $1
shift
if (( $# ))
then gem install $@ --no-ri --no-rdoc
fi
#!/usr/bin/env bash
curl -sSL https://get.rvm.io | bash -s $1
#!/usr/bin/env bash
source /usr/local/rvm/scripts/rvm
rvm use $1 --default
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "precise64"
# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
# Provision
config.vm.provision :shell, :path => "bootstrap.sh"
config.vm.provision :shell, :path => "install-rvm.sh", :args => "stable"
config.vm.provision :shell, :path => "install-ruby.sh", :args => "2.1.0 rails"
config.vm.provision :shell, :path => "set-default-ruby.sh", :args => "2.1.0"
config.vm.provision :shell, :path => "install-nodejs.sh"
# To access our website, we can open a web browser on our workstation
# and go to http://localhost:3000
config.vm.network "forwarded_port", guest: 3000, host: 3000
# Shared folder
config.vm.synced_folder "vagrant/", "/home/vagrant", create: true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment