Install a Virtual Ruby on Rails development environment

If you still wish to roll your own virtual development environment, these steps should work. It is a good exercise to do this for yourself, although it takes a bit of time.

The commands you enter on your local host are prefixed by $ and the commands you enter on the virtual machine are prefixed by vagrant$.

Before you begin: Prerequisites

  1. Install VirtualBox
  2. Install Vagrant

Setting up your local “host” machine

  1. Create a new virtual project:

     $ cd to/the/place/i/store/my/projects
     $ mkdir new_project
     $ cd new_project
     $ vagrant init ubuntu/trusty64
    
  2. Modify the Vagrantfile to include the following line in the configure section:

       config.vm.network "forwarded_port", guest: 3000, host: 3000
    
  3. Then uncomment the provider block and set the memory to at least 1024 (since we’ll be compiling ruby):

       config.vm.provider "virtualbox" do |vb|
         # Display the VirtualBox GUI when booting the machine
         # vb.gui = true
    
         # Customize the amount of memory on the VM:
         vb.memory = "1024"
       end
    

    You can leave the vb.gui line commented out, and you really want to at this point. The virtual machine needs 1024 GB of memory in order to successfully compile ruby and it’s documentation.

  4. Save the Vagrantfile and return to the command prompt.

  5. Then start up the VM and log into it:

     $ vagrant up
     $ vagrant ssh
    

Provisioning and Configuring you virtual machine

  1. Follow these excellent instructions

Initializing your Rails project

  1. Initialize your rails project:

     vagrant$ cp .ruby-version /vagrant # need this to get your ruby version over to the project dir
     vagrant$ cd /vagrant # this is mapped to the host machine folder for your project
     vagrant$ rails new . [options] # select the options you want in your project
    

    I suggest starting out with these options: --skip-spring --skip-turbolinks as they frequently cause people starting out problems.

  2. Start up the rails server.

     vagrant$ rails s -b 0.0.0.0
    

    Since you added that config.vm.netork line above, you can access the rails server on it’s default port of 3000 with the line above.

  3. Browse to http://localhost:3000 and make sure you see the standard Rails welcome page. Make sure to check the log if anything looks wrong.

  4. Exit out of the VM shell or open another local terminal and put the finishing touches on your new project set up:

     $ git init
     $ git add --all --verbose
     $ git commit -m 'Initial Commit'
    

    Get in the habit of using source code management early and often.

  5. Edit and manage your files on your local host machine with your favourite editor, view the results by pointing your browser at http://localhost:3000, run your tests on the VM via vagrant ssh --command 'cd /vagrant && rake'.

  6. Install gems, bundle, run migrations, etc., in the VM. It’s usually a good idea to keep a terminal window open on the virtual machine to conveniently do things needed there.

Streamlining future Project Setup

All that work to create the box, now you can package it up so you don’t have to do it all again. The vagrant package command creates a box file you can save and reuse.

  1. Saving your work

    From the new_project directory on the host machine, enter:

     $ vagrant package --output my_rails.box # you can choose your own name for the box file if you wish
     $ vagrant box add --name my_rails my_rails.box
    
  2. Starting your new virtual machine

    The next time you want to create a VM for a rails project, instead of the previous vagrant init ubuntu/trusty64 command, you enter:

     $ mkdir another_new_project
     $ cd another_new_project
     $ vagrant init my_rails
    

    (Modify the Vagrantfile as needed for this project.)

     $ vagrant up
     $ vagrant ssh
    

    (Note that you may be asked for your password during initial startup. This would be your local user password.)

  3. Creating your new project

    At this point, you don’t have to do any more installation of Ruby, Rails, Git, and the rest because it’s already there! You can go straight on to creating your project with rails new.

This should give you a Rails development box suitable for beginning projects and learning your way arround Rails sufficiently.