---
slug: "ruby-rails-rubymine-vagrant"
title: "Setting Up Ruby on Rails + Vagrant + Rubymine Environment"
description: "At work, I use Python + Virtualenv + Django + PyCharm, but since a project requiring Rails came up, I set up a new environment."
url: "https://www.ytyng.com/en/blog/ruby-rails-rubymine-vagrant"
publish_date: "2017-03-12T01:00:00Z"
created: "2017-03-12T01:00:00Z"
updated: "2026-02-27T05:43:47.319Z"
categories: ["Rails"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20230812/f4ab0f9de85d4350993ce7fd109316fb.png.webp?width=768"
has_video: false
has_music: false
video_urls: []
music_urls: []
lang: "en"
---

# Setting Up Ruby on Rails + Vagrant + Rubymine Environment

<p>At work, I use Python + Virtualenv + Django + PyCharm, but since a project requiring Rails came up, I set up a new environment.</p>
<p>I'll be setting up the development environment manually using Vagrant (planning to switch to Docker once I get used to it).</p>
<p>Initially, I tried installing a specific version of Ruby directly on Ubuntu without rbenv, but it seemed more challenging without rbenv, so I decided to use it.<br /><br /></p>
<h3>Downloading RubyMine</h3>
<p><a href="https://www.jetbrains.com/ruby/" target="_blank">Ruby on Rails IDE :: JetBrains RubyMine</a></p>
<p>It's easier to install using <a href="https://www.jetbrains.com/toolbox/app/" target="_blank">JetBrains Toolbox</a></p>
<p></p>
<h3>Downloading Vagrant</h3>
<p><a href="https://www.vagrantup.com/" target="_blank">Vagrant by HashiCorp</a></p>
<p></p>
<h3>Creating Directories</h3>
<pre>mac$ cd ~/workspace<br />mac$ mkdir rails-tutorial<br />mac$ cd rails-tutorial</pre>
<p></p>
<h3>Creating a Vagrantfile</h3>
<pre>mac$ vagrant init rails-tutorial <a href="https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-i386-vagrant.box">https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-i386-vagrant.box</a></pre>
<p>The <code>Vagrantfile</code> will be created.</p>
<h3>Editing the Vagrantfile</h3>
<p>Uncomment this line:</p>
<pre style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"><span style="color: #c37522; font-style: italic;">config</span>.vm.network <span style="color: #008000; font-weight: bold;">"private_network"</span>, <span style="color: #660e7a; font-weight: bold;">ip</span>: <span style="color: #008000; font-weight: bold;">"192.168.33.10"</span></pre>
<p>Add this line:</p>
<pre style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"><span style="color: #c37522; font-style: italic;">config</span>.vm.synced_folder <span style="color: #008000; font-weight: bold;">"."</span>, <span style="color: #008000; font-weight: bold;">"/var/rails/rails-tutorial"</span></pre>
<p>Add this line (towards the end):</p>
<pre style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"><span style="color: #c37522; font-style: italic;">config</span>.vm.provision <span style="color: #008000; font-weight: bold;">"shell"</span>, <span style="color: #660e7a; font-weight: bold;">path</span>: <span style="color: #008000; font-weight: bold;">'provision/vagrant-setup-root.sh'<br /></span><span style="color: #c37522; font-style: italic;">config</span>.vm.provision <span style="color: #008000; font-weight: bold;">"file"</span>, <span style="color: #660e7a; font-weight: bold;">source</span>: <span style="color: #008000; font-weight: bold;">"provision/.rbenvrc"</span>, <span style="color: #660e7a; font-weight: bold;">destination</span>: <span style="color: #008000; font-weight: bold;">"/home/ubuntu/.rbenvrc"<br /></span><span style="color: #c37522; font-style: italic;">config</span>.vm.provision <span style="color: #008000; font-weight: bold;">"file"</span>, <span style="color: #660e7a; font-weight: bold;">source</span>: <span style="color: #008000; font-weight: bold;">"provision/vagrant-setup-ubuntu.sh"</span>, <span style="color: #660e7a; font-weight: bold;">destination</span>: <span style="color: #008000; font-weight: bold;">"/tmp/vagrant-setup-ubuntu.sh"<br /></span><span style="color: #c37522; font-style: italic;">config</span>.vm.provision <span style="color: #008000; font-weight: bold;">"shell"</span>, <span style="color: #660e7a; font-weight: bold;">inline</span>: <span style="color: #008000; font-weight: bold;">"sudo --login --user=ubuntu /usr/bin/env bash /tmp/vagrant-setup-ubuntu.sh"<br /></span></pre>
<p></p>
<h3>Creating Provisioning Scripts</h3>
<p>Create a <code>provision</code> directory and create files within it</p>
<h4>provision/.rbenvrc</h4>
<pre style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"># Script to enable rbenv<br /># Load with dot command<br />export PATH=${HOME}/.rbenv/bin:${PATH}<br />eval "$(${HOME}/.rbenv/bin/rbenv init -)"<br /><br />function runserver() {<br />  cd /var/rails/rails-tutorial/rails-tutorial/<br />  rails server -b 0.0.0.0 -p 8080<br />}<br /><br />function debugserver() {<br />  cd /var/rails/rails-tutorial/rails-tutorial/<br />  rdebug-ide --host 0.0.0.0 --port 1234 --dispatcher-port 26162 -- bin/rails s -b 0.0.0.0 -p 8080<br />}</pre>
<h4>provision/vagrant-setup-root.sh</h4>
<pre style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"><span style="font-weight: bold;">#!/usr/bin/env bash<br /></span><span style="font-weight: bold;"><br /></span><span style="color: #000080; font-weight: bold;">if [[ </span>! <span style="color: #000080; font-weight: bold;">-d </span>/var/rails<span style="color: #000080; font-weight: bold;"> ]]</span>; <span style="color: #000080; font-weight: bold;">then<br /></span><span style="color: #000080; font-weight: bold;">    </span>mkdir -p /var/rails<br />    chown ubuntu:ubuntu /var/rails<br /><span style="color: #000080; font-weight: bold;">fi<br /></span><span style="color: #000080; font-weight: bold;"><br /></span>apt update -y<br />apt install -y git build-essential<br />apt install -y libssl-dev libreadline-dev zlib1g-dev</pre>
<h4>provision/vagrant-setup-ubuntu.sh</h4>
<pre style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"><span style="font-weight: bold;">#!/usr/bin/env bash<br /></span><span style="font-weight: bold;"><br /></span>ruby_version=2.4.0<br /><br /><span style="color: #000080; font-weight: bold;">[[ -d </span><span style="color: #008000; font-weight: bold;">"${HOME}/.rbenv"</span><span style="color: #000080; font-weight: bold;"> ]] || </span>{<br />  git clone https://github.com/sstephenson/rbenv.git ~/.rbenv<br />  git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build<br />}<br /><br />grep .rbenvrc <span style="color: #000080; font-weight: bold;">$</span>{HOME}/.bashrc <span style="color: #000080; font-weight: bold;">|| </span>{<br />  <span style="color: #0b0c95;">echo </span><span style="color: #008000; font-weight: bold;">'. ~/.rbenvrc' </span><span style="color: #5a5a5a; font-style: italic;">&gt;&gt; ${HOME}/.bashrc</span>;<br />}<br /><br /><span style="color: #0b0c95;">. </span>~/.rbenvrc<br /><br />rbenv versions |grep <span style="color: #000080; font-weight: bold;">$</span>{ruby_version} <span style="color: #000080; font-weight: bold;">|| </span>{<br />  rbenv install <span style="color: #000080; font-weight: bold;">$</span>{ruby_version}<br />  rbenv global <span style="color: #000080; font-weight: bold;">$</span>{ruby_version}<br />}<br /><br />which rails <span style="color: #000080; font-weight: bold;">|| </span>gem install rails</pre>
<p>Note: <code>rbenv</code> and <code>ruby-build</code> can also be installed with <code>apt install rbenv ruby-build</code>, but due to older versions, fewer Ruby versions are available. It's better to install via git clone.</p>
<h3>Starting the Environment as a Test</h3>
<pre>mac$ cd ..<br />mac$ vagrant up</pre>
<p></p>
<p>Note: To regenerate the virtual machine, use <code>vagrant reload</code></p>
<p>Note: To rerun only the provisioning script, use <code>vagrant provision</code></p>
<p></p>
<p></p>
<h3>Logging into the Virtual Machine via SSH</h3>
<pre>mac$ vagrant ssh</pre>
<p>&rarr; You should be able to log in.</p>
<pre>ubuntu$ ruby --version<br />ruby 2.4.0p0 (2016-12-24 revision 57164) [i686-linux]</pre>
<p></p>
<p></p>
<h2>Setting up the Initial Rails Environment</h2>
<p>After logging in with <code>vagrant ssh</code></p>
<pre>ubuntu$ cd /var/rails/rails-tutorial/<br />ubuntu$ rails new rails-tutorial</pre>
<p>&rarr; <code>/var/rails/rails-tutorial/rails-tutorial/</code> will be created.</p>
<p>Since it mounts the host directory, you can see that the <code>rails-tutorial</code> directory within the <code>rails-tutorial</code> project directory on the host (i.e., <code>~/workspace/rails-tutorial/rails-tutorial</code>) has been created.</p>
<h3>Adding Debugging Gems</h3>
<p>There is a <code>Gemfile</code> inside <code>/var/rails/rails-tutorial/rails-tutorial/</code>. You can modify it either on the host side or the virtual machine side.</p>
<p></p>
<h3>Setting up Libraries for the Virtual Machine</h3>
<pre style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"><span style="color: #808080; font-style: italic;"># gem </span><span style="color: #808080; font-weight: bold; font-style: italic;">'therubyracer'</span><span style="color: #808080; font-style: italic;">, platforms: </span><span style="color: #808080; font-weight: bold; font-style: italic;">:ruby</span></pre>
<p>▲ Uncomment this line.</p>
<p></p>
<h3>Setting up Debugging Gems</h3>
<p>Add to the Gemfile (within <span style="font-style: italic;">group </span><span style="color: #660e7a; font-weight: bold;">:development</span>, <span style="color: #660e7a; font-weight: bold;">:test </span><span style="color: #000080; font-weight: bold;">do </span>, etc.)</p>
<pre style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"><span style="font-style: italic;">gem </span><span style="color: #008000; font-weight: bold;">'ruby-debug-ide'<br /></span><span style="font-style: italic;">gem </span><span style="color: #008000; font-weight: bold;">'debase'</span></pre>
<p></p>
<h3>Running Bundle Install for Libraries</h3>
<p>While logged in with <code>vagrant ssh</code></p>
<pre>ubuntu$ cd /var/rails/rails-tutorial/rails-tutorial/<br />ubuntu$ bundle install</pre>
<p>&rarr; Libraries will be installed.</p>
<p></p>
<h2>Running a Test Server</h2>
<p>After logging into the virtual machine via SSH</p>
<pre>ubuntu$ runserver</pre>
<p></p>
<p>Or, from the host PC</p>
<pre>mac$ vagrant ssh -c ". ~/.rbenvrc; runserver"</pre>
<p></p>
<h3>Test Display</h3>
<p>Access <a href="http://192.168.33.10:8080/" target="_blank">http://192.168.33.10:8080/</a> from your browser</p>
<p>&rarr; You should be able to see it.</p>
<p></p>
<p></p>
<h2>Setting up Remote Debugging</h2>
<p></p>
<p>Open the project directory in RubyMine</p>
<p>From the menu bar, go to <code>Run</code> &rarr; <code>Edit Configuration</code></p>
<p>Click <code>+</code> &rarr; <code>Ruby remote debug</code></p>
<p>Name: <code>remote debug</code> or any suitable name</p>
<p>Remote host: <code>192.168.33.10</code></p>
<p>Remote port: Keep <code>1234</code></p>
<p>Remote root folder: <code>/var/rails/rails-tutorial</code></p>
<p>Local port: Keep <code>26162</code></p>
<p>Local root folder: <code>/Users/ytyng/workspace/rails-tutorial</code></p>
<p>&rarr; Click OK</p>
<h3>Starting the Debug Server</h3>
<p>After logging into the virtual machine via <code>vagrant ssh</code></p>
<pre>ubuntu$ debugserver</pre>
<p></p>
<p>Or, from the host PC</p>
<pre>mac$ vagrant ssh -c ". ~/.rbenvrc; debugserver"</pre>
<p></p>
<p>Once the debug server is running, start debugging with the <code>remote debug</code> configuration in RubyMine: <code>Run</code> &rarr; <code>Debug...</code> &rarr; <code>remote debug</code> &rarr; <code>debug</code> (For those with the menu bar displayed, click the bug button)</p>
<p>&rarr; There will be a response in the terminal connected to Vagrant.</p>
<p></p>
<p>Access <a href="http://192.168.33.10:8080/" target="_blank">http://192.168.33.10:8080/</a> from your browser</p>
<p>&rarr; You should be able to see it.</p>
<p></p>
<p>With this, you should now be able to set breakpoints and debug.</p>
