{"id":406,"date":"2013-09-17T12:56:36","date_gmt":"2013-09-17T12:56:36","guid":{"rendered":"https:\/\/intelligentbee.com\/blog\/?p=406"},"modified":"2024-09-30T07:58:09","modified_gmt":"2024-09-30T07:58:09","slug":"setup-a-simple-ruby-2-on-rails-4-environment-with-vagrant-and-virtual-box","status":"publish","type":"post","link":"https:\/\/intelligentbee.com\/blog\/setup-a-simple-ruby-2-on-rails-4-environment-with-vagrant-and-virtual-box\/","title":{"rendered":"Setup A Simple Ruby 2 On Rails 4 Environment With Vagrant And Virtual Box"},"content":{"rendered":"<h2>Ruby 2 on rails 4 environment<\/h2>\n<p>Since a developer time is most valuable when dealing with actual &#8230; 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.<!--more--><\/p>\n<p>That being said AND the fact that I am kinda lazy developer (to be read as &#8220;I automate everything so I won&#8217;t have to do it again&#8221;) &#8211; here&#8217;s how to setup a new Rails environment that can be reused, reinstalled easily by anyone in your team.<\/p>\n<p>I&#8217;ll be talking about the mac OS X now, a Windows similar blog will come in the near future.<\/p>\n<p>What you need:<\/p>\n<ul>\n<li><a title=\"Vagrant getting started guide\" href=\"http:\/\/docs.vagrantup.com\/v2\/getting-started\/index.html\" target=\"_blank\" rel=\"noopener\">Vagrant<\/a> installed on your local machine (OS X)<\/li>\n<\/ul>\n<h3>What you&#8217;ll get:<\/h3>\n<ul>\n<li>a fully functional virtual machine (Ubuntu 12.04) that you can: start, use, shutdown, share with your coworkers.<\/li>\n<li><code>Rails 4<\/code> and <code>Ruby 2<\/code> environment<\/li>\n<li>working dir shared with your local machine so you can use your own IDE and have your work testable in the virtual machine instantly<\/li>\n<\/ul>\n<h3>How to do it:<\/h3>\n<ol>\n<li>download and install <code>Vagrant<\/code> &#8211; I use version 1.2.7 You can check that with:\n<pre class=\"theme:tomorrow-night line-height:20 toolbar:2 nums:false lang:default decode:true \">vagrant -v<\/pre>\n<p>You should see:<\/p>\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">Vagrant version 1.2.7<\/pre>\n<\/li>\n<li>create a folder for your Rails application and go to it\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">mkdir rorapp\r\ncd rorapp<\/pre>\n<\/li>\n<li>initialize your Vagrant machine\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">vagrant init precise32 http:\/\/files.vagrantup.com\/precise32.box<\/pre>\n<p>This is your virtual machine (server) that will hold and run your Rails application.<br \/>\nIf you check now, Vagrant created a <code>config<\/code> file in your <code>rorapp<\/code> folder, called <code>Vagrantfile<\/code>. You&#8217;ll change that a little on next step.<\/li>\n<li>make some setup configuration before starting your VM<br \/>\nSince you&#8217;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.<br \/>\nFor that, please edit the <code>Vagrantfile<\/code> and change the port forwarding setting.<br \/>\nFind the next line:<\/p>\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\"># config.vm.network :forwarded_port, guest: 80, host: 8080<\/pre>\n<p>change it as below (don&#8217;t forget to uncomment it: delete the <code>\"#\"<\/code> before it)<\/p>\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">config.vm.network :forwarded_port, guest: 3000, host: 3000<\/pre>\n<p>Cool. You&#8217;ve just forwarded everything that your local machine browser calls on port 3000 to your vagrant VM on the same port.<\/li>\n<li>start your Vagrant machine\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">vagrant up<\/pre>\n<p>After that, you should see something like<\/p>\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">vagrant up\r\n[default] Setting the name of the VM...\r\n[default] Clearing any previously set forwarded ports...\r\n[default] Creating shared folders metadata...\r\n[default] Clearing any previously set network interfaces...\r\n[default] Preparing network interfaces based on configuration...\r\n[default] Forwarding ports...\r\n[default] -- 22 =&gt; 2222 (adapter 1)\r\n[default] -- 3000 =&gt; 3000 (adapter 1)\r\n[default] Booting VM...\r\n[default] Waiting for VM to boot. This can take a few minutes.\r\n[default] VM booted and ready for use!\r\n[default] Mounting shared folders...\r\n[default] -- \/vagrant<\/pre>\n<p>Note how it tells you it just forwarded the 3000 port on <code>adapter1<\/code>.<br \/>\nAlso, your current folder on local machine (<code>rorapp<\/code>) is mounted as <code>\/vagrant<\/code> 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.<\/li>\n<li>after VM is started, then access it through SSH\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">vagrant ssh<\/pre>\n<p>This will open up a SSH to your VM<\/li>\n<li>In your VM, go to your application folder<br \/>\n<code>cd \/vagrant<\/code><br \/>\nNote: 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.<br \/>\nYou can do something like:<\/p>\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">nano Vagrantfile<\/pre>\n<p>or you can use your favorite console editor for that &#8230;<\/li>\n<li>Do an apt-get update, to be sure you have the latest\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\"> sudo apt-get update<\/pre>\n<\/li>\n<li>We&#8217;re going to install the Ruby 2.* version using RVM, and for that we need curl installed too.\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">sudo apt-get install curl<\/pre>\n<p>Then install RVM and latest Ruby version, in one single command:<\/p>\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">curl -L https:\/\/get.rvm.io | bash -s stable --ruby<\/pre>\n<p>Reload your profile:<\/p>\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">source ~\/.profile<\/pre>\n<p>You should have rvm installed just fine now:<\/p>\n<pre class=\"theme:tomorrow-night toolbar:1 nums:false lang:default decode:true\">vagrant@precise32:\/vagrant$ rvm -v\r\nrvm 1.22.3 (stable) by Wayne E. Seguin &lt;wayneeseguin@gmail.com&gt;, Michal Papis &lt;mpapis@gmail.com&gt; [https:\/\/rvm.io\/]<\/pre>\n<p>And also the Ruby should be at its latest version:<\/p>\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">vagrant@precise32:\/vagrant$ rvm list\r\n\r\nrvm rubies\r\n\r\n=* ruby-2.0.0-p247 [ i686 ]\r\n\r\n# =&gt; - current\r\n# =* - current &amp;&amp; default\r\n# * - default<\/pre>\n<p>But you&#8217;re not yet using the latest:<\/p>\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\"> ruby -v<\/pre>\n<\/li>\n<li>Add this line to your ~\/.profile file:\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">[[ -s \"$HOME\/.rvm\/scripts\/rvm\" ]] &amp;&amp; . \"$HOME\/.rvm\/scripts\/rvm\"<\/pre>\n<p>Otherwise, it&#8217;s possible that you cannot change between the Ruby versions &#8211; more details <a href=\"http:\/\/rvm.io\/integration\/gnome-terminal\">here<\/a>;<\/li>\n<li>Now you can switch to Ruby2.0 with the following command:\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">rvm use ruby-2.0.0-p247<\/pre>\n<p>You can check your version with:<\/p>\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">vagrant@precise32:\/vagrant$ ruby -v\r\nruby 2.0.0p247 (2013-06-27 revision 41674) [i686-linux]<\/pre>\n<p>You&#8217;re done with Ruby, let&#8217;s take care of the Rails too, then move forward to our application<\/li>\n<li>Install Rails\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">gem install rails<\/pre>\n<p>this installs the latest version of Rails.<br \/>\nIt will install a bunch of packages (takes a while too).<br \/>\nCheck the version with<\/p>\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">vagrant@precise32:\/vagrant$ rails -v\r\nRails 4.0.0<\/pre>\n<\/li>\n<li>Ok, now that you have Ruby2 and Rails4 installed, let&#8217;s create a sample application to check the whole thing.<br \/>\nCreate the app with<\/p>\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">rails new \/vagrant<\/pre>\n<p>this will install a new Ruby on Rails application in the \/vagrant folder (current one)<br \/>\nNow, before you fire it, you need to do a small change. Open your Gemfile and make sure you have the following packages in there:<\/p>\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">gem 'execjs'\r\ngem 'therubyracer'<\/pre>\n<p>If you don&#8217;t have it there, add it and then run<\/p>\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">bundle install<\/pre>\n<\/li>\n<li>Let&#8217;s run it and see what we&#8217;ve done\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">rails server<\/pre>\n<p>you should see it running like this:<\/p>\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:default decode:true\">vagrant@precise32:\/vagrant$ rails server\r\n=&gt; Booting WEBrick\r\n=&gt; Rails 4.0.0 application starting in development on http:\/\/0.0.0.0:3000\r\n=&gt; Run `rails server -h` for more startup options\r\n=&gt; Ctrl-C to shutdown server\r\n[2013-09-02 14:57:14] INFO WEBrick 1.3.1\r\n[2013-09-02 14:57:14] INFO ruby 2.0.0 (2013-06-27) [i686-linux]\r\n[2013-09-02 14:57:14] INFO WEBrick::HTTPServer#start: pid=12919 port=3000<\/pre>\n<p>Then go to your local machine, open a browser window and go to: http:\/\/localhost:3000<br \/>\nIf all went well, you should be seeing something like this<\/li>\n<\/ol>\n<p>That&#8217;s all, enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ruby 2 on rails 4 environment Since a developer time is most valuable when dealing with actual &#8230; development tasks, [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[84],"tags":[132,204,214,252,256],"yst_prominent_words":[384,639,700,990,1149,1440],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/406"}],"collection":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/comments?post=406"}],"version-history":[{"count":8,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/406\/revisions"}],"predecessor-version":[{"id":133247,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/406\/revisions\/133247"}],"wp:attachment":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media?parent=406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/categories?post=406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/tags?post=406"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}