Hello! Welcome to your first lesson in our journey to learn Laravel.
Today, we'll focus on setting up your local development environment. This is the foundational first step for any web developer. By the end of this lesson, you will have installed the two essential tools required to build Laravel applications: PHP and Composer. We'll explore what these tools are and why they're crucial for your work with Laravel.
What is a Local Development Environment?
Before we dive in, let's clarify what a "local development environment" is. Think of it as a personal web server running on your own computer. It allows you to build and test your web applications privately before you deploy them to the public internet.
For Laravel, our environment needs two core components:
- PHP: The programming language Laravel is built with.
- Composer: A dependency manager for PHP, which we'll use to install Laravel and other necessary code libraries.
You have a background in computer science, so you might be familiar with package managers from other ecosystems, like npm for Node.js or pip for Python. Composer serves the exact same purpose in the PHP world: it manages the external code packages (or "dependencies") your project needs to function.
We'll explore two excellent ways to get set up. Given your tight 7-day timeline, I highly recommend the first approach using Laravel Herd, as it's the fastest and most modern way to get started. The second, a manual installation, is a great alternative if you prefer to see how each component is installed individually.
Approach 1: The Modern Way with Laravel Herd (Recommended for Speed)
The creators of Laravel have built a fantastic tool called Laravel Herd. It's a free, high-speed development environment for both macOS and Windows that bundles everything you need—PHP, Composer, and a web server—into a single, easy-to-use application.
Why use Herd?
- It's fast and simple: It eliminates the need to install and configure multiple tools separately.
- It's official: It's built and maintained by the Laravel team.
- It's powerful: It handles local site routing automatically, so you can access your projects at simple
.testdomains (e.g.,my-project.test) without any complex configuration.
To get a better sense of why Herd is so popular, I recommend reading the introduction and "Why Laravel Herd?" sections of the following article.
Quickest way to setup PHP Environment (Laravel Herd + MySql)
This article from Backpack for Laravel provides a great overview of why Laravel Herd is a game-changer for local development.
Please read the introduction and the section titled 'Why Laravel Herd?'. Focus on understanding the benefits of using an integrated environment like Herd.
Installing Laravel Herd
Now, let's get it installed. The official Laravel documentation provides the most direct instructions.
The official Laravel documentation outlines the installation process using Herd.
Please read the section 'Installation Using Herd'. Follow the instructions for your operating system (macOS or Windows) to download and install Herd from its website. You can stop when you reach the 'IDE Support' section.
Verifying Your Installation
Once Herd is installed, it's crucial to verify that PHP and Composer are available in your command-line interface (CLI).
-
Open your terminal (Terminal on macOS, or PowerShell/Windows Terminal on Windows).
-
Run the following commands, one at a time:
php --versionYou should see output displaying the PHP version, for example:
PHP 8.3.4...composer --versionYou should see output displaying the Composer version, for example:
Composer version 2.7.6...
If both commands run successfully and show a version number, your environment is ready! You can skip the next section and move on to the conclusion.
Approach 2: The Manual Installation (Alternative)
If you prefer a more hands-on approach to understand each piece of the puzzle, you can install PHP and Composer manually. This video provides an excellent, step-by-step guide for both Windows and Mac users. It intentionally avoids bundled software like XAMPP so you can see exactly what's being installed.
How To Get Started With PHP, Composer & Laravel (Windows & Mac)
This video from the LearnWebCode channel provides clear, separate instructions for installing PHP and Composer on both Windows and macOS.
Watch the sections relevant to your operating system: If you are on Windows: Watch the PHP installation guide from 00:05:15 to 00:22:12. Watch the Composer installation guide from 00:50:33 to 00:57:23. If you are on macOS: Watch the PHP installation guide from 00:22:12 to 00:30:47. Watch the Composer installation guide from 00:57:23 to 01:02:11. The video will guide you through downloading the tools, setting up your system's PATH environment variable, and configuring PHP with the necessary extensions for Laravel.
Key Steps & Verification
After following the video, you need to verify that everything works from your terminal.
-
Important: Close and re-open your terminal window. This ensures it recognizes the changes made to your system's
PATH. On Windows, it's sometimes necessary to restart your computer entirely, as mentioned in the video. -
Run the verification commands:
php --versioncomposerRunning
composerby itself should display the Composer logo and a list of available commands. This confirms the installation was successful.
Test your understanding!
You've followed the manual installation steps, but when you type composer into a new terminal window, you get a "command not found" error. What are two common reasons this might happen, and how would you begin to troubleshoot them?
Show answer
-
The
PATHEnvironment Variable is Incorrect: The most likely reason is that the directory containing the Composer executable was not successfully added to your system'sPATH. Your operating system uses thePATHto find executable files without you needing to type out their full location.- Troubleshooting: You would need to re-open your system's environment variable settings and confirm that the path to Composer's directory is present and correct. For example, on Windows, this is often in a location like
C:\ProgramData\ComposerSetup\bin.
- Troubleshooting: You would need to re-open your system's environment variable settings and confirm that the path to Composer's directory is present and correct. For example, on Windows, this is often in a location like
-
The Terminal Session is Stale: Terminals load the
PATHvariable when they start. If you installed Composer but didn't close and re-open your terminal, it won't know about the new path.- Troubleshooting: The first and easiest step is always to close your current terminal window and open a brand new one. On Windows, a full system restart is sometimes required to make the changes take effect system-wide.
Conclusion
Congratulations on completing the first and most crucial setup step!
In this lesson, we've established your local development environment by installing PHP and Composer. You learned that:
- PHP is the scripting language that powers Laravel.
- Composer is PHP's dependency manager, which we use to install and manage code libraries.
- You can set up your environment using an all-in-one tool like Laravel Herd (the fast, modern approach) or by installing PHP and Composer manually (for a deeper understanding of the components).
The most important outcome is that you can now run php and composer from your terminal.
Preview of the Next Lesson:
With our tools in place, we're ready for the exciting part. In the next lesson, we will use Composer to install the Laravel framework and create your very first project. We'll then take a look at the project structure it generates.