Skip to content

Instantly share code, notes, and snippets.

@johnnymo87
Created August 13, 2024 19:43
Show Gist options
  • Save johnnymo87/af1d7b0a578e65348e03db848752d030 to your computer and use it in GitHub Desktop.
Save johnnymo87/af1d7b0a578e65348e03db848752d030 to your computer and use it in GitHub Desktop.
Setting up a Ruby on Rails app on Mac OS X

Development Environment Setup

NB: Assumes OS X

Table of Contents

General Requirements

The following commands are not tied to a particular repository.

  • Homebrew for managing software packages on OS X

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    

    Note: if you are on a newer Mac you may be instructed to put this in your bash_profile:

    eval "$(/opt/homebrew/bin/brew shellenv)"
    

    If you have this line, make sure it is near the TOP of the file before any other if clauses from below

  • Docker for Mac.

  • BA CLI for commands like ba login and ba exec.

  • Install direnv and configure your shell to enable automatic environment variable loading

    brew install direnv
    
    # in e.g. ~/.bash_profile
    
    if which direnv > /dev/null; then
      eval "$(direnv hook bash)"
    fi
    
  • Install Postgres (we currently use v12)

    • Note that you brew link --force postgresql@12 because our version of postgresql is old and no longer the default in brew, and you want to set its respective old postgresql client libraries (e.g. psql, pg_restore) to be your default so that actions like bundle exec rake db:migrate uses them. If you have other versions of these libraries installed via brew, and you later want them to go back to having them be your default, you will need to brew link --force them back into place.
      brew install postgresql@12
      brew link --force postgresql@12
      brew services start postgresql@12
      
    • Create the blueapron postgres user used by all our services
      createuser -s blueapron
      
  • Install Redis

    brew install redis
    brew services start redis
    
  • Install a library for xml2

    brew install libxml2
    
  • Install stuff needed for mimemagic

    brew install shared-mime-info
    

Per Language

Javascript

  • Install or update nodenv (and node-build to get access to recent releases of node).

    brew update && brew install node-build nodenv
    brew update && brew upgrade node-build nodenv
    
  • Configure your shell to enable shims

    # in e.g. ~/.bash_profile
    
    if which nodenv > /dev/null; then
      eval "$(nodenv init -)"
    fi
    
  • Install Node

    nodenv install -s $(cat ./.node-version)
    
  • Install packages

    npm install -g yarn
    nodenv rehash
    yarn
    
  • For ember projects You may need to do this per-repo that will be interacting with ember:

    npm install -g ember-cli
    

Ruby

  • Install or update rbenv (and ruby-build to get access to recent releases of ruby).

    brew update && brew install ruby-build rbenv
    brew update && brew upgrade ruby-build rbenv
    
  • Configure your shell to enable shims

    # in e.g. ~/.bash_profile
    
    if which rbenv > /dev/null; then
      eval "$(rbenv init -)"
    fi
    
  • Install Ruby

    rbenv install -s $(cat .ruby-version)
    
  • Install bundler

    gem update --system
    gem install bundler
    
  • If on apple silicon, let bundler know, so you don't have problems like this with your google-protobuf and grpc gems

    bundle config set force_ruby_platform true
    
  • Get the environment variables that bundler will need to install private gems

    ba config get -r wms-server -b wms -e staging | grep BUNDLE
    
    goba v8.0.6 ==> ba config get -r wms-server -b wms -e staging
    
    Using staging(blue)
    BUNDLE_ENTERPRISE__CONTRIBSYS__COM:   SOME_CONTRIBSYS_TOKEN
    BUNDLE_GEM__FURY__IO:                 SOME_GEMFURY_TOKEN
    
  • Give bundler this information

    bundle config --global gem.fury.io SOME_GEMFURY_TOKEN
    bundle config --global enterprise.contribsys.com SOME_CONTRIBSYS_TOKEN
    
  • Tell bundler where to find the library for xml2

    bundle config --global build.libxml-ruby --with-xml2-config="$(brew --prefix libxml2)/bin/xml2-config"
    
  • Install gems

    rbenv rehash
    bundle install
    

Setup the Database

NB: Assumes a rails application.

To start with a fresh slate, run the following:

bundle exec rake db:drop db:setup

This will ...

  1. Drop your test and development databases.
  2. Reset them both according to the app's schema file (either db/schema.rb or db/structure.sql).
  3. Seed your development database by running the db/seeds.rb script.

If the db/seeds.rb file looks like an empty auto-generated file, then running it of course does nothing. If you think it would be helpful to have some data seeded in your development database, reach out to your teammates to learn how they do it, as it varies from app to app. But in short, you could either add some logic to this db/seeds.rb file, or you could flash your database with a database dump file copied from staging. To do this, you will need to get a copy of a database dump. Go to jenkins, and look for the build pipeline for your application. Click it, and then look for a job named your-application-name-maintenance-job-db_dump_stage (or *_production). Click it, rerun it, and wait for it to finish. When it finishes, look at its console output. At the bottom will be a URL to an S3 bucket. Click it to download the dump. Once you have it, apply it to your database like so:

pg_restore --verbose --clean -j 4 --no-acl -O -U blueapron -h localhost -d YOUR_DATABASE_NAME YOUR_DUMPFILE_NAME

To know what your database name is, see the config/database.yml file. It is probably your service name with a *_development suffix.

Note that if you only intend to run the test suite, you don't need to bother with seeding data.

M1 troubleshooting

See this other guide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment