Setting up local rails development

published 16.12.2020

backendweb appdevopsquick tip

After years, I got back to ruby and rails development. There's a lot of exciting changes in language, framework and gems, and I couldn't wait to dive into coding in RoR again.

I've decided I'll start light on the env setup, and just spin up a good old Vagrant, to not mess up my machine environment. Various ruby/rails versions need different versions of gems, fsevents, ffi, nokogiri, and postgresql, which usually mess things up.

As of today, this is my Vagrantfile to get a decent starting point for a new rails app:

Vagrant.configure("2") do |config|
  # You can search for boxes at = "ubuntu/bionic64"

  # Accessing "localhost:8100" will access port 3000 on the guest machine. "forwarded_port", guest: 3000, host: 8100
  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via to disable public access
  # "forwarded_port", guest: 80, host: 8080, host_ip: ""
  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # "private_network", ip: ""
  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # "public_network"

  # Share an additional folder to the guest VM.
  # The first argument is the path on the host to the actual folder.
  # The second argument is the path on the guest to mount the folder.
  config.vm.synced_folder ".", "/app"

  config.vm.provider "virtualbox" do |vb|
    vb.memory = "4096"
    vb.cpus = 2

  config.vm.provision "shell", inline: <<-SHELL
    apt-get update
    apt-get install -y curl
    curl -sL | sudo -E bash -
    curl -sS | sudo apt-key add -
    echo "deb stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
    apt-get update
    apt-get install -y git postgresql libpq-dev nodejs yarn
    curl -sSL | bash
    sudo usermod -a -G rvm vagrant
    source /etc/profile.d/
    rvm requirements
    rvm install 2.7.1
    rvm use 2.7.1 --default
    gem install rails bundler --no-doc

I add this Vagrantifle in my app's folder and run vagrant up. After vagrant downloads the image, it boots it up and runs provisioning part. Few minutes later, I can log into the vagrant machine using vagrant ssh, get to the app folder and run bundler and rails app.

Then I run rails s -b in the vagrant box to run rails and make the app accessible on the host machine,