Setting up local rails development

published 16.12.2020

backendweb appdevopsquick tip
rubyrailsvirtualization

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 https://vagrantcloud.com/search.
  config.vm.box = "ubuntu/bionic64"

  # Accessing "localhost:8100" will access port 3000 on the guest machine.
  config.vm.network "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 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"
  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.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
  end

  config.vm.provision "shell", inline: <<-SHELL
    apt-get update
    apt-get install -y curl
    curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
    curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
    echo "deb https://dl.yarnpkg.com/debian/ 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 https://get.rvm.io | bash
    sudo usermod -a -G rvm vagrant
    source /etc/profile.d/rvm.sh
    rvm requirements
    rvm install 2.7.1
    rvm use 2.7.1 --default
    gem install rails bundler --no-doc
  SHELL
end

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 0.0.0.0 in the vagrant box to run rails and make the app accessible on the host machine,