PHP for Mac: get started fast using Laravel Valet
Table of contents:
Install the Xcode Command Line Tools
Install the latest version of PHP
Make the global Composer packages available without their full path
Install Laravel Valet
Allow Laravel Valet to be run without admin privileges
Park Laravel Valet in your projects’ folder
Serve a specific project no matter where it is
Serve a project over HTTPS with TLS
Switch the version of PHP
Per-project PHP version
Install a database alongside Laravel Valet
Before we start, why don’t you take a look at Laravel Herd instead? It’s an even simpler solution for people who have better things to do than messing with Homebrew and troubleshooting weird bugs.
Laravel Valet is a minimalist’s dream development environment for macOS.
It’s a lightweight solution that’s fast and uses around 7MB of RAM.
Unlike Docker, Laravel Valet is pragmatic and has minimal impact on your Mac’s resources.
With Laravel Valet, you don’t have to manage the state of your containers, and you can work on an infinite amount of projects simultaneously since they’re always available.
Laravel Valet uses a driver system that supports a wide range of platforms, such as Laravel, Bedrock, CakePHP 3, ConcreteCMS, Contao, Craft, Drupal, ExpressionEngine, Jigsaw, Joomla, Katana, Kirby, Magento, OctoberCMS, Sculpin, Slim, Statamic, Static HTML, Symfony, WordPress, and Zend out of the box.
You can also extend Valet with your own custom drivers.
Xcode Command Line Tools are a neat little collection of tools provided by Apple. They’re super handy for developers who need to compile and debug applications from the terminal.
In our case, we won’t directly use them. But Homebrew will! So let’s get this out of the way before the next step:
Homebrew is like a super cool genie in a bottle for Mac users. If you’re already familiar with any Linux distribution, you will get this concept.
Homebrew is a non-official package manager that makes it ridiculously easy to install software on your Mac.
Instead of hunting down various files, struggling with dependencies, and wrestling with installations, you just tell Homebrew what you want, and it takes care of everything.
Run this command and it will automatically be installed on your Mac. Just follow the instructions that come next, it’s super easy!
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
With Homebrew installed, you can now also install the latest version of PHP on your system using the following command:
brew install php
Laravel Valet is built on top of PHP, so this is a mandatory step.
Instead of using the official way to install Composer, we’ll let Homebrew do all the work. It’s much quicker!
brew install composer
To add Composer’s global packages binaries into your
PATH, you can modify your shell’s configuration file.
If you’re using a bash shell, this is usually the .bashrc or .bash_profile file, and for Zsh users, it’s the .zshrc file.
Here’s a simple step-by-step guide:
- Open your terminal.
nano ~/.bash_profileif you’re using Bash, or
nano ~/.zshrcfor Zsh. This will open your shell’s configuration file in a text editor called nano.
- Add this line to the file:
- Save and exit by pressing ctrl+X, then Y to confirm saving, and finally Enter to use the same filename.
- Finally, reload your shell’s configuration by typing
Now, why does it mean and why do we need this? When you install packages globally with Composer, their binaries are placed in the
By adding this directory to your
PATH (a simple environment variable), you’re telling your shell “Hey, when I type a command, also look in this directory to see if you can find it.”
It’s a way to easily use the commands provided by the packages you install with Composer, without having to type the full path each time. Super convenient, right?
Now that we have Composer installed, we can install Laravel Valet as a global package:
composer global require laravel/valet
And thanks to the previous step, we can now do this to complete the installation:
Then, make sure it works by running the following command:
And you should see something like this:
PING foo.test (127.0.0.1): 56 data bytes64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.081 ms64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.139 ms64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.254 ms
When everything works correctly, Laravel Valet redirects any .test domain to your local Nginx server.
What’s annoying when using Laravel Valet frequently is that it’s always asking you for your password.
If you are willing to trust it with the safesty of your Mac, run the following command and be done with passwords!
Next, you’ll want to direct Valet to ‘park’ in your projects’ directory. This means Valet will automatically serve all projects the chosen directory:
cd /path/to/projects/foldervalet park
If you have a project in a random folder, you can also serve it without serving the whole folder.
valet link /path/to/project
Being able to serve local projects over HTTPS has several advantages. As someone who also develop native apps for the Apple ecosystem, I can think of serving secure local REST APIs for local iOS apps development, since Xcode doesn’t like it when they’re not.
valet secure /path/to/project
Whenever you need to, you can unsecure your project:
valet unsecure /path/to/project
With Laravel Valet, you are not restricted to just the latest version of PHP. You can also use older versions depending on your needs.
For instance, if you are still working with PHP 8.1, you can install it as well:
brew install firstname.lastname@example.org
And switch back in forth whenever you need it.
valet use email@example.com
Some projects are more modern than others. This is why it’s useful to be able to serve projects with different versions of PHP. Luckily, Valet makes it super easy thanks to the following command:
valet isolate /path/to/project
Databases can also be installed with Homebrew. You can even install multiple versions, just like with PHP.
brew install mysql firstname.lastname@example.org postgresql sqlite
Alternatively, you can use DBngin, a free database management tool and the easiest way to get started with PostgreSQL, MySQL, Redis & more.
It can even manage the software that you installed via Homebrew. 👌