Search
  • tim64985

Laravel 7 on M1 Mac with Docker

Updated: May 17, 2021

I finally pulled the trigger and got a new Mac, an M1 MacBook Air. This machine will be used as my main development machine and one of the projects that I’m working on now is in Laravel 7.4 with PHP 7, MySQL, Composer, etc.. you know the stack. So I started with some google searches and came up with a few very helpful websites:


Sam Soffes about homebrew on M1:

https://soffes.blog/homebrew-on-apple-silicon

 

Tighten’s Laravel M1 page:

https://laravelm1.tighten.co/

 

Austen Cameron’s Post:

https://austencam.com/posts/setting-up-an-m1-mac-for-laravel-development-with-homebrew-php-mysql-valet-and-redis


But I was also interested in Docker since Laravel 8 is now using Laravel Sail (link) and I’ve used docker for a Laravel 8 project already.


Andrew Schmelyun had a great post here which I'm following almost exactly:

https://dev.to/aschmelyun/the-beauty-of-docker-for-local-laravel-development-13c0


I’m pretty much using Austen’s Homebrew / Composer / MySQL / PHP post and Andrew’s Docker posts to get everything up and running so that I think I have the best of both worlds. I followed all the steps on Austen’s post but skipped installing Redis and Valet for now, just because I’m using Docker and can have Redis on docker and don’t need Valet right now. I wanted composer, mySQL and PHP but really it’s not necessary if you’re willing to do all commands through docker as Andrew explains in his post. I changed a few things on Andrew’s configuration of the docker stack. Here are my steps. I’ve put a comment after each one for whom it belongs to. I also wanted Node and NPM on my Mac so I follow the Node website, you'll see it below.


First we need to install rosetta (this is from Apple)

https://support.apple.com/en-us/HT211861


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.

I use the nano app to edit the .zshrc file, as shown below.

nano ~/.zshrc

This should give you the correct permissions if you use sudo make sure to change permissions after the .zshrc is created.


Edit the file and add:

arm() {
 arch -x86_64 $@
}

after you save make sure to close out of terminal and go back in! (this will create a new console with the arm command accessible.


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!
arm /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Install PHP by running:

arm brew install php

Install MySQL by running:

arm brew install mysql
arm brew services start mysql

Install Composer by running:

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

Now add the following line to your .zshrc file

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

Intall Node / NPM / NVM

Go to Node's and NPM website download and install Node JS

https://nodejs.org/en/


Once Node is installed you'll need install NVM

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
nvm install

Check Versions of Nod

node -v
npm -v

From there we move on to Andrew’s post:

He also has a YouTube page:

https://www.youtube.com/channel/UCc07-IBVwRlOsMg2WMdd8Sg


Install docker desktop from here

https://www.docker.com/products/docker-desktop

Make sure to download the M1 (Apple Chip) version!



He explains everything in there and does an incredible job at it, I’m just going to give you the next steps but please read through his entire post.

https://github.com/aschmelyun/docker-compose-laravel


Open with GitHub desktop - you need to install GitHub if it's not installed already


I made minor changes to Andres docker-compose.yml file I wanted to have port 80 working so you can: http://localhost into it and I made small modifications to the mysql version.

Download the repo here:

https://github.com/Stack-Systems/m1-laravel-docker

Terminal into the directory


Terminal into the directory and run the following:

docker-compose build
docker-compose up -d

Ok so now your docker should be up and running if you go into the docker desktop app you should see your machines running.


The final steps are all to install Laravel and get it working now.


Your Laravel root directory is the /src folder in the docker directory. Place all your files in this folder and remember to create an .env file


THIS IS IMPORTANT make sure to change the DB_HOST=mysql (this is what we're calling the mysql docker in the yml file.


Finally just make sure to make this change to your .env file

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=hometead
DB_PASSWORD=secret

Connect: http://localhost


To run migrations follow the instructions in Andrew's Blog but you'll run them from within docker

docker-compose exec php php /var/www/artisan migrate


Hope this helps someone.



298 views0 comments