One of the hardest parts of getting started in Rails development is setting up your computer. One of the hardest parts of teaching rails development is dealing with the all the special case problems that arise when students are running different development environments on different operating systems. One solution I’ve heard proposed from a couple different sources is to have all beginners develop in a vagrant virtual machine, that way the teacher only has to deal with the peculiarities of one environment and students can follow one simple set of instructions to set up their computers.
The problem is that poking around the Internet, I couldn’t find a beginner friendly simple set of instructions anywhere or a vagrant vm box setup specifically for Rails development beginners. This blog entry will hopefully provide Rails beginners with both simple instructions to get started and virtual machine to download.
1. A shell with ssh.
The first thing you need is a shell with ssh. If your using OSX then your computer already has one installed, it’s in applications > utilities and it is called Terminal. Get acquainted, maybe go into the preferences and set the colours to something you like. As a Rails developer you are about to start spending a lot of time in Terminal.
If you are using windows, you’re going to have download something. I recommend Git. Git is a distributed version control system that as a developer you’ll be using to work collaboratively on your Rails projects. But we are not worrying about that now. Right now we are simply going to be taking advantage of the nice shell environment with ssh that is integrated into the Windows installation of Git.
So download and install it. During the install process there will be an option to put a shortcut on your desktop. I recommend doing so, you’ll be using this shell every time you use Rails. The shortcut will make your life easier.
2. An Amazing Text Editor
The next thing you need is a text editor. I’m going to recommend Sublime Text 2 because it is popular and available for all operating systems. Download and install it.
Now we are starting to build our virtual environment, a computer that will be running inside your computer! Sort of like the movie Inception but with operating systems instead of dreams. So download and install VirtualBox. If you are using windows you don’t need to install the desktop short cut for VirtualBox. It does most of its magic in the background, you shouldn’t have to open it often.
4. Install & Setup Vagrant
So now its time to put all this together to setup your virtual Rails environment. Bring up your shell prompt, this would be Terminal on OSX or the Git Shell we installed in step one for windows.
First lets make sure you are in your home directory, enter the command:
Next we will make a directory for your rails projects and then enter that directory:
This will be the shared directory for your vagrant virtual machine. A directory that exists both on your computer and in the virtual computer you are setting up. I’ve setup a vagrant box running Ubuntu Precise 32bit with RVM, Ruby 2, Rails 4, SQLite3, Curl and Git installed. We need to give vagrant the location where it can download that box and use it to setup your local virtual rails development environment. To do this use the following command:
vagrant init beginner-box https://s3-us-west-2.amazonaws.com/rails-beginner-box/rails-beginner.box
This will create a
Vagrantfile in the current directory which should be the
rails_projects directory. Now you need to use the text editor you downloaded in step 2 to edit this
Vagrantfile. You are going to setup port forwarding from your virtual computer to your real computer so that you can see the app you are developing on your virtual machine in your browser. Open the
Vagrantfile in your text editor and around line 22 you should see a line that reads:
# config.vm.network :forwarded_port, guest: 80, host: 8080
Change it to the following and save the changes:
config.vm.network :forwarded_port, guest: 3000, host: 3000
Next go back to your shell prompt and enter the following command to download and startup your virtual machine:
This should take some time. Once your shell prompt returns, your virtual environment should be up and running! Next you want to access it so in your shell prompt enter the following command:
Your shell prompt should now look different indicating you are now interacting directly with your virtual rails development environment! Next navigate to your shared directory using the following command:
Now setup your first rails app using the command:
rails new my_first_app
and use your text editor via your rails_project directory to edit your app. Now enjoy riding Ruby on Rails!
- This virtual environment requires
- To access your rails development server when it is running you should just have to point your browser to localhost:3000 as normal because of the port forwarding we setup in step 4.
Without these references this guild wouldn’t have been possible: