Tutorial

Use Laravel Valet for a Super Quick Dev Server

Draft updated on Invalid Date
author

Noman Ur Rehman

Use Laravel Valet for a Super Quick Dev Server

This tutorial is out of date and no longer maintained.

Introduction

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.

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/Scotch 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.

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

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.

  1. php -v

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

Execute the following command to install PHP 7.0 using homebrew:

  1. 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.

  1. 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.

Add composer bin directory to PATH:

  1. export PATH="$PATH:$HOME/.composer/vendor/bin"

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.

  1. 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.

  1. composer global require laravel/valet

Next, install Valet.

  1. 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:

  1. brew update

And upgrade Homebrew formulas:

  1. brew upgrade

Next, install DnsMasq using Homebrew.

  1. 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:

  1. valet restart

Start the Valet daemon:

  1. valet start

Stop the Valet daemon:

  1. 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:

  1. cd ~/projects/laravel/

Execute the park command inside the directory:

  1. 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

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:

  1. cd ~/projects/laravel/

Execute the forget command inside the directory to unpark it:

  1. valet forget

Valet Forget Command

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

  1. 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:

  1. cd ~/projects/laravel/marvel/

Link the marvel directory to make it accessible at http://<app-name>.dev

  1. 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 keyword unlink.

Move to the marvel directory:

  1. cd ~/projects/laravel/marvel/

Unlink the marvel directory:

  1. 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.

  1. 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.

  1. valet domain <suffix>

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.

  1. 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:

  1. cd ~/projects/laravel/marvel/

Create a share link:

  1. 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

Uninstalling 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.

  1. 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:

  1. brew install mysql

Or install MariaDB:

  1. 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.

Conclusion

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!

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Noman Ur Rehman

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel