Ruby 2 on rails 4 environment
Since a developer time is most valuable when dealing with actual … development tasks, setting up the environment for a new project better take less or no time, so that the developer can focus on building the actual product rather than focusing on setting up things.
That being said AND the fact that I am kinda lazy developer (to be read as “I automate everything so I won’t have to do it again”) – here’s how to setup a new Rails environment that can be reused, reinstalled easily by anyone in your team.
I’ll be talking about the mac OS X now, a Windows similar blog will come in the near future.
What you need:
- Vagrant installed on your local machine (OS X)
What you’ll get:
- a fully functional virtual machine (Ubuntu 12.04) that you can: start, use, shutdown, share with your coworkers.
Rails 4
andRuby 2
environment- working dir shared with your local machine so you can use your own IDE and have your work testable in the virtual machine instantly
How to do it:
- download and install
Vagrant
– I use version 1.2.7 You can check that with:vagrant -v
You should see:
Vagrant version 1.2.7
- create a folder for your Rails application and go to it
mkdir rorapp cd rorapp
- initialize your Vagrant machine
vagrant init precise32 http://files.vagrantup.com/precise32.box
This is your virtual machine (server) that will hold and run your Rails application.
If you check now, Vagrant created aconfig
file in yourrorapp
folder, calledVagrantfile
. You’ll change that a little on next step. - make some setup configuration before starting your VM
Since you’ll want your application to run on VM (as server) but access it from your local machine (client) by simply going to an URL in your browser, you need to forward everything that calls port 3000 to your VM.
For that, please edit theVagrantfile
and change the port forwarding setting.
Find the next line:# config.vm.network :forwarded_port, guest: 80, host: 8080
change it as below (don’t forget to uncomment it: delete the
"#"
before it)config.vm.network :forwarded_port, guest: 3000, host: 3000
Cool. You’ve just forwarded everything that your local machine browser calls on port 3000 to your vagrant VM on the same port.
- start your Vagrant machine
vagrant up
After that, you should see something like
vagrant up [default] Setting the name of the VM... [default] Clearing any previously set forwarded ports... [default] Creating shared folders metadata... [default] Clearing any previously set network interfaces... [default] Preparing network interfaces based on configuration... [default] Forwarding ports... [default] -- 22 => 2222 (adapter 1) [default] -- 3000 => 3000 (adapter 1) [default] Booting VM... [default] Waiting for VM to boot. This can take a few minutes. [default] VM booted and ready for use! [default] Mounting shared folders... [default] -- /vagrant
Note how it tells you it just forwarded the 3000 port on
adapter1
.
Also, your current folder on local machine (rorapp
) is mounted as/vagrant
on your VM. Here we will build our Rails application forward. This means you can easily use your favorite IDE on local machine and your code changes will be instantly saved in your VM too. - after VM is started, then access it through SSH
vagrant ssh
This will open up a SSH to your VM
- In your VM, go to your application folder
cd /vagrant
Note: you can easily check that is the same folder (rorapp on your local machine) by peaking into the only file there: Vagrantfile. Go ahead and open it in your VM and notice your previous change for the 3000 port.
You can do something like:nano Vagrantfile
or you can use your favorite console editor for that …
- Do an apt-get update, to be sure you have the latest
sudo apt-get update
- We’re going to install the Ruby 2.* version using RVM, and for that we need curl installed too.
sudo apt-get install curl
Then install RVM and latest Ruby version, in one single command:
curl -L https://get.rvm.io | bash -s stable --ruby
Reload your profile:
source ~/.profile
You should have rvm installed just fine now:
vagrant@precise32:/vagrant$ rvm -v rvm 1.22.3 (stable) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]
And also the Ruby should be at its latest version:
vagrant@precise32:/vagrant$ rvm list rvm rubies =* ruby-2.0.0-p247 [ i686 ] # => - current # =* - current && default # * - default
But you’re not yet using the latest:
ruby -v
- Add this line to your ~/.profile file:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
Otherwise, it’s possible that you cannot change between the Ruby versions – more details here;
- Now you can switch to Ruby2.0 with the following command:
rvm use ruby-2.0.0-p247
You can check your version with:
vagrant@precise32:/vagrant$ ruby -v ruby 2.0.0p247 (2013-06-27 revision 41674) [i686-linux]
You’re done with Ruby, let’s take care of the Rails too, then move forward to our application
- Install Rails
gem install rails
this installs the latest version of Rails.
It will install a bunch of packages (takes a while too).
Check the version withvagrant@precise32:/vagrant$ rails -v Rails 4.0.0
- Ok, now that you have Ruby2 and Rails4 installed, let’s create a sample application to check the whole thing.
Create the app withrails new /vagrant
this will install a new Ruby on Rails application in the /vagrant folder (current one)
Now, before you fire it, you need to do a small change. Open your Gemfile and make sure you have the following packages in there:gem 'execjs' gem 'therubyracer'
If you don’t have it there, add it and then run
bundle install
- Let’s run it and see what we’ve done
rails server
you should see it running like this:
vagrant@precise32:/vagrant$ rails server => Booting WEBrick => Rails 4.0.0 application starting in development on http://0.0.0.0:3000 => Run `rails server -h` for more startup options => Ctrl-C to shutdown server [2013-09-02 14:57:14] INFO WEBrick 1.3.1 [2013-09-02 14:57:14] INFO ruby 2.0.0 (2013-06-27) [i686-linux] [2013-09-02 14:57:14] INFO WEBrick::HTTPServer#start: pid=12919 port=3000
Then go to your local machine, open a browser window and go to: http://localhost:3000
If all went well, you should be seeing something like this
That’s all, enjoy!
8 Comments
Comments are closed.
Hi,
Thanks a lot for the instructions! Would you be able to tell how I can now distribute this to other developers, so that they needn’t setup this all over again?
Thanks.
Okay, I figured that out. Answer is here: http://superuser.com/questions/635256/copy-vagrant-box-locally
Great post!! Thanks for sharing.
Hey, would be nice if you published the Windows version of this tutorial 🙂
It’s the same for windows. Except that to install vagrant you just download the installer from vagrantup.com
Can you please advise how to install on Windows 8. Tried installing in windows to no avail. Would like to try VM way.
Hi,
Thanks for this post !!
Just a precision : Reload your profile after step 10
In case anyone stumbles across this helpful article now, Rails 4.2 binds to 127.0.0.1 now (default), NOT 0.0.0.0
I was trying to troubleshoot why everything was set up correctly with my forwarded_port, but on my local machine, I could not view anything on.
Instead of:
rails server
Use:
bin/rails server -b 0.0.0.0
Still a very helpful article 🙂
—
Article:
http://stackoverflow.com/questions/27799260/rails-4-2-server-port-forwarding-on-vagrant-does-not-work
Credit: Max Woolf
http://stackoverflow.com/users/1029521/max-woolf