Last Updated: December 31, 2020
· joshteng

Setting Up Vagrant

This was written with Github flavored markdown.. It messes up here a little. See a better version:

Download and Install Vagrant and VirtualBox or VMware before beginning

Set up Vagrant

*this guide assumes the use of Mac OS X Mountain Lion on local machine and Ubuntu 12.04 LTS x64 on Vagrant box. It is compatible with many other distros but hasn't been tested.

Step 1: Make and run box

vagrant init
vagrant box add <path to box directory or name according to>
vagrant up
vagrant ssh

Step 2: Forward ports

Vagrantfile "forwarded_port", guest: 80, host: 8080 "forwarded_port", guest: 3000, host: 3000

Set up environment

You can choose to use configuration management/provisioning tools like Chef/Puppet for the following steps to set up your environment. But if you want to understand how it's done, I encourage you to do it manually.

Step 3: Install needed system libraries and Postgresql

sudo apt-get update
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev libcurl4-openssl-dev python-software-properties libpq-dev postgresql postgresql-contrib

Step 4: Install ruby

git clone git:// .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

git clone git:// ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

rbenv install 2.1.0
rbenv global 2.1.0
rbenv rehash

Step 5: Install Bundler

echo "gem: --no-ri --no-rdoc" > ~/.gemrc
gem install bundler
rbenv rehash

Step 6: Install Node.js for Rails asset pipeline

sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

Step 7: Install Nginx on server

sudo apt-get install nginx

Step 8: Set up postgres 'vagrant' role

sudo su - postgres
createuser --pwprompt #create a role called vagrant.. just leave a blank password if you're using Vagrant locally for development and make it a superuser when prompted!
ALTER USER Postgres WITH PASSWORD 'vagrant'; --run this in psql..I actually don't know why I had to do this.
\q --exit psql

Set up app

Step 9: Bundle install

cd /vagrant #this is where your rails app should be by default if you changed it, just go to the right path on your VM
bundle install
rbenv rehash

Step 10: Create and Migrate DB

bundle exec rake db:create
bundle exec rake db:migrate
  • if there's encoding issues:
  • if you are facing cryptic errors (eg. wrong ELF class: ELFCLASS32) with bcrypt, pg etc+ gems just remove it from /vendor/bundle and re-run bundle install
  • if the above still doesn't solve your problem, remove all the gems from your rails app by doing rm -rf /vagrant/vendor/bundle and then run bundle install again. (unfortunately this takes ages depending on your internet connection and
  • if you're getting error FATAL: role "vagrant" does not exist, you didn't do step 8 right

Step 11: Testing

bundle exec rails s
sudo service nginx start

http://localhost:3000 should work now

http://localhost:8080 should show nginx welcome page

Step 12: Set up unicorn


gem 'unicorn'


bundle install
touch <app_root>/config/unicorn.rb


root = "/vagrant"
app_name = "YOUR_APP_NAME"
working_directory root
pid "#{root}/tmp/pids/"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"

listen "/tmp/unicorn.#{app_name}.sock"
worker_processes 4
timeout 40
preload_app true

# Force unicorn to look at the Gemfile in the current_path
# otherwise once we've first started a master process, it
# will always point to the first one it started.
before_exec do |server|
  ENV['BUNDLE_GEMFILE'] = "#{root}/Gemfile"

before_fork do |server, worker|
   defined?(ActiveRecord::Base) and
  # Quit the old unicorn process
  old_pid = "#{server.config[:pid]}.oldbin"
  if File.exists?(old_pid) && != old_pid
    puts "We've got an old pid and server pid is not the old pid"
      puts "killing master process (good thing tm)"
    rescue Errno::ENOENT, Errno::ESRCH
      puts "unicorn master already killed"

after_fork do |server, worker|
  port = 5000 +
  child_pid = server.config[:pid].sub('.pid', ".#{port}.pid")
  system("echo #{} > #{child_pid}")
   defined?(ActiveRecord::Base) and
  1. Starting unicorn: bundle exec unicorn -c /vagrant/config/unicorn.rb -p 3000

Set up Nginx

Step 13: Set up nginx.conf in rails config directory

touch <app_root>/config/nginx.conf


upstream unicorn {
  server unix:/tmp/unicorn.YOUR_APP_NAME.sock fail_timeout=0;

server {
  listen 80 default deferred;
  # server_name;
  root /vagrant/public;
  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;

Step 14: Symlink nginx.conf

sudo ln -s /vagrant/config/nginx.conf /etc/nginx/sites-enabled/<your_app_name>

Step 15: Delete Nginx default site

sudo rm /etc/nginx/sites-enabled/default

Step 16: Restart Nginx

sudo service nginx restart
  1. Start unicorn: bundle exec unicorn -c /vagrant/config/unicorn.rb
  2. Site should work at http://localhost:8080 now (make sure unicorn is still running)

Make Unicorn a service for convenience

Step 17: Set up

touch <app_root>/config/
bundle install --binstubs #creates an executable in the bin directory for our unicorn_init shell script

set -e
# Example init script, this can be used with nginx, too,
# since nginx and unicorn accept the same signals

# Feel free to change any of the following variables for your app:
CMD="$APP_ROOT/bin/unicorn -D -c $APP_ROOT/config/unicorn.rb"
set -u


cd $APP_ROOT || exit 1

sig () {
  test -s "$PID" && kill -$1 `cat $PID`

oldsig () {
  test -s $old_pid && kill -$1 `cat $old_pid`

case $action in
  sig 0 && echo >&2 "Already running" && exit 0
  ruby -v
  # sudo su -c "ruby -v" - postgres
  # sudo su -c "$CMD" - vagrant
  sig QUIT && exit 0
  echo >&2 "Not running"
  sig TERM && exit 0
  echo >&2 "Not running"
  sig HUP && echo reloaded OK && exit 0
  echo >&2 "Couldn't reload, starting '$CMD' instead"
  if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
    while test -s $old_pid && test $n -ge 0
      printf '.' && sleep 1 && n=$(( $n - 1 ))

    if test $n -lt 0 && test -s $old_pid
      echo >&2 "$old_pid still exists after $TIMEOUT seconds"
      exit 1
    exit 0
  echo >&2 "Couldn't upgrade, starting '$CMD' instead"
  sig USR1
  echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
  exit 1

Make executable

chmod +x config/

Step 18: Symlink

sudo ln -s <app_root>/config/ /etc/init.d/unicorn_your_app_name

Step 19: Start up Unicorn

#shut down all instances of Unicorn (optional)
ps aux | grep unicorn #find unicorn processes
kill -9 <pid> #shut it down

##start up unicorn as a linux process
sudo service unicorn_your_app_name start #it's important to know which version of ruby will be used when running as different users. if you're facing errors, see below.
If you're getting this error:

/opt/vagrant_ruby/lib/ruby/site_ruby/1.8/rubygems.rb:900:in "report_activate_error": Could not find RubyGem unicorn (>= 0) (Gem::LoadError)
Your init script isn't using the right version of Ruby. Here are few things you can try:

  1. Modify shell script to run your command as vagrant su -c "$CMD" - vagrant instead of just $CMD
  2. Try sudo service unicorn_app_name start and service unicorn_app_name start
To debug effectively:

ONE: Check the version of Ruby that is being used when attempting to boot up Unicorn through your init shell script

this is just a snippet of code in unicorn_init file

case $action in
sig 0 && echo >&2 "Already running" && exit 0
ruby -v #check the version of ruby you're using
# sudo su -c "ruby -v" - vagrant #check the version of ruby you're using
# sudo su -c "$CMD" - vagrant

TWO: Tail Unicorn log output to figure out what other erros you might have
sh tail -f /vagrant/log/unicorn.log


if you're running unicorn as root, you will need to add root as a role for postgres (go through step 8 but add root instead of vagrant)

Voila! It should work now!


VirtualBox slowness issues on mac

  1. Speeding up Vagrant:
  2. Add following in Vagrantfile ruby config.vm.provider :virtualbox do |vb| vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"] vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"] end disclaimer: I can't seem to get any of these solutions to speed up my VM. If you managed to speed up your VM on a Mac with VirtualBox, please leave some comment and help us out! Thanks!
    In the meantime, you might want to look into VMware instead of VirtualBox

2 Responses
Add your response

nice post, thanks

over 1 year ago ·

You're welcome!

over 1 year ago ·