avatarDino Cajic

Summary

The article provides a comprehensive guide to setting up a Laravel development environment using Docker on a new MacBook with Apple's M1/M2 chips.

Abstract

The article outlines the process of setting up a development environment for Laravel applications on MacBooks with Apple's M1/M2 chips. It begins by listing the necessary tools: Homebrew, Composer, and Docker, and provides step-by-step instructions for their installation. The author addresses potential errors that may occur during the installation process, particularly when using the Apple chip architecture, and offers solutions to overcome these issues. The guide covers installing Laravel via Docker, cloning a Laravel project from GitHub, and configuring the environment for running the application. The article aims to assist developers in navigating common pitfalls and ensuring a smooth setup experience.

Opinions

  • The author acknowledges the potential frustrations of setting up a new development environment, especially with new hardware like the M1/M2 chips.
  • There is an expectation that readers may encounter errors specific to the Apple chip architecture, and the author provides troubleshooting steps for these scenarios.
  • The author suggests that installing Homebrew and Composer using the provided commands should be straightforward and problem-free for most users.
  • The article implies that the process of setting up Laravel with Docker on the new MacBooks can be challenging but is ultimately achievable with the right guidance.
  • The author expresses a hope that their sarcastically described "fun time" in dealing with the setup challenges will benefit others facing similar issues.
  • The author's background in software engineering and his current role as Head of IT at multiple companies lend credibility to the guidance provided in the article.

Setting up Laravel/Docker on the new MacBook: Apple Chip (M1/M2)

We’re going to pretend that you just bought a new Mac and are trying to get up and running as quickly as possible. There are going to be a couple of things that you’ll need to get started, such as brew, composer, and Docker.

If you don’t need to pull a Laravel project from GitHub, and this is your first time installing a Laravel project on your computer, you can skip the Brew and Composer installation steps. Make sure that you have Docker installed and run the Laravel install command.

If you do have a Laravel project, and want to clone it, the simplest method that I found was to have composer installed locally so that you can install the vendor files, sail specifically, and create the docker container with sail.

Read this article and others on my website:

Brew

The simplest way to install composer is with brew. Open your terminal and type in brew. If you don’t get zsh: command not found: brew, you don’t have it installed. Luckily, installing it is one line away.

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

Just copy and paste the code above into your terminal and watch it go.

It’ll ask for your sudo password. Enter it and press Enter. You’ll have to hit Enter once more to start the script. Sit back and watch it install.

Once it’s installed, it’ll tell you to run two commands.

==> Next steps:
- Run these two commands in your terminal to add Homebrew to your PATH:
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/YOUR_USER_NAME/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
- Run brew help to get started
- Further documentation:
https://docs.brew.sh

Run those two commands. Type in brew afterwards into your terminal and watch all of the commands populate.

Composer

Now that you have brew installed, it’s time to install composer. Run the following command in your terminal.

brew install composer

First Potential Error

If you’re running on the Apple chip, you may get an error.

Warning: No available formula with the name "composer".
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
==> Searching for a previously deleted formula (in the last month)...
Error: No previously deleted formula found.
==> Searching taps on GitHub...
Error: No formulae found in taps.

To fix it, run the following commands:

rm -rf $(brew --repo homebrew/core)
brew tap homebrew/core

Second Potential Error

And you might get another error. This time with git. This is during the time that you’re trying to reinstall homebrew/core.

error: RPC failed; curl 56 LibreSSL SSL_read: error:06FFF064:digital envelope routines:CRYPTO_internal:bad decrypt, errno 0

To fix this error, run the following lines:

git init
git config http.postBuffer 524288000

Then, rerun: brew tap homebrew/core

Finally

If you didn’t experience any errors, great. If you did, you’ll have to attempt to install composer again.

brew install composer

Docker

The last dependency that you’ll need is Docker. Just visit https://www.docker.com/ to get started. Be careful if you have the Apple (M1/M2) chip. You’ll need to click on the Apple Chip download for it to download the correct version.

Once Docker is installed, you’ll see it running in your top menu.

Click on the Docker logo to open up the dropdown and select Dashboard. You won’t have anything running.

Laravel Fresh Install

To install a new fresh instance of Laravel now is really straight-forward. Run the following command in the directory of your choice. I setup a www folder under my root directory where I keep all my code.

curl -s "https://laravel.build/example-app" | bash

The example-app should be replaced with your app name.

Once it’s installed, navigate to your newly created app directory and run the sail upcommand.

cd example-app
./vendor/bin/sail up

You’re going to see your Docker application running in the Docker Dashboard.

See what the output is going to be. For me it was http://0.0.0.0.

Laravel GitHub Install

Clone your repository from GitHub. Once cloned, cd into your directory and run composer install.

If you get an error, you may have to run composer update first. Once updated, run composer install again.

Copy your .env.example file into a new .env file and configure any passwords if necessary.

cp .env.example .env

Run the sail up command.

./vendor/bin/sail up

I know you know what’s going to happen now…possibly an error on the Apple chips.

executor failed running [/bin/sh -c apt-get update     && apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2     && mkdir -p ~/.gnupg     && chmod 600 ~/.gnupg ...

To fix it, you’ll have to publish your Dockerfile and make a change. You’ll need to make sure that you have php installed first.

brew install php

Navigate to your project’s root directory and run the following:

php artisan sail:publish

This will create a new docker directory in your root directory. I have php 8.1 running, so I navigated to /docker/8.1/ and opened up the Dockerfile.

Delete the following lines of code.

&& curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | tee /usr/share/keyrings/yarn.gpg >/dev/null \
&& echo "deb [signed-by=/usr/share/keyrings/yarn.gpg] https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \
&& apt-get install -y yarn \

Save the Dockerfile and run the sail up command again.

./vendor/bin/sail up

You can get into your Docker container now. Click on your Laravel application.

When you click on the laravel.test-1 (in my case), you’ll be able to click on the CLI that appears at the top of the Docker Dashboard.

This opens up your integrated terminal. You can still open it up as an external terminal if you prefer.

You’ll now need to run your code to get everything up and running.

php artisan key:generate
composer update
php artisan storage:link
php artisan migrate --seed

See what the output is going to be. For me it was http://0.0.0.0.

Conclusion

Whenever something new comes out, stuff just breaks. I had a pretty fun time (I hope you sense the sarcasm) setting up Docker with Laravel on my M2 powered MacBook Air. I hope that this article helps someone going through the same issues.

Dino Cajic is currently the Head of IT at LSBio (LifeSpan BioSciences, Inc.), Absolute Antibody, Kerafast, Everest BioTech, Nordic MUbio and Exalpha. He also serves as the CEO at MyAutoSystem. He has over a decade of software engineering experience. He has a B.S. in Computer Science and a minor in Biology. His background consists of creating enterprise level e-commerce applications, performing research based software development, and facilitating the spread of knowledge through writing.

You can connect with him on LinkedIn, follow him on Instagram, or subscribe to his Medium publication.

Read every story from Dino Cajic (and thousands of other writers on Medium). Your membership fee directly supports Dino Cajic and other writers you read. You’ll also get full access to every story on Medium.

Laravel
Docker
PHP
Web Development
Programming
Recommended from ReadMedium