Setting Up Ruby on Rails + Vagrant + Rubymine Environment

Rails
2017-03-12 10:00 (7 years ago) ytyng

At work, I use Python + Virtualenv + Django + PyCharm, but since a project requiring Rails came up, I set up a new environment.

I'll be setting up the development environment manually using Vagrant (planning to switch to Docker once I get used to it).

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.

Downloading RubyMine

Ruby on Rails IDE :: JetBrains RubyMine

It's easier to install using JetBrains Toolbox

Downloading Vagrant

Vagrant by HashiCorp

Creating Directories

mac$ cd ~/workspace
mac$ mkdir rails-tutorial
mac$ cd rails-tutorial

Creating a Vagrantfile

mac$ vagrant init rails-tutorial https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-i386-vagrant.box

The Vagrantfile will be created.

Editing the Vagrantfile

Uncomment this line:

config.vm.network "private_network", ip: "192.168.33.10"

Add this line:

config.vm.synced_folder ".", "/var/rails/rails-tutorial"

Add this line (towards the end):

config.vm.provision "shell", path: 'provision/vagrant-setup-root.sh'
config.vm.provision "file", source: "provision/.rbenvrc", destination: "/home/ubuntu/.rbenvrc"
config.vm.provision "file", source: "provision/vagrant-setup-ubuntu.sh", destination: "/tmp/vagrant-setup-ubuntu.sh"
config.vm.provision "shell", inline: "sudo --login --user=ubuntu /usr/bin/env bash /tmp/vagrant-setup-ubuntu.sh"

Creating Provisioning Scripts

Create a provision directory and create files within it

provision/.rbenvrc

# Script to enable rbenv
# Load with dot command
export PATH=${HOME}/.rbenv/bin:${PATH}
eval "$(${HOME}/.rbenv/bin/rbenv init -)"

function runserver() {
cd /var/rails/rails-tutorial/rails-tutorial/
rails server -b 0.0.0.0 -p 8080
}

function debugserver() {
cd /var/rails/rails-tutorial/rails-tutorial/
rdebug-ide --host 0.0.0.0 --port 1234 --dispatcher-port 26162 -- bin/rails s -b 0.0.0.0 -p 8080
}

provision/vagrant-setup-root.sh

#!/usr/bin/env bash

if [[ ! -d /var/rails ]]; then
mkdir -p /var/rails
chown ubuntu:ubuntu /var/rails
fi

apt update -y
apt install -y git build-essential
apt install -y libssl-dev libreadline-dev zlib1g-dev

provision/vagrant-setup-ubuntu.sh

#!/usr/bin/env bash

ruby_version=2.4.0

[[ -d "${HOME}/.rbenv" ]] || {
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
}

grep .rbenvrc ${HOME}/.bashrc || {
echo '. ~/.rbenvrc' >> ${HOME}/.bashrc;
}

. ~/.rbenvrc

rbenv versions |grep ${ruby_version} || {
rbenv install ${ruby_version}
rbenv global ${ruby_version}
}

which rails || gem install rails

Note: rbenv and ruby-build can also be installed with apt install rbenv ruby-build, but due to older versions, fewer Ruby versions are available. It's better to install via git clone.

Starting the Environment as a Test

mac$ cd ..
mac$ vagrant up

Note: To regenerate the virtual machine, use vagrant reload

Note: To rerun only the provisioning script, use vagrant provision

Logging into the Virtual Machine via SSH

mac$ vagrant ssh

→ You should be able to log in.

ubuntu$ ruby --version
ruby 2.4.0p0 (2016-12-24 revision 57164) [i686-linux]

Setting up the Initial Rails Environment

After logging in with vagrant ssh

ubuntu$ cd /var/rails/rails-tutorial/
ubuntu$ rails new rails-tutorial

/var/rails/rails-tutorial/rails-tutorial/ will be created.

Since it mounts the host directory, you can see that the rails-tutorial directory within the rails-tutorial project directory on the host (i.e., ~/workspace/rails-tutorial/rails-tutorial) has been created.

Adding Debugging Gems

There is a Gemfile inside /var/rails/rails-tutorial/rails-tutorial/. You can modify it either on the host side or the virtual machine side.

Setting up Libraries for the Virtual Machine

# gem 'therubyracer', platforms: :ruby

▲ Uncomment this line.

Setting up Debugging Gems

Add to the Gemfile (within group :development, :test do , etc.)

gem 'ruby-debug-ide'
gem 'debase'

Running Bundle Install for Libraries

While logged in with vagrant ssh

ubuntu$ cd /var/rails/rails-tutorial/rails-tutorial/
ubuntu$ bundle install

→ Libraries will be installed.

Running a Test Server

After logging into the virtual machine via SSH

ubuntu$ runserver

Or, from the host PC

mac$ vagrant ssh -c ". ~/.rbenvrc; runserver"

Test Display

Access http://192.168.33.10:8080/ from your browser

→ You should be able to see it.

Setting up Remote Debugging

Open the project directory in RubyMine

From the menu bar, go to RunEdit Configuration

Click +Ruby remote debug

Name: remote debug or any suitable name

Remote host: 192.168.33.10

Remote port: Keep 1234

Remote root folder: /var/rails/rails-tutorial

Local port: Keep 26162

Local root folder: /Users/ytyng/workspace/rails-tutorial

→ Click OK

Starting the Debug Server

After logging into the virtual machine via vagrant ssh

ubuntu$ debugserver

Or, from the host PC

mac$ vagrant ssh -c ". ~/.rbenvrc; debugserver"

Once the debug server is running, start debugging with the remote debug configuration in RubyMine: RunDebug...remote debugdebug (For those with the menu bar displayed, click the bug button)

→ There will be a response in the terminal connected to Vagrant.

Access http://192.168.33.10:8080/ from your browser

→ You should be able to see it.

With this, you should now be able to set breakpoints and debug.

Current rating: 5

Comments

Archive

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011