DEV Community

Cover image for Local Craft CMS Development with Laravel Valet 🚀
Chris Violette
Chris Violette

Posted on

Local Craft CMS Development with Laravel Valet 🚀

Craft CMS is a flexible, user-friendly content management system. There are many ways to run Craft on your local machine for development (such as MAMP or Docker), but I'm going to tell you about my current favorite — Laravel Valet.

Contents

Laravel? I thought this was about Craft! 😕

It is! Laravel Valet, while being made by the folks behind Laravel, can support many PHP-based frameworks — Craft CMS included.

Valet is a local development environment for Mac — sorry Windows/Linux folks. Still, check out my next article in this series to learn about adding Laravel Mix to the — erm, mix.

Rather than running a virtual machine à la Docker, Valet runs Nginx in the background and uses your local PHP and MySQL. This keeps your development environment very lean, only using about 7MB of RAM.

However, Valet isn't a replacement for an isolated container environment, just an alternative if you want some easy-to-configure basics. If you need a containerized setup, that's ok, but this isn't the article for you.

🏗️ Prerequisites

This article assumes you're comfortable with Terminal (or similar) to run commands. You should also be logged in as an admin user on your computer; some of these commands will require you to enter your Mac password.

It also assumes you have Homebrew and Composer installed. If you don't, I'll walk through the initial setup below:

💡 Already have Homebrew and Composer installed? Skip ahead!

Homebrew, PHP, and Composer

These installation instructions are accurate as of this writing, but I recommend checking Homebrew and Composer documentation for current details:

Homebrew

To install Homebrew (a package manager for macOS), open up Terminal and run the following:

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

Follow the instructions that appear; the script will pause at times to explain exactly what it's doing and confirm before doing it.

PHP

Once Homebrew is installed, run the following command to install PHP. As of this writing, it will install PHP 7.4.

brew install php

Confirm PHP is installed with:

php -v
# PHP 7.4.X

Composer

Finally, with PHP installed, run the following to install Composer, a dependency manager for PHP:

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

Installing Valet

Current installation instructions can be found here.

💡 Before installing, be sure nothing else is binding to your local machine's port 80 and 3306.

If you are running MAMP, it may be using these ports for Apache and MySQL. Stop those servers if that's the case.

Open up Terminal and run the following:

composer global require laravel/valet

Once Valet is downloaded, run:

valet install

This will start Valet and set it up to automatically start whenever your machine boots. Set it and forget it! 🍗

You can confirm Valet is running with the following:

ping foo.test

It should be responding from 127.0.0.1. Hit Control+C to stop pinging.

Installing Craft CMS

Current Craft installation instructions can be found here.

Before installing Craft, we first need to make sure we have MySQL installed. From a command line, run:

brew install mysql@5.7

Once installed, you can start MySQL with:

brew services start mysql@5.7

While we have MySQL on the brain, let's go ahead and create a database. I like to organize my local databases by naming them craft3_projectname, but you can use whatever name you like.

mysql -u root -e "CREATE DATABASE database_name"

No password is necessary; you should be able to connect to MySQL with just the username root.

Finally, we're ready to install Craft. In a command line, move to a directory you'd like to download the Craft files and run the following command, where <craft-directory> is the path where the project should be created.

💡 Valet will eventually map this project to [dir].test, so it's useful to name your Craft directory accordingly. For example, my-fun-project will be accessible at http://my-fun-project.test.

composer create-project craftcms/craft <craft-directory>

Composer will download & install Craft and a bunch of dependencies. When it's done, you should see a message like:

"Welcome to Craft CMS! Run the following command
if you want to setup Craft from your terminal:

    path/to/<craft-directory>/craft setup"

Go ahead and run that command. Follow the instructions and answer the following. Values in square brackets are default, so you can just hit return if they're already correct.

  • Which database driver are you using?: mysql
  • Database server name or IP address: 127.0.0.1
  • Database port: 3306
  • Database username: root
  • Database password: (blank, just hit return)
  • Database name: your_db_name
  • Database table prefix: leave blank, unless you need to have multiple installs on the same database

Craft will attempt to connect to your database and then save the credentials in an .env file in your project root.

Now, you will be asked if you want to install Craft. Choose yes, and then follow the prompts to create an initial admin user. Remember this information, you'll need it to log into the Craft admin panel.

For Site URL, http://<craft-directory>.test. Replace <craft-directory> with the name of directory in which you installed Craft.

Finally, Craft will create all the tables and initial data. Now, just one last step remains...

Connecting Valet to Craft

There are two commands Valet has for serving your sites: park and link. First, I'll cover the link command, useful for one-off projects.

valet link 🔗

Move to the directory you installed Craft in (what we've been calling <craft-directory> so far) and run the following command:

valet link

That's it. Your site is now live at http://<craft-directory>.test. Valet just created a symbolic link in ~/.config/valet/Sites and pointed it to the current directory.

If at any point you need to remove this link, run valet unlink <craft-directory>.

valet park 🚘

If you're like me, you probably have several sites currently in some form of development, and it's handy to have them all in one place. With valet park, any directory you add to a "parked" directory will be served at http://directory-name.test.

If I wanted my ~/Sites directory to be home to all my current projects, all I need to do is

cd ~/Sites
valet park

Done. No need to run valet link every time you make a new project, just create a new project directory and it does the linking for you.

More Projects

Next time you need to install a new Craft project, all you need to do is:

# Move to your parked projects directory
cd ~/Sites

# Create a new database
mysql -u root -e "CREATE DATABASE new_db_name"

# Install Craft
composer create-project craftcms/craft <new-site-name>

# Setup Craft, filling in database credentials
# with username root and no password
<new-site-name>/craft setup

<new-site-name> will automatically be served at http://<new-site-name>.test.

💡 If you didn't park a directory for multiple sites, move into the <new-site-name> directory and run valet link.

Wrapping Up

Laravel Valet is currently my favorite way to work on Craft CMS project. Being able to drop a project in my parked sites directory and just have it work is so useful to me. No more starting and stopping MAMP or Docker, no big virtual server images clogging up my hard drive, just a fast, lean development environment.

Have you been using Valet? Is Docker or MAMP more your style? Or have you made the leap to Craft's new similar tool, Nitro? Let me know what you think!


Coming up next, I'll show you how I use Laravel Mix and Tailwind CSS to make frontend development a breeze. Stay tuned...

Top comments (1)

Collapse
 
laurensiusadi profile image
Laurensius Adi

Fortunately there's Valet for Windows! github.com/cretueusebiu/valet-windows
It's not the easiest setup with Acrylic DNS, but it works!