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.
Ruby on Rails IDE :: JetBrains RubyMine
It's easier to install using JetBrains Toolbox
mac$ cd ~/workspace
mac$ mkdir rails-tutorial
mac$ cd rails-tutorial
mac$ vagrant init rails-tutorial https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-i386-vagrant.box
The Vagrantfile
will be created.
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"
Create a provision
directory and create files within it
# 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
}
#!/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
#!/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.
mac$ cd ..
mac$ vagrant up
Note: To regenerate the virtual machine, use vagrant reload
Note: To rerun only the provisioning script, use vagrant provision
mac$ vagrant ssh
→ You should be able to log in.
ubuntu$ ruby --version
ruby 2.4.0p0 (2016-12-24 revision 57164) [i686-linux]
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.
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.
# gem 'therubyracer', platforms: :ruby
▲ Uncomment this line.
Add to the Gemfile (within group :development, :test do , etc.)
gem 'ruby-debug-ide'
gem 'debase'
While logged in with vagrant ssh
ubuntu$ cd /var/rails/rails-tutorial/rails-tutorial/
ubuntu$ bundle install
→ Libraries will be installed.
After logging into the virtual machine via SSH
ubuntu$ runserver
Or, from the host PC
mac$ vagrant ssh -c ". ~/.rbenvrc; runserver"
Access http://192.168.33.10:8080/ from your browser
→ You should be able to see it.
Open the project directory in RubyMine
From the menu bar, go to Run
→ Edit 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
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: Run
→ Debug...
→ remote debug
→ debug
(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.
Comments