A place to breathe

Sunday, August 31, 2014

A new kind of animal: VM, Vagrant and Rails

Now, I manage to run a Vagrant provided by Discourse team. Next, I want to serve static page on Rails. 

All I want to do is to serve a static page. And now let's see what kind of mess I am getting into!
 
Once I plunged into Rails development, I realize that it is hard to develop on my local machine. I don't want to mess up my Mac environment with silly Rails gems dependencies, I am willing to invest a bit more time to understand all kind of Ruby tools. For all its worth, you better be good, Ruby on Rails!!!
  
My idea is to install a Virtual box using Vagrant so that I can ssh into a virtual box. Plus, I will run a Ruby environment within the Vagrant and install all the "germs" (yeah, pun intended) into that vagrant (read: it's not a misspelled of Viagra).
Now that's clean.

First, get the Vagrant image. Create a project folder, say "myapp" and make the source code development "into" a virtual box: 

mkdir myapp
cd myapp 
vagrant init precise32 http://files.vagrantup.com/precise32.box 

This will download a Vagrant config file called "Vagrantfile". Now, I want to host my Rails on port 3000 (following convention), so that, I edit the file here:

config.vm.network :forwarded_port, guest: 3000, host: 3000

Now, we're ready to configure the machines. Issue the following command:

vagrant up

Now, what it does is to do some magic of creating a default VM environment for your "myapp" folder so that you can build an isolated environment within that folder. To access that isolated environment, you need to remote login using ssh :

vagrant ssh
cd /vagrant

You now see your "Vagrantfile". Basically your "myapp" folder has been mounted into "/vagrant" folder in the VM. Cool so far? Run everything to get Rails started:

sudo apt-get update
sudo apt-get install curl 
\curl -sSL https://get.rvm.io | bash -s stable --rails 

Try check your rails:

vagrant@precise32:~$ which rails
/home/vagrant/.rvm/gems/ruby-2.1.3/bin/rails

I feel so safe that all these commands live within the Ubuntu VM on my Mac. At least if things screws up, I can just forget about this VM and starts over.

vagrant@precise32:~$ rvm -v
rvm 1.25.29 (stable) by Wayne E. Seguin , Michal Papis [https://rvm.io/]


A note of caution here: make sure you are running rails version from your home's RVM!.

which rvm 
/home/vagrant/.rvm/bin/rvm 

Now, install Rails. Because you need Rails to create projects.

gem install rails
rails new /vagrant 

make sure you have 'therubyracer' and 'execjs' inside your Gemfile.

bundle install 
rails server 


Go to "http://localhost:3000" . See something? Rejoice !!









No comments:

About Me

I'm currently a software engineer. My specific interest is games and networking. I'm running software company called Nusantara Software.