Laravel Valet Local Development Server
๐ฏ Summary
Laravel Valet is a blazing-fast, minimalist local development environment for Laravel projects. ๐ก Unlike Vagrant or Docker, Valet configures your Mac to always run Nginx in the background, then uses DnsMasq to proxy all *.test
domain requests to point to sites installed on your local machine. This guide provides a comprehensive overview of Laravel Valet, covering everything from installation and configuration to advanced usage and troubleshooting. By the end of this article, you'll be equipped to leverage Valet for a streamlined and efficient Laravel development workflow. โ
๐ Getting Started with Laravel Valet
Before diving into the details, let's cover the fundamental aspects of getting Valet up and running. We will explore the necessary prerequisites, step-by-step installation instructions, and initial configuration settings.
Prerequisites
To use Laravel Valet, ensure you have the following installed on your macOS system:
- macOS (obviously!)
- Homebrew (package manager for macOS)
- PHP (Valet supports multiple PHP versions)
Installation Steps
- Install Homebrew: If you don't have Homebrew, install it using the following command in your terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Install PHP: Install PHP using Homebrew. You can install a specific version (e.g., PHP 8.1):
brew install php@8.1
Make sure to link the PHP version correctly. Instructions are usually provided after installation.
- Install Composer: Composer is a dependency manager for PHP. Install it globally:
brew install composer
- Install Laravel Valet: Use Composer to install Valet globally:
composer global require laravel/valet
Make sure to add
~/.composer/vendor/bin
to your system's "PATH" environment variable. - Run Valet Install: After installation, run the following command to configure and install Valet:
valet install
This command configures Nginx and DnsMasq. You might be prompted for your administrator password.
Basic Configuration
After installation, Valet automatically starts its services. You can verify this by checking if Nginx and DnsMasq are running.
๐ ๏ธ Using Laravel Valet for Local Development
Now that Valet is installed, let's explore how to use it for your Laravel projects. The core concepts involve "parking" your projects and accessing them through the .test
domain.
Parking Your Projects
Parking allows Valet to serve all projects within a specific directory. This is ideal for organizing multiple Laravel projects.
- Navigate to the directory where you store your Laravel projects:
cd ~/Sites
- Park the directory using the
valet park
command:valet park
Now, any Laravel project within the ~/Sites
directory can be accessed via http://project-name.test
in your browser.
Linking Individual Projects
Linking is useful when you want to serve a single project outside of a parked directory.
- Navigate to your project directory:
cd /path/to/your/project
- Link the project using the
valet link
command, providing a name for the site:valet link project-name
You can then access the project via http://project-name.test
.
Securing Your Sites with TLS (HTTPS)
Valet makes it easy to secure your local sites with TLS using the valet secure
command.
- Navigate to your project directory (or a parked directory).
- Run the secure command:
valet secure project-name
This command generates a self-signed certificate for your site, allowing you to access it via https://project-name.test
.
๐ง Advanced Valet Configuration and Usage
Valet offers several advanced features that can further enhance your development workflow. These include custom domains, custom drivers, and service management.
Custom Domains
While Valet defaults to the .test
domain, you can configure it to use a different domain if needed. This is typically not necessary, but the option exists.
Custom Drivers
Valet's architecture allows you to define custom drivers for serving PHP applications other than Laravel. This is an advanced topic, but it provides flexibility for supporting different frameworks or CMSs. You might find one of these advanced configurations useful for another topic like "Deploying Laravel Applications on AWS".
Service Management
You can manage Valet's services using the following commands:
valet start
: Starts all Valet services.valet stop
: Stops all Valet services.valet restart
: Restarts all Valet services.valet uninstall
: Uninstalls Valet.
๐ Troubleshooting Common Issues
While Valet is generally reliable, you might encounter issues from time to time. Here are some common problems and their solutions.
.test
Domain Not Resolving
If you're unable to access your sites via the .test
domain, ensure that DnsMasq is running correctly. You can restart Valet to attempt to resolve this:
valet restart
Also, check your /etc/resolv.conf
file to ensure that DnsMasq is configured as the DNS server.
Nginx Failing to Start
If Nginx fails to start, it might be due to a port conflict. Check if another application is using port 80 or 443. You can also check the Nginx error logs for more details:
tail -f ~/.config/valet/Log/nginx-error.log
Sites Returning a 404 Error
A 404 error typically indicates that Nginx is not correctly configured to serve your project. Double-check that you have parked or linked your project correctly. Also, ensure that your Laravel project's public
directory is correctly configured as the document root.
Example Bug Fix: Artisan Command Not Found
Sometimes, when working with Laravel, you might encounter an "Artisan command not found" error when running commands via Valet. This usually happens if the PHP version used by Valet doesn't match the one required by your project. Here's how to address it:
- Check the PHP version used by Valet:
valet php
- Verify your project's PHP requirement: Look at the `composer.json` file in your Laravel project. The `require` section will specify the required PHP version (e.g., "php": "^8.0").
- If there's a mismatch, update Valet to use the correct PHP version:
valet use php@8.1 # Replace 8.1 with the correct version
- Restart Valet:
valet restart
Interactive Example: Debugging with Xdebug and Valet
Debugging is crucial in development. Here's an example of how to use Xdebug with Valet for efficient debugging.
Step 1: Install Xdebug
Install Xdebug using PECL:
pecl install xdebug
Step 2: Configure Xdebug
Add the following to your PHP configuration file (php.ini):
zend_extension="xdebug.so" xdebug.mode=debug xdebug.start_with_request=yes xdebug.client_host=127.0.0.1 xdebug.client_port=9000
Step 3: Restart PHP and Valet
Restart PHP-FPM and Valet to apply the changes:
brew services restart php@8.1 # Replace with your PHP version valet restart
Step 4: Configure Your IDE
Configure your IDE (e.g., VS Code, PHPStorm) to listen for Xdebug connections on port 9000. Set breakpoints in your code. When you run your Laravel application through Valet, the debugger will trigger at the breakpoints.
Here's an example debugging scenario in VS Code:
- Install the "PHP Debug" extension in VS Code.
- Create a launch configuration (`launch.json`) in the `.vscode` directory of your project. A sample configuration looks like this:
- Start listening for Xdebug connections in VS Code (Debug -> Start Debugging).
- Access your Laravel application through Valet in your browser. When the code execution hits a breakpoint, VS Code will pause execution and allow you to inspect variables, step through code, etc.
{ "version": "0.2.0", "configurations": [ { "name": "Listen for XDebug", "type": "php", "request": "launch", "port": 9000, "pathMappings": { "/path/to/your/project": "${workspaceFolder}" } } ] }
๐ฐ Optimizing Your Workflow with Valet
Valet can significantly boost your productivity. Here are a few tips to maximize its benefits:
Use a Consistent Project Structure
Organize your projects in a consistent directory structure to streamline parking and linking.
Leverage Valet's Share Feature
The valet share
command allows you to temporarily expose your local site to the internet using Ngrok. This is useful for sharing your work with others or testing webhooks.
Keep Valet Updated
Regularly update Valet to benefit from the latest features and bug fixes:
composer global update laravel/valet valet install
Code Sandboxes for Valet
Code sandboxes offer a quick way to test code snippets. You can integrate code sandboxes with Valet by creating a simple PHP file in your Valet-served directory.
Example: Create a file called `sandbox.php` with:
<?php phpinfo(); ?>
Access `sandbox.test/sandbox.php` to execute the code. This is especially useful for quickly testing PHP configurations or small pieces of code without affecting your main project.
The Takeaway
Laravel Valet is an indispensable tool for Laravel developers, offering a simple, fast, and efficient local development environment. By understanding its features and configuration options, you can significantly enhance your development workflow and focus on building amazing applications. Give it a try and experience the benefits firsthand! It helps tremendously with articles like "Building Your First Laravel Application".
Keywords
Laravel Valet, local development, PHP, Nginx, DnsMasq, macOS, Homebrew, development environment, valet install, valet park, valet link, valet secure, valet share, troubleshooting, debugging, Xdebug, PHP configuration, composer, artisan, Laravel development, development workflow
Frequently Asked Questions
What is Laravel Valet?
Laravel Valet is a local development environment for macOS minimalists. It configures your Mac to run Nginx and DnsMasq, providing extremely fast local development without the overhead of Vagrant or Docker.
Is Valet only for Laravel projects?
While Valet is designed to work seamlessly with Laravel, it can also serve other PHP applications using custom drivers.
How do I update Valet?
You can update Valet using Composer:
composer global update laravel/valet valet install
How do I secure my Valet sites with HTTPS?
Use the valet secure
command in your project directory:
valet secure
How can I share my local Valet site with others?
Use the valet share
command. This uses Ngrok to create a temporary public URL for your site.