http://bit.ly/dbc-installfest # DevBootcamp Installfest ## What we'll cover 1. `subl` 1. Understanding the Unix Environment 1. Homebrew 1. sqlite3 1. postgres 1. RVM/rbenv 1. Ruby 1. gems ## Want to skip all the hard work? Check out my [automated environment setup doctor](https://github.com/mikelikesbikes/environment_linter). ## `subl` We'll start by testing that `subl` is somewhere in our `$PATH` (we'll cover `$PATH` later): ````sh $ which subl /usr/local/bin/subl ```` If this comes back blank, then we need to set up the `subl` command. A quick Google search for "Sublime command line" gives me the [directions](https://www.sublimetext.com/docs/2/osx_command_line.html). These directions will link the `subl` executable packaged with the Sublime Application to your `$HOME/bin` directory. Before you can link the command, you'll want to ensure you have a `$HOME/bin` directory (`mkdir -p $HOME/bin`) and that `$HOME/bin` is in your `$PATH`. I recommend putting the symlink in `/usr/local/bin` instead of `$HOME/bin`. Here's how I do it: ````sh $ find /Applications/Sublime\ Text\ 2.app -name subl /Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl $ ln -fs "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl $ which subl /usr/local/bin/subl ```` ## The Unix Environment The Unix environment is a huge topic. For today's purposes, we'll be interacting with a few pieces: 1. `printenv` 1. `$PATH` * `$ echo $PATH` or a bit more useful `$ echo $PATH | tr : '\n'` * `$ which ` and `$ which -a ` * `/etc/paths` (and why you shouldn't modify this) 1. `$PS1` * Your prompt. 1. `$HOME/.bash_profile`, `$HOME/.bashrc`, `$HOME/.profile` * Your login environment's customizations For more information on the Unix directory structure, check out [this](http://askubuntu.com/questions/308045/differences-between-bin-sbin-usr-bin-usr-sbin-usr-local-bin-usr-local) and [this](http://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard). ## Homebrew First let's ensure we've installed homebrew correctly. ````sh $ which brew /usr/local/bin/brew ```` If you don't see this, run: ````sh ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" ```` Homebrew is going to do a lot of work for us, so always ensure that it's up-to-date and that it's healthy. ````sh brew update ```` and ````sh $ brew doctor # … LOTS OF STUFF THAT NEEDS TO BE FIXED … ```` `brew doctor` may tell you a lot of stuff… you'll want to read through each item, and attempt to resolve the issue for Homebrew. We're ideally shooting for a message like this: ````sh $ brew doctor Your system is ready to brew. ```` ## Adjusting $PATH for Homebrew Homebrew is going to be installing packages and tools for us, so we need to make sure that when possible our system is using homebrew's installed packages. We'll start by inspecting our path ````sh $ echo $PATH | tr : '\n' /Users/mike/bin /usr/local/bin /usr/bin /bin /usr/sbin /sbin /usr/local/bin ```` Ensure that `/usr/local/bin` comes before `/usr/bin` and `/bin`. If it doesn't, open `$HOME/.bash_profile` using Sublime Text and add the following: ```` export PATH="/usr/local/bin:$PATH" ```` Also check out the [rbenv documentation](https://github.com/sstephenson/rbenv). ## Installing `sqlite3` using Homebrew ````sh $ brew install sqlite3 ```` OS X provides `sqlite3` for you, so we'll need to force homebrew to link it so that we can use the homebrew version by default: ````sh $ brew link sqlite3 --force ```` ## Installing `postgres` using Homebrew ````sh $ brew install postgres ```` `postgres` is a piece of software that requires a server to interface with. It's easiest to let OS X's `launchd` utility keep this server running, otherwise we'd have to restart it every time it died or our machine rebooted. ````sh $ ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents $ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist ```` ````sh $ which postgres /usr/local/bin/postgres ```` ````sh $ which psql /usr/local/bin/psql ```` Now Homebrew will keep our postgres server alive so that we can just develop awesome apps and not worry about the database. Let's ensure that we have a server running. To do that we'll use `ps`: ````sh ps aux | grep postgres mike 444 0.0 0.0 2439324 148 ?? Ss 13Feb14 0:02.33 postgres: stats collector process mike 443 0.0 0.0 2443176 624 ?? Ss 13Feb14 0:02.44 postgres: autovacuum launcher process mike 442 0.0 0.0 2443044 80 ?? Ss 13Feb14 0:03.72 postgres: wal writer process mike 441 0.0 0.0 2443044 132 ?? Ss 13Feb14 1:29.90 postgres: writer process mike 440 0.0 0.0 2443044 136 ?? Ss 13Feb14 0:00.14 postgres: checkpointer process mike 403 0.0 0.0 2443044 68 ?? S 13Feb14 0:00.40 /usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres -r /usr/local/var/postgres/server.log mike 6469 0.0 0.0 2432768 600 s000 R+ 5:42PM 0:00.00 grep postgres ```` The process with `pid` (process id) #403 is our server process. You can see here what executable (the full path) is running, where the data file is (ie. `/usr/local/var/postgres`), and where we can find the logs (ie. `/usr/local/var/postgres/server.log`). Finally, create a default database and test our postgres client, `psql`. ````sh createdb $USER psql $USER # this is a sql console for interacting with a postgres database ```` ## Managing Ruby Installations `rvm`, `rbenv`, or `chruby`? There are a number of different popular tools for installing/managing/using different versions of ruby on your machine. We're going to use `rbenv` (that's what's on the DBc workstations). If you already have `rvm` installed, you are fine, and can skip this portion. ## Verifying your Ruby Manager ````sh which rbenv $HOME/.rbenv/bin/rbenv OR /usr/local/bin/rbenv ```` ````sh which rvm $HOME/.rvm/bin/rvm ```` ## Install `rbenv` using Homebrew ````sh $ brew install ruby-build rbenv ```` `rbenv` will store rubies and gems to `$HOME/.rbenv`. There's no need to change this. `ruby-build` is the tool that will do the installing. Now that `rbenv` is installed, we need to configure our environment to use it. The instructions say that we need to add the following to our `$HOME/.bash_profile`: ````sh if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi ```` Usage: ````sh $ rbenv install --list # will list all of the Rubies ruby-build can install $ rbenv install 2.0.0-p353 # will install ruby 2.0.0 patch 353 $ rbenv versions # will list the Rubies you already have installed $ rbenv global 2.0.0-p353 # will set the global/shell/local ruby ```` ## Ruby Let's make sure you're NOT using the system ruby. ````sh $ which ruby /Users/mike/.rbenv/shims/ruby ```` If this says `/usr/bin/ruby`, then we need to get your ruby installed via a ruby version manager. I suggest rbenv (see above). If you're using rbenv, run this: ````sh $ rbenv which ruby /Users/mike/.rbenv/versions/2.0.0-p481/bin/ruby ```` Same as above, it should not say `/usr/bin/ruby`. If it does, then install a ruby (via rbenv), then set it as your default ruby (`rbenv global `). ## Gems Most gems are fairly easy to install, but there are a few that we use at DBC that can be problematic (usually the ones with native dependencies). Let's start by verifying that the gem executable is installed via a ruby version manager. ````sh $ which gem /Users/mike/.rbenv/shims/gem ```` You'll notice that `gem` is in the same directory as `ruby` (from above). This is good. If they aren't in the same place that's usually a problem. If it's `/usr/bin/gem` that's an issue. If you're using rbenv, run: ````sh $ rbenv which gem /Users/mike/.rbenv/versions/2.0.0-p481/bin/gem ```` Again, just like ruby, this shouldn't say `/usr/bin/gem`. It should also be in the same location as when you ran `rbenv which ruby`. Let's also update rubygems. ````sh $ gem update --system ```` Alright, ON TO THE HARD GEMS! First, bundler. ````sh gem install bundler rbenv rehash // if you're using rbenv ```` And the rest: ````sh gem install sqlite3 pg nokogiri ```` # Extras ## Install `git` using Homebrew ````sh $ brew install git ```` Ensure we're using `git` from homebrew: ````sh $ which git /usr/local/bin/git ```` Question: OS X ships with `git` isn't that good enough? Answer: It might be, but it's likely out of date, and you won't benefit from new tools and developments. `git` is a tool we'll use frequently, so installing it and keeping it up to date is akin to a chef keeping his knives sharp. Up-to-date tools are one of the marks of a craftsman, strive to be a craftsman. ## Install `bash` using Homebrew ````sh $ brew install bash ```` To use this new version of bash, we'll need to add it to `/etc/shells`. `/etc/shells` is owned by the `root` user (verify using `ls -la /etc/shells`), so we'll need to use `sudo` to write to it. ````sh $ sudo -s "echo /usr/local/bin/bash >> /etc/shells" ```` Now we need to change our shell command: ````sh $ chsh -s /usr/local/bin/bash ```` All set. We can launch a new teminal window and verify our setup. ````sh $ bash --version ````