Mac M1 setup for Local Development with Laravel Valet
Tags: laravel, valet, mac, m1
Published: 2021-04-02 10:11:58

Last week, I have received my new Macbook Pro M1. I am really amazed by its performance and battery timing. The one thing you instantly notice is that it does not heat up like the Intel-based Macbook Pro.

I won't go into the details about how I struggled to set up my local development environment with different options like Homestead, Laradoc, Sail etc. 

Using a new M1 Mac the best and easy solution is to use Laravel Valet.

Following are the steps:
### Setup ARM Compatibility
* Install Rosetta with the following command:
```bash
/usr/sbin/softwareupdate --install-rosetta --agree-to-license
```
* Add the following function to your `.zshrc` in order to make an alias for `arm` while running commands with x86 flags. Shoutout to [Matt Stauffer](https://twitter.com/stauffermatt/status/1329598260076572673?s=20)
```bash
arm() {
  arch -x86_64 $@
}
```

### Homebrew
* Run the following command to install homebrew
```bash
arm /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
```
* You can confirm the installation with `brew --version` and you should get a result like the following:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s91ljeygn84uil3tawrk.png)

### PHP
* Run the following command to install PHP latest version
```bash
arm brew install php
```
* If you want to install a specific version of PHP the run the following command
```bash
arm brew install php@7.3
```
* You can confirm the installation with `php -v` and you should get a response like the following
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p3sgffh9ioa5tqlbjws6.png)

### MySQL
* For the latest version of MySQL run the following command
```bash
arm brew install mysql
arm brew services start mysql
```
* For a specific version
```bash
arm brew install mysql@5.7
arm brew services start mysql@5.7
```
* Setup default root password with the following command
``` bash
mysql_secure_installation
```
* It will prompt you steps like the following image
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l64e2gbcajntw344pu7t.png)

### Composer
* Install composer via the following commands and then move the PHAR file to the bin folder
```bash
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
```
* Add the following line to your `.zshrc` file
```bash
export PATH=${PATH}:~/.composer/vendor/bin
```

### Valet
* To install Valet run the following command
```bash
composer global require laravel/valet
valet install
```
* You can confirm the Valet installation by running `valet -V` and you should get a response like the following
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2kqkbw2enwjsntuvnvq8.png)
* After successful installation, now you can either park an entire folder or link a single application folder. For more details go to [Valet](https://laravel.com/docs/8.x/valet#serving-sites) 

### Start Valet
* You can start Valet with the following command
```bash
valet start
```
* You should get a response like the following
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s1acpxhi9m0lcawefl70.png)
* Now, you can visit any of your Laravel application that you have already linked

### Some of the useful commands list
```bash
arm brew services list
```
```bash
arm brew services start mysql
```
```bash
arm brew services stop mysql
```
```bash
arm brew services restart mysql
```
```bash
valet start
```
```bash
valet stop
```
```bash
valet restart
```
```bash
valet install php@7.3
```
```bash
valet use php@7.3
```

##### References
* https://laravel.com/docs/8.x/valet
* https://austencam.com/posts/setting-up-an-m1-mac-for-laravel-development-with-homebrew-php-mysql-valet-and-redis
* https://flaviocopes.com/mysql-how-to-install/
* https://gist.github.com/operatino/392614486ce4421063b9dece4dfe6c21