MacOS, Homebrew, PHP, Valet, MySQL

As well as a couple of private projects and SLA work, I work under contract for a handful of clients, most of my working days are pre-planned spent developing, testing and deploying code. This means I need a development stack that is reliable, quick to get up and running and is not going to burden me with issues I need to fix! Which means I can do what I enjoy doing best, and that’s coding!

However, there comes a time when we all need a reset, computers are the same and in my case, my iMac is in much need of an overhaul. Over time it’s just become a complete snail to work with, I think it’s still running from its original out of the box state from about 4 years ago, all sorts of apps would crash and memory fluctuations that would randomly restart the machine, not ideal when you need to a reliable working environment! So, this weekend I decided to use my MacBook Pro to dummy run how long it would take to give a machine the full overhaul, this would give me an idea of how long it would take to get up and running with a reliable working environment on my iMac.

I’ve used a few local development environments over the years, MAMP, MAMPPro, Vagrant, Local by Flywheel to name a few, while they have all served me well they do have their own little quirky faults that I’ve personally found problematic. For instance, MAMPPro used to crash on me sometimes, it meant I had to recreate my httpdocs file every now and then, Vagrant used to suck all the memory out of my machine and slow it right down to a snails pace and Local by Flywheel was excellent until you spun a certain number of sites up before things would start going pear-shaped – granted most of these issues were probably down to local memory or storage but I need to manage things quickly, especially if I’m working to a deadline.

So in the last year or so I’ve settled with Homebrew to use services such as Valet, PHP & MySQL, its very lightweight, quick to run and a powerful way to develop your code locally without the headache of all the clunk that can come with other local development environments.

Going back to the overhaul I mentioned earlier, I wanted to use my MacBook Pro as a test case to see how quickly I could reformat my drive, install a new OS and set up a local development environment.

Now, although it did have a few stumbling blocks I actually learnt quite a bit along the way, so as a point of reference and to help others I wanted to document it here.

First things first, BACKUP EVERYTHING! This probably took the longest but it depends on how much data you have that will define how long it takes for you, I use Backblaze so technically I have a backup of most things apart from some local files and databases etc – others will be different, the importing thing to remember here is that I’m going to reformat my drive so I wanted to ensure I had everything backed up. I can recover it later. Oh, and I have quite a few apps installed as well, here’s a little tip on how to create a text file of those apps as a reference so you don’t have to try and remember what you had installed before reformatting, in your terminal you can do the following to create a text file of all the files in a folder.

just cd in to the folder you want to create a text file for and run the following, naming the .txt file whatever you want:

ls > listoffiles.txt

After the backup was complete, and that included the text file I just created, it was time to reformat and reinstall a new OS, there are lots of resources on the web for that so I won’t go it to detail about that, just Google it for your particular machine.

Now I have a reformatted MacBook Pro with a clean up to date OS installed, it was time to start setting up my development environment. From the Laravel development community comes a development environment for macOS called Valet, you’ll see how Laravel/Valet is set up here, my installation process follows the same step although it’s slightly different when it comes to MySQL so let’s begin. Fire up your preferred command line app or the native Terminal App on Mac

Install Homebrew from this link – they provide a command you can copy and paste into your terminal to install everything you need, it will look something like this

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

copy it from the Homebrew link provided though as it might change.

Once complete you can now use the following brew commands.

To install PHP:

brew install php@7.3

To install Composer

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'c5b9b6d368201a9db6f74e2611495f369991b72d9c8cbd3ffbc63edff210eb73d46ffbfce88669ad33695ef77dc76976') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Note: the above commands may change so it’s worth using the official composer guide to install it properly.

Move Composer, so you can access it globally

mv composer.phar /usr/local/bin/composer

This will install an invisible folder called composer in your current users folder, you can run the following to check its there, change directory into your users folder:

cd ~

then list all the files including the invisible ones that will be prefixed with a full stop eg .composer

ls -a

next, you’ll need to copy the .composer reference into your systems $PATH. This might be slightly different for each machine but for a fresh install of macOS it’s in the .zshrc file in the same folder as your user directory.

Note: depending on your setup this might be slightly different eg. you might be overriding with a .bash_profile if you have that setup.

For my example I’m using the global .zshrc

I used nano to edit the file like so:

nano .zshrc

This will open the file, scroll to the bottom of that file and add the following:

export PATH="~/.composer/vendor/bin:$PATH"

To check it’s all done correctly you can echo out the $PATH, if it is correct you should see this line in the output somewhere ~/.composer/vendor/bin. Go ahead and echo it out;

echo $PATH

Now to install Valet

composer global require laravel/valet
valet install

Valet by default server local sites on .test TLD. You can now ping a domain in your terminal to check its working, you should see a list of transmitted and received packages which indicates it’s working.

ping randomdomain.test

You can use valet link in any folder to serve a local site but I prefer to serve all my sites from one folder, so I did the following to create a folder in my user’s directory:

cd ~
mkdir valet-sites
cd valet-sites
valet park

Whatever folder you create in the valet-sites folder will use the .test TDL in my browser preceded by the folder name eg.

~/valet-sites/ginnersite

will be served as

http://ginnersite.test

Whatever application you now install in that folder will get served on that domain locally. If you want to secure that site simply run

valet secure

Your domain will now be served as

https://ginnersite.test

Of course, if you’re now ready to develop applications with WordPress, Laravel or whatever requires a PHP/MySQL stack you going to need MySQL, so let’s do that:

Install MySQL

brew install mysql

Now we need to start MySQL, at the moment it is only installed, it is not running, do the following

brew services start mysql

Once MySQL is running do the following command to run through securing your MySQL instance

mysql_secure_installation

Run through the steps choosing your options and setting up a password for the root user etc.

Once complete, log in to your MySQL instance using the root user by doing the following, you’ll be prompted for the password you set in the previous step:

mysql -u root -p

If everything was correct you now are working in MySQL from the command line. While you are in MySQL you should see a prompt that looks like this mysql>

Set the root user to use the password you just set up changing <your_password> to be the password.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '<your_password>'

And that’s it, you can now use PHPMyAdmin or SequelPro or any other GUI to login to MySQL

For more on running Valet commands just run the following in your terminal:

valet

Or for serving sites locally see Laravel/Valet Serving Site

Leave a comment

Your email address will not be published. Required fields are marked *