We're live-coding on Twitch! Join us!
Use Laravel Valet for a Super Quick Dev Server

Use Laravel Valet for a Super Quick Dev Server

From the early days of tedious drills to set up your development environment, things have moved way forward.

We had early signs of the direction things were going in with the advent of MAMP. Today, we have come to a point where we have portable, virtual boxes that can be used to develop and deploy your applications.

The newest addition to the I want to set it up quickly movement is Laravel Valet.

Though it's used in a slightly different manner, it still scores if you wish to set up your development environment in a zap!

During the course of this tutorial, you will learn.

  1. What is Laravel Valet and how it works.
  2. How it compares to Homestead/Scotch Box and their use cases.
  3. The complete Valet command set.
  4. How to use a database with Valet.
  5. Sharing your application with the world(yes, you can do that with Valet)

At the time of writing this article, Valet is only available for OSX. If you do not have OSX available, you are limited to Windows and *nix based options and the discussion that follows does not apply.

Laravel Valet - A Brief Introduction

Laravel Valet is a development environment that runs without Nginx and Apache. Yes, read that again.

It's a layer written upon the Caddy server to make setting up a Laravel environment easier.

Essential Reading: Learn React from Scratch! (2019 Edition)

Plus, it also employs DnsMasq to route requests to sites on your localhost so it takes the need to configure /etc/hosts and virtual hosts out of the equation.

Homestead and Scotch Box are very popular options for setting up a Laravel development environment. Let's see how they compare to Valet.

Homestead, Scotch Box, and Valet

Homestead and Scotch Box are portable development environments powered by Vagrant.

Vagrant itself is another software layer built on top of hardware virtualization tools like VMWare Workstation and VirtualBox.

It has automation infrastructure built into it as it supports provisioning tools like shell scripts, Chef, and Puppet that you can use to build pre-packaged, pre-configured virtual machines.

Though Vagrant runs purely from the command line, underneath, it uses VMWare Workstation, VirtualBox(or other hardware virtualization software) to create an isolated, virtual environment on your local machine that is used to run your applications.

Further, inside the virtual environment, you have softwares like Apache, and MySQL pre-installed.

Valet, on the other hand, buys into a completely different scheme of things.

It's installed on your physical machine and uses the Caddy server and DnsMasq to re-route requests to locally stored websites.

Bear in mind, if you opt for Valet, you still need to install pre-requisite softwares such as MySQL and RabbitMQ if they are required by your application.

Homestead/Scoth Box and Valet Use Cases

If you have understood the basic difference between Homestead/Scotch Box and Valet, the obvious question would be how their usage fits into different scenarios.

Valet is a lot lighter option than the mentioned Vagrant boxes so if computational resources are an issue, in that case, you are better off going with Valet.

Moreover, Valet is more suited to individuals. It's a nice alternative for solo artists who wish to steer clear of heavy configurations and software installation.

It can also serve to be quite a neat option to demo projects in quick client meetings.

For maximum portability, and in cases where your environment needs to be re-produced multiple times, say while working within a team, my suggestion would be to use Homestead or Scotch Box.

In a replicate across multiple machines scenario, Valet would need some work to set things up plus, remember it's OSX only?

Homestead and Scotch Box also open you up to deployment options. Generally speaking, Vagrant facilitates application deployment(through FTP, Heroku, and other options) using a single push command.

In case of Valet, deployment won't be a simple task. For a start, you will need to setup Caddy server on your production machine.

If you are still unsure, I would recommend going for Homestead/Scotch Box as it's a more professional, well-rounded option.

Valet is a rather personal, compact option. If those traits ring a bell, it's right down your alley.

Installing the Pre-Requisites

In order to install Valet, you should have Homebrew, PHP 7, and Composer installed.

Installing Homebrew

Homebrew is a package manager for OSX just like apt, yum, and pacman. You can read about it if you are interested in the details.

Execute the following command in your terminal to install Homebrew.

# install homebrew

The script will prompt you through the process so read and follow the steps carefully.

Installing PHP 7

Valet requires PHP's version 7.0 or greater.

In order to find out your current PHP version, execute the following command in your terminal.

# view php version
$ php -v

If the output shows a version less than 7.0 than you will need to update your PHP version.

Execute the following command to install PHP 7.0.

# install php 7 using homebrew
$ brew install homebrew/php/php70

Restart your terminal session, the output of the php -v command should now echo version 7 for PHP.

Installing Composer

Lastly, install Composer globally by following the instructions at the official website.

When you are done, you should be able to access Composer from anywhere in your terminal.

You can verify Composer's global installation by executing the following command.

# view composer version
$ composer -v

If the output of the command shows the Composer version, it was set up correctly.

Also, make sure Composer's bin directory is added to your system PATH. You can do that by appending the following line at the end of your ~/.profile file.

If you're on Windows, make sure you have the path (C:\Users\<Username>\AppData\Roaming\Composer\vendor\bin) to composer in your environment variables.

export PATH="$PATH:$HOME/.composer/vendor/bin" # Add composer bin directory to PATH 

Once you have Homebrew, PHP, and Composer installed, you can proceed to installing Valet.

Installing Valet

Before installing Valet, make sure there is no application installed and running that uses port 80. Generally, web servers such as Apache and Nginx use port 80 which will be required by Valet to run the Caddy server. If you have any service(or web server) configured to run on port 80, you should disable it.

Execute the following command to query any running services on port 80.

# check if an application
# is using port 80
$ netstat -an | grep "\.80" | grep LISTEN

If the command produced empty output, port 80 is clean, and you are good to proceed with Valet installation.

Execute the following command to require Valet globally.

# install valet package using composer
$ composer global require laravel/valet

Next, install Valet.

# run valet setup
$ valet install

If Valet was installed correctly, you will be prompted with the Valet installed successfully! message.

An Extra Word on Valet Installation

Despite following the aforementioned installation steps, you may run into a funny [DomainException] error as shown in the screenshot.

Valet installation error

If that is the case, update Homebrew and upgrade Homebrew formulas first.

# update homebrew
$ brew update
# upgrade homebrew formulas
$ brew upgrade

Next, install DnsMasq using Homebrew.

# install dnsmasq using homebrew
$ brew install dnsmasq

Then proceed with Valet installation using the valet install command.

Using Laravel Valet

Phew! we have got Valet installation out of the way so let's get to seeing it in action.

Controlling the Valet Daemon

The Valet installation configures a daemon to run automatically when your system boots.

This makes sure all your sites(that have been setup) are served without re-configuration or manually starting the web server.

However, you can jump into the automated process and do as you like with the Valet daemon.

You can execute the following commands to control the Valet daemon.

# restart the Valet daemon
$ valet restart
# start the Valet daemon
$ valet start
# stop the Valet daemon
$ valet stop

Valet Daemon

If you wish to empty out port 80 and work with other web servers, these commands can come in handy.

Parking/Unparking Directories

The park command allows you to add a directory to Valet's path so that Laravel sites can be automatically detected and served from it.

Let's say you keep all your Laravel projects at ~/projects/laravel/.

In that case, here is how you would park this directory.

# move to the laravel projects directory
$ cd ~/projects/laravel/
# execute the park command inside the directory
$ valet park

Valet Park Command

Valet's auto-generated URLs follow the convention http://directory-name.dev.

For instance, let's assume you had the following directories inside the ~/projects/laravel/ directory as shown in the screenshot.

Projects Directory

In that case, your Laravel projects would be available at the URLs http://bar.dev, http://blog.dev, http://foo.dev, http://marvel.dev, http://pastebin.dev, http://snappy.dev, and http://zoo-tycoon.dev respectively.

The park command is a nice, time-saving shortcut if you keep all your Laravel projects in one place.

The forget command does the opposite of the park command.

If you wish to remove a directory from the list of parked directories, here is how to do it.

# move to the laravel projects directory
$ cd ~/projects/laravel/
# execute the forget command inside
# the directory to unpark it
$ valet forget

Valet Forget Command

If you wish to view the list of parked directories, you can execute the following command.

# list parked directories
$ valet paths

Valet Paths Command

Linking/Unlinking Directories

It could be that your projects directory contains applications of different race and color(like Ruby, Python, and Java). In such a case, it may not be wise to park the whole directory as non-PHP applications, though parked, will be of no use.

In such a case, you can use the link command to symbolically link a single directory to Valet.

For example, re-visiting the previous screenshot, assume marvel was the only Laravel application in your directory.

You could link it like so.

# move to the marvel directory
$ cd ~/projects/laravel/marvel/
# link the marvel directory to
# make it accessible at http://<app-name>.dev
$ valet link <app-name>
# example: valet link marvel

Valet Link Command

As previously mentioned, your application will be accessible at http://marvel.dev.

You can also unlink directories by following the same routine, the only difference is the keywork unlink.

# move to the marvel directory
$ cd ~/projects/laravel/marvel/
# unlink the marvel directory
$ valet unlink <app-name>
# example: valet unlink marvel

Valet Unlink Command

Also be sure to provide the same <app-name> when unlinking that you provided when linking.

If you wish to view the list of linked directories, you can execute the following command.

# list linked directories
$ valet links

Valet Links Command

Changing Default Domain Suffix

Valet's default domain schema follows the convention http://<directory-name>.dev.

If you wish to change the domain's dev suffix, you can do so by executing the domain command.

# make all local sites
# available at http://<directory-name>.<suffix> 
$ valet domain <suffix>
# example: valet domain abc

Valet Domain Command

All your local sites will now be accessible at http://<directory-name>.<suffix>.

Streaming Logs to Terminal

Though your Laravel application's logs are written to disk and you can skim through them at your leisure, sometimes you wish to look at things as they happen.

In such a case, you can instruct Valet to stream your logs to the terminal using the logs command.

# puke all logs to terminal
$ valet logs

Sharing Your Site With the World

Brace yourselves, you are about to witness the coolest feature of Valet.

Earlier, we saw how you can set up local sites using the park and link commands.

Once you have set up a local site using either of these commands, you can create a sharing link that can be used to share your website with the world(read: anyone who has a internet connection).

Assuming you have linked the marvel directory using the link command, here is how to create a sharing link.

# move to the marvel directory
$ cd ~/projects/laravel/marvel/
# create a share link
$ valet share

A new window will be initialized with a new process. Your site will be accessible as long as the process keeps running. You can terminate the process by pressing ctrl+C.

Also, you can find the share url for your site next to the forwarding section. I have highlighted the sharing url generated in my case in the following screenshot.

Valet Share Command

Slam Dunkin Valet

Despite the convenience that Valet provides, there is still a very microscopic chance that for some reason, you do not like it.

If you want to remove Valet, execute the following command.

# uninstall Valet
$ valet uninstall

And you are done(or undone?).

Using a Database with Valet

So far, so good.

You have learned the complete command set of Valet and must be wondering, how do I use a database with this thing.

Recall, in the earlier section where I compared Valet to Homestead/Scotch Box, I mentioned that if you opt to use Valet, you will need to install pre-requisite software required by your application on your physical machine.

A database server (MySQL/MariaDB) falls under the umbrella of your app's requirements and you can install it using Homebrew.

# install MySQL
$ brew install mysql
# or install MariaDB
$ brew install mariadb

Once you have MySQL or MariaDB installed, simply configure your Laravel application with the correct database settings and it should work fine with Valet.

That's it. There are no extra, Valet-specfic steps needed.

Final Thoughts

Valet is a great option for Mac minimalists.

If you are the kind of designer/developer who does not work much with application infrastructure and is only concerned with application delivery, Valet can even serve to be a long term option.

I recommend diving into the Caddy server if Valet really interested you. Plus, you can also look into developing Valet drivers if you are up for a weekend coding challenge.

I hope you found this tutorial interesting and knowledgeable. Until my next piece, happy coding!

Like this article? Follow @nomanurrehman on Twitter