Speed up local environment for Magento 2 with Valet+

Speed up local environment for Magento 2 with Valet+

12th Dec 2018 Adrian Sanduleasa

Local environment is a crucial part of how frustrated a developer can get. In the past I went from Ubuntu, to Windows to macOS, using LAMP, WAMP, MAMP, Vagrant, VM's and local server with Apache, PHP and MySQL, each of them have their advantages and disadvantages, but until now I wasn't fully happy with any of them, they were either too slow or unstable or hard to configure. So I was looking for a new and better way to setup my local environment and a colleague mentioned Valet+. I was intrigued by it and gave it a shot. All I can say is that it was a good decision, the main thing is that it's way faster than anything I ever used, and once it's set up, it's extremely easy to use. I had some hiccups during the setup process but all of it is covered in the steps below, so let's begin.

First of all, I'm using a clean version of macOS Mojave, Brew, iTerm for the terminal and Visual Studio Code for editing config files. So go ahead and open your terminal of choice and proceed with the followings:

1. brew install php@7.1 -> This will get the latest version of PHP 7.1
2. brew install homebrew/php/composer -> This will get the latest version of composer
3. composer global require consolidation/cgr -> CGR is a safer method for composer as it takes care of version conflicts
4. cgr weprovide/valet-plus -> This will get the latest version of Valet+
5. Add export PATH in your bash profile export PATH="$PATH:$HOME/.composer/vendor/bin" -> In order to do that, write sudo code ~/.bash_profile and this will open bash_profile with visual studio code. If it retruns an error saying that code is not a command or something similar to that, open Visual Studio Code, press CMD + Shit + P, type "Shell" and select Install 'code' command in PATH. restart terminal and sudo code ~/.bash_profile should work now. If it still doesn't work, write source ~/.bash_profile, that will force the terminal to update the available PATHS with the ones from .bash_profile.
6. valet fix -> This is the most crucial part of the setup, it runs a suite of test to Valet before installing it, read all the instructions that it gives and apply them.
7. valet install --with-mariadb -> This will install Valet+ with MariaDb instead of MySQL.
8. Go to your folder where you want to have all your sites and write mkdir sites && cd sites
9. Run valet park in sites directory
10. Download latest m2 version with sample data in a new folder under sites folder
11. Run mysql -u root -p <password>
12. Inside the mysql terminal write CREATE DATABASE magento_blank;
13. Type exit in mysql or control+c
14. Edit the following commands based on your setup and run them from the root of your magento folder:
php -d memory_limit=-1 bin/magento setup:install \
--admin-firstname="magento" \
--admin-lastname="admin" \
--admin-email="admin@email.com" \
--admin-user='admin' \
--admin-password="Admin123" \
--base-url="https://YOUR URL HERE" \
--use-rewrites=1 \
--db-host="localhost" \
--db-user="root" \
--db-name="magento_blank" \
--db-password="YOUR MYSQL ROOT PASSWORD HERE" \
--use-secure=1 \
--use-sample-data
15. Run valet open in m2 root and whatch it GO!

Now, I said at the beginning of the article that I had some hiccups, so here they are:

I had too many PHP versions installed so I had to clean them up using the following:

1. brew services list -> This will list all services installed via brew
2. brew services stop <service> -> This will stop the service. E.g: brew services stop php@7.2
3. brew uninstall <service> -> This will uninstall the service. E.g: brew uninstall php@7.2

I tried using both valet and valet plus, then uninstall it but I didn't removed it so when I re-tried the installation proccess it was telling me that I already have Valet installed so in order to fix that I did the following:

1. composer global show -i -> This will list all services required by composer
2. composer global remove <service> -> This will remove the service from composer. E.g: composer global remove laravel/valet
3. rm -rf <folder> -> This will completly remove the service. E.g: rm -rf laravel/valet

ERROR on valet install! Unable to determine linked PHP. This was fixed running the following:

1. brew unlink php && brew link php -> Which gave me another error -> Error: No such keg: /usr/local/Cellar/php
2. brew install php
3. valet install

After I finished all the 15 steps from above, the result was "Could not resolve", which basically means that the server is not started/working. In order to fix that I just ran vagrant secure in magento root and it was fixed.

That's about it for the Valet+ setup. Now every folder that you add in sites folder will have its own host at *.test. Let us know how you find yourself with Valet+ on Twitter