Moving From MAMP to Valet: For Craft CMS 3 and Everything Else

Tim Knight
11 min readMar 13, 2018

When you’re use to a certain workflow it can feel a bit uncomfortable to switch things up. Often it’s some pain point in your existing workflow that finally gets too uncomfortable that you push to find other options. As someone who is fairly comfortable with the command line I still enjoyed using MAMP Pro because it gave me a quick way of just getting a site online locally. With that convenience however came some annoyances, like making sure to edit my http.conf with each update so that it continued to point to the latest FastCGI to help reduce performance issues in Craft CMS. Then came dealing with self-signed certificates when I wanted to run something locally over SSL. Sure it was easy enough, but it was minutes added to a workflow that just became annoying.

When Craft CMS 3 came out with their terminal interface to install and setup Craft using a simple ./craft command-line tool, I noticed that MAMP required yet another small addition, specifically adding the socket path to be able to connect to MAMP’s MySQL using the command-line. Enough was enough, it was then when I started looking for other options.

Getting Valet Installed

If you’re not familiar with it, Valet is a lightweight development environment created by the Laravel Project described as:

Valet is a Laravel development environment for Mac minimalists. No Vagrant, no /etc/hosts file. You can even share your sites publicly using local tunnels.

A mix of Nginx, PHP, and Dnsmasq points all sites using the *.test TLD (or an alternative TLD you configure) to your local computer meaning there’s no more using the hosts file to deal with your local development.

Installing Valet can be done through two tools on the Mac. First is Homebrew, a terminal package manager for Mac. If you’re not using it as a developer on your Mac, you’re really missing out, it simplifies the process of installing and managing applications and services you’d use in the terminal. So first you’re going to want to get that installed. You’ll want to open up your Terminal application, which if this is a new process for you, can be found within your /Applications/Utilities folder. Other terminal applications that are popular are iTerm and Hyper, but all-in-all they do the same thing. I’m going to use $ to represent the prompt on your terminal, don’t worry about typing that in that’s just to represent the start of the prompt. To install Homebrew you’ll just paste the following in (don’t forget, leave off the first $ symbol):

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

With Homebrew installed, you’ll want to make sure all of those applications Homebrew cooks up for you (see what I did there?) are available to you so we’ll need to get Homebrew in your PATH. The easy way to do this is just to run this:

$ echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bash_profile

This basically takes the /usr/local/bin and sets it before everything else in your path, so Homebrew’s stuff takes priority.

Next up we’re going to install the rest of the tools we’ll want for this process. The second primary tool we’ll need is composer and we’re going to use Homebrew to install it. If you’re developing on Craft CMS 3 you already have composer installed, so you can likely omit this item in the list when you install everything else. Personally, I use Homebrew to install and manage all of this so it’s my go-to tool for installing all of these. We’ll also take this time to install PHP and ImageMagick.

$ brew install composer php imagemagick
$ pecl install imagick

Through the powers of Homebrew this will also install ImageMagick, a recommendation for Craft 3. PECL is automatically installed with PHP on Homebrew which allows you to install PHP extensions so you can install the PHP ImageMagick extension. When you execute PECL to install imagick you’ll be asked for the prefix. You can just press return there for it to autodetect.

Craft 3 does require 256MB of memory allocated to PHP. To update this within the PHP installation from Homebrew you’ll want to edit the file located at /usr/local/etc/php/7.2/conf.d/php-memory-limits.ini which is linked in the php.ini file. Right at the top of the file you’ll want to edit the value of memory_limit so it reflects accordingly:

memory_limit = 256M

Next, with Composer installed, just like with Homebrew, you’re going to want to add composer’s tools to our path as well. Again, the easiest way for that is just to execute:

$ echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bash_profile

As you can see from this new adjustment to the path, your global composer packages are stored within your user folder, within a .composer folder. Just to review both of those. If I look at my .bash_profile file I’ll see these two lines (as well as anything else I’ve customized):

export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
export PATH="$PATH:$HOME/.composer/vendor/bin"

Finally, we’ll want to use our composer installation to install Valet. All Valet is, is a composer package that will be installed globally throughout our entire machine as opposed to just within an individual project. We do this from the terminal using:

$ composer global require laravel/valet

Now that Valet is available on your system we have to install it to get all of it’s tools running, this step is incredibly simple though, using:

valet install

Once installed, Nginx and Dnsmasq are now running whenever you start your machine. If you ever want to stop, start, or restart you have valet stop, valet start, and valet restart respectively. If you decide to move onto something else for local development you can easily run valet uninstall to move on with your life.

Setting Up the Database

You’ll notice that Valet doesn’t come with a database server like MAMP does and since Craft 3 now support Postgres this is a good thing. If you’re comfortable with MySQL you can still use that, though I’d encourage you to look at MariaDB. It’s a MySQL folk created by the original developer of MySQL that was done out of concern with the Oracle acquisition of MySQL.

Since we have Homebrew installed, Homebrew is going to make the process of installing MariaDB pretty trivial.

$ brew install mariadb

MariaDB is installed with an empty password for the user of root. It’s also bound to localhost so only you can connect to it by default. As far as I remember you have to specifically state this within a MySQL setup usually. Homebrew also makes managing your services easier too with it’s services add on which it’ll install automatically for you if you attempt to use it. This integrates Homebrew formulae with macOS’ launchctl manager. Let’s setup MariaDB to start with our computer.

$ brew services start mariadb

You can get a quick list of all of the services running for you using brew services list which makes it very easy to see what’s happening in the background. If you wish to unregister it from launching at login, you just need to type brew services stop mariadb and it’s done.

Want to use MySQL instead? Just replace mariadb with mysql in your installation and service start, but I would encourage you to stick with MariaDB if you don’t have any strong opinions otherwise.

If you’re coming from MAMP you might be use to using Sequel Pro, a great application for managing your databases that I would continue to recommend for you on your Mac. If you decide to roll with Postgres some of my favorites for that include Postico and pgAdmin.

Linking Projects and Using SSL

Where Valet really shines is in how it starts up a host based on the directory structure of your projects. You have two options when using Valet: park or link. Using park will allow you to setup a parent directory for all of your projects. For example, let’s say my project folder is ~/Sites. From within this directory I could run:

$ valet park

Now any sub-directory within this parent directory will immediately have a host created for it. So if you have a project called myclient within that folder you’ll immediately have myclient.test as an available host. Pretty easy right?

But what if you don’t want to use every sub-directory? Well, that’s where link comes in. Instead of running valet park within the parent folder, you’ll run this individual per project folder. In this same example I’d go into the directory ~/Sites/myclient and then run:

$ valet link

This will create myclient.test. Don’t want to use the folder name and would rather call it something else? No problem, just pass that as an added attribute:

$ valet link theclient

With that you’ll have theclient.test. But what about that TLD? You can easily change that too. Just type in:

$ valet domain app

And now your domains will use the *.app extension.

I mentioned in the beginning that one of my favorite things about Valet was how it manages self-signed certificates. To get Valet to setup a self-signed certificate all you need to do is run:

$ valet secure

From within the project folder or include the project name from outside of the project folder like so:

$ valet secure project-name

If you want to remove the SSL certificate you’ll change out secure with unsecure and you’re back to using HTTP instead of HTTPS. Couldn’t be easier right? This process will generate the certificate and immediately add it to your certificate store on your computer and will set it as trusted for all users. Running the unsecure feature will reverse this process and delete that certificate from the store.

Installing and Setting Up Craft CMS

Let’s review where we’re at now. You have a server installed, a database, and a way to easily add new hosts to your system. That’s a pretty amazing feat. Now let’s look at how you’d install Craft 3 in this environment.

Since Craft CMS 3 uses Composer you already have the primary tool you’ll need to install the CMS. From within the project directory (example: ~/Sites/myclient) you’ll run:

$ composer create-project craftcms/craft .

The RC in this case gets the release candidate since Craft 3 remains in the RC stage and has not been officially released.

With that run you’ll be asked to run the setup script next.

But before you do that you’ll want to make sure the database has been created. The fastest way to do this (unless you want to use your GUI tool from above) would be to create it right from the terminal where you are. Use:

$ mysql -u root -e "CREATE DATABASE myclient DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci;"

In this case I’m creating a database called myclient to match the project name I’m using. Then I’m setting my collation to utf8_unicode_ci. With that finished you’ll run the setup script and follow the prompts.

$ ~/Sites/myclient/craft setup

For the most part you’ll be able to accept many of the defaults and have those written to your project’s .env file.

With that in place press enter again to begin the project installation of your site and you’ll have Craft fully installed after a few short questions. End that process by running valet link to link your current project if you haven’t already and you’re ready to go.

Once you login to your control panel you’ll be able to go into the Utilities and System Report and confirm that your installation meets all of Craft’s installation requirements.

(Optionally) Automating the Process

If you’ve read my previous article on “How I Automate My Local Development Craft CMS 2.x Installs”, you may have guessed that I’m a fan of automating some of the more repetitive stuff so I don’t have to remember all of the details each time I want to setup a new Craft 3 site. With Craft 3 however that process gets so much easier.

If creating shell scripts isn’t your thing, feel free to skip passed this section.

I start by checking to see if someone has passed an attribute after the script and use that to create the project name.

#!/usr/bin/env bash# Setup the project folder
if [[ $# -eq 1 ]]; then
# Grab the attribute passed in and use it to create a folder.
project=$1
mkdir $project && cd $project
else
echo "Usage:"
echo -e '\tType "gocraft [project]" to create a project with a specific name'
exit 1
fi

I then ask the user a few questions and setup values to use during the setup process.

# Use the valet domain setting to determine what the domain TLD is
domain="$project.$(valet domain)"
read -r -p "What do you want to call this project? (e.g. My Project) `echo $'\n> '`" sitetitle
echo ""
read -r -p "What email address should be used for the admin? `echo $'\n> '`" adminEmail
echo ""

With those questions answered, I create the database, pull down Craft 3 using composer, then execute all of the necessary setup scripts.

echo "Setting up Craft Project";mysql -u root -e "CREATE DATABASE \`$project\` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci;"composer create-project craftcms/craft ../craft setup/security-key
./craft setup/db-creds --interactive=0 --database=$project --user=root
./craft install --interactive=0 --email="$adminEmail" --username=admin --password=password --siteName="$sitetitle" --siteUrl=http://$domain
touch templates/index.twigvalet link

I include the line to create the index.twig just to include a blank document at the web root so you don’t immediately get an error. Finally, I link the project to Valet before echo the project details to the screen.

echo ""
echo ""
echo ""
echo "Project Details"
echo "---------------"
echo "Title: $sitetitle"
echo "Domain: $domain"
echo "Admin: $domain/admin"
echo "Username: admin"
echo "Password: password"
echo "Database: $project"

Once finished you’re given a quick summary of the new development site setup and the login details to use to get you logged in.

It’s Not Just for Craft CMS

Valet is a lightweight and powerful solution to local development on the Mac. As I finish up this article I’m happy to say that not only have I moved away from MAMP Pro but I’ve uninstalled it altogether. My development environment feels lighter, clean, and so much faster. Valet is just about helping you with Laravel or Craft CMS installations. There are adaptors for WordPress, Drupal, Joomla, Concrete5, Statamic, and most popular PHP based platforms that you might be using. Even better, if you just want to mess around with static files it’s great for that too.

Since Valet is based around platform adaptors, if you’re using a non-standard folder for your Craft 3 installation you’ll want to checkout this Gist that will let you run Craft using custom public folder paths.

What Craft does for you in terms of comfort, power, and flexibility, Valet does for you in terms of development environment setup. You’ll no longer feel like you’re fighting your machine just to get going, so what do you have to lose?

Updated on April 18, 2018: Homebrew recently deprecated the homebrew/php tap and made some adjustments in how it manages PHP versions. I’ve since adjusted this article to work with these latest changes.

--

--

Tim Knight

VP of Product Design @ Mad Mobile. Former Dir of UX @ GravityFree. Product Designer. Prototyper. Design Leader.