Moving in a new direction, I’ve taken up a position as a Rails developer and am documenting the setup process on OS X as the tutorials out there once again felt a bit lacking – leaving out some crucial bits.

I’ll be developing on Ubuntu at work (which had a much easier setup), but I like to play around on my iMac at home and thus need to have Rails running on there as well.

Don’t even bother with RVM on OS X – it did just enough damage to have me almost having to reinstall Homebrew.

OK, let’s get started.

For the most part you can follow the instructions at Go Rails, but install Ruby 2.2.4 instead of version 2.2.3. Similarly with Rails, if you’re just starting out, you might as well start with version 5.0.0 instead of version 4.2.4.

PostgreSQL

I’ve decided to go with PostgreSQL instead of MySQL for Rails as that’s what the majority of the tutorials I found seem to recommend. The install instructions from Go Rails are good, but to actually create a user, database, etc. on OS X use the instructions below.

Replace myapp with whatever your Rails app is actually named.

Add a user for your Rails app in the terminal with $ createuser myapp -s.

Connect to PostgreSQL with $ sudo psql -U $(whoami) postgres. $(whoami) can be replaced by your actual username or simply used as-is as it will insert your username where it’s specified.

Once the user has been created, change its password with postgres=# \password myapp.

In conclusion…

After first struggling for years with setting up my Mac for local development by trying to update the pre-installed version of PHP and then adding mcrypt, MySQL and whichever other components, I finally started using Homebrew while simultaneously switching from Apache to nginx. This was a huge improvement from what I was used to, but Taylor Otwell gifted us with an even better solution with Laravel Homestead.

Even though it does come with setup instructions, there are many steps involved when starting from scratch. It was also frustrating when things broke during setup, since certain things are assumed from people installing the software. Never assume.

So, I worked through a few separate tutorials and compiled this all-in-one list of what to do. Here we go!

Homebrew

You don’t need to install Homebrew, but it will make installing things like PHP much easier. You can install Homebrew by running the following command in Terminal, following the on-screen instructions to completion:

PHP

As with Homebrew, you don’t strictly need PHP installed locally, but it does come in handy if you want to use a linter or Composer. Run all of the following commands to install PHP, following any on-screen instructions.

Composer

Composer is to PHP, as Homebrew is to OS X – the missing package manager. Most frameworks and packages use it for dependency handling, so it makes sense to install it locally if you want to do anything outside the Homestead environment.

Install it by running the following command in Terminal:

curl -sS https://getcomposer.org/installer | php
When it completes, move the composer.phar executable for global access in Terminals:
sudo mv composer.phar /usr/local/bin/composer
You may need to restart Terminal, but Composer should now be usable.

Virtual Machine

This step caught me by surprise (although the newer Homestead instructions now include this) and probably proves that I’m blonder than I appear… The Vagrant installation instructions mention needing a virtual machine host, but they never explicitly instruct you to install it prior to installing itself.

So, at this stage go ahead and install VirtualBox, or VMware Fusion if you’re able to pay $79 plus another $79 for VMware integration with additional benefits such as increased speed.

Vagrant

Vagrant is used to build local virtual machines through downloadable “boxes”. The main advantage is that your development environment will be completely self-contained and can be recreated on any other computer.

Now simply download the Vagrant binary from their website and double-click the installer.

SSH Key

As you will be using SSH to access your Homestead box, set up an SSH key. In Terminal, run:

ssh-keygen -t rsa

Homestead

Although Homestead is a Laravel product, you should be able to use it for non-Laravel development without any issues.

You’re free to set up a virtual machine for each site you’re working on or one machine for all of them. The choice really comes down to what’s more comfortable for you. I prefer a single machine as I tend to be working on multiple things concurrently.

To install the Homestead Vagrant box run the following command in Terminal:

vagrant box add laravel/homestead
This downloads an image file clocking in at almost 1 GB, so give it a few minutes. Once it's done you'll need to install Homestead. You can use any location, but I prefer sticking to the `~/Sites` folder. In Terminal:
cd ~/Sites
Now clone the Homestead repository:
git clone https://github.com/laravel/homestead.git Homestead
If you need to use PHP 7.0 specific features, try the new version:
git clone -b php-7 https://github.com/laravel/homestead.git Homestead
Once the clone operation is complete, run:
cd ~/Sites/Homestead
bash init.sh
This script creates the `~/.homestead/Homestead.yaml` configuration file. Open this file in your editor of choice to configure the folders, sites and databases.

The folders section lists all the local folders you wish to share with the virtual machine. If you choose to use the ~/Sites folder, simply change ~/Code to ~/Sites – or whatever folder you’ll be using.

The sites section is where you configure all the local sites you’ll be working on. For example:

sites:
- map: local.myapp.com
to: /home/vagrant/Sites/myapp/public
Homestead uses this information to set up the site on nginx on the virtual machine for you.

Lastly, you’ll need to declare any databases you require in the databases section.

databases:
- myapp
Save the file and run `vagrant up` from the `~/Sites/Homestead` folder in the Terminal. You should receive a message once it's up and running.

Alternatively, you can use Vagrant Manager if you’d prefer to have a handy menubar app.

In order to access our Homestead sites from a browser, we will need to add the domains from the Homestead.yaml file into the local hosts file. Open /etc/hosts in your editor, adding a line for each site to the end of the file and saving it.

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 local.myapp.com
Alternatively, you can use [Gas Mask](http://clockwise.ee) which allows you to more effectively manage your `hosts` file.

By default Homestead serves sites on port 8000, but you can make it use port 80 by using the IP address from the Homestead.yaml file, for example:

192.168.10.10 local.myapp.com
To test your new site, create a file at `~/Sites/myapp/public/index.php` with the following content:
<?php
echo 'Hello World!'{semicolon}
Now visit the address in your browser.
http://local.myapp.com
Remember to add port 8000 if you used `127.0.0.1` in your `hosts` file.
http://local.myapp.com:8000
You should see "Hello World!", if everything went according to plan.

To set Laravel up to function in a shared hosting environment, you’ll need to split the installation. Instead of doing a normal installation into just your project folder, you’ll instead install Laravel into the project folder at the same level as the public_html folder.

I occasionally receive financial statements as password-protected PDF files. I appreciate the security, but would prefer to archive the files without the password requirement as I’m never going to remember the password when I do need to open them again.

There’s lots of PDF password removal applications out there, but I’ve found QPDF to do the job quite well. You can either install it from their site or simply install it with Homebrew:

Want to SSH into your server without having to enter the password each time? Read on.

On your local machine in Terminal:

ssh-keygen -t rsa
scp ~/.ssh/id_rsa.pub remote:~/
ssh username@remote
Enter your password and then on the remote server:
cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 644 ~/.ssh/authorized_keys
exit
And finally on your local machine again:
ssh username@remote
You should now have been able to SSH in without having to enter your password.