Setting Up an M1 Mac for Laravel Development with Homebrew, PHP, MySQL, Valet and Redis

Published November 22nd, 2020
6 minute read
Warning!
This was written over two years ago, so some information might be outdated. Frameworks and best practices change. The web moves fast! You may need to adjust a few things if you follow this article word for word.

Friday, I received a snazzy new M1 Macbook Pro in the mail. This article outlines how I was able to set it up for doing web development. We'll set up Homebrew, PHP, MySQL, Composer, and Laravel Valet. Let's jump in!

Heads up! I wrote this when M1 Macs were new. Homebrew now has an M1-compatible version, so you may be able to use that instead, but I haven't tested it yet! If you want to try it, just ignore the Rosetta stuff and take out the arm from the rest of the commands.

The previous article outlines first impressions from the perspective of someone upgrading from a 2013 mac.

Setting up an M1 mac for PHP development is not much different than other macs. Unless you're using Docker, which doesn't work on the new ARM processor (yet — they're working on it). I expected to have way more problems being an early adopter, but Apple has done a wonderful job with their Rosetta 2 translation layer. It mostly feels invisible, so (except for a few terminal commands like homebrew) you hardly even notice it is there.

Homebrew

First, we'll need to install Homebrew. They don't have an ARM-compatible build ready yet, so this is where we'll need to use some Rosetta flags on the command line.

Install Rosetta on the command line with the following:

/usr/sbin/softwareupdate --install-rosetta --agree-to-license

Next, add this function to your .zshrc file. It makes a nice arm alias for running commands with x86_64 architecture flags. Perhaps calling it x86 would be better? Shoutout to Matt Stauffer for posting this.

1arm() {
2 arch -x86_64 $@
3}
1arm() {
2 arch -x86_64 $@
3}

You'll need to run the homebrew commands with this prefix for now. We can copy the script from their site, add our arm prefix, and homebrew should install!

1arm /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
1arm /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

PHP, MySQL, and Composer

Now that homebrew is installed, the rest of the Valet install is pretty much stock (except for Redis, which we'll get to in the next section).

PHP

Just run arm brew install phpit's that easy! You may want to restart your terminal after this.

MySQL

Normal besides the arm prefix again.

1arm brew install mysql
2arm brew services start mysql
1arm brew install mysql
2arm brew services start mysql

Composer

Run the download script from Composer's website, then move the PHAR file to the bin folder. Also we'll add the global composer vendor folder to our system path.

1php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
2php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
3php composer-setup.php
4php -r "unlink('composer-setup.php');"
5mv composer.phar /usr/local/bin/composer
1php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
2php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
3php composer-setup.php
4php -r "unlink('composer-setup.php');"
5mv composer.phar /usr/local/bin/composer

Now add the following line to your .zshrc file

1export PATH=${PATH}:~/.composer/vendor/bin
1export PATH=${PATH}:~/.composer/vendor/bin

Valet and Redis

Installing Valet should work as normal now. Run the following commands:

1composer global require laravel/valet
2valet install
1composer global require laravel/valet
2valet install

After that, I also ran cd ~/Code && valet park .

Installing Redis

Redis presented the only real speed bump I've encountered thus far. It installs via brew, but starting the Redis server doesn't work correctly (even though brew says it does). Until then, we can start the server manually.

First, run arm brew install redis to install it.

Next, install the Redis PHP extension with PECL — pecl install redis.

Starting the Server

Normally you'd use arm brew services start to start Redis (and at login), but it's not working yet. That command just runs redis-server under the hood. For some reason, this command only works with sudo right now. The workaround is to run this to start Redis server as a daemon:

1sudo redis-server --daemonize yes
1sudo redis-server --daemonize yes

Cleaning up after PECL (optional)
By default, PECL plops a new extension="redis.so" line at the top of the main php.ini file. I prefer to move this line to its own extension file. These steps are optional, but it's more in line with how extensions should be loaded in modern php versions.

Remove the extension="redis.so" line that PECL added at the top of /usr/local/etc/php/7.4/php.ini.

Then create a file at /usr/local/etc/php/7.4/conf.d/ext-redis.ini with these contents:

1[redis]
2extension="redis.so"
1[redis]
2extension="redis.so"

After doing all of this, I'd recommend running valet restart. Enjoy developing Laravel apps on your new mac!

If you encounter any problems or have any thoughts about this process, reach out to me on Twitter, I'd love to hear about them!

Enjoy this article? Follow me on Twitter for more tips, articles and links.

Want Updates?

Sign up here if you want to stay in the loop about new articles or products I'm making.
I'll never spam you. Unsubscribe at any time.
Copyright ©2024 Austen Cameron