Setting Up Laravel Development Environment
# CHAPTER 2
Setting Up Laravel Development Environment
1. Introduction
Unlike a simple HTML file, you cannot just download Laravel in a.zip file and double-click it. Laravel is a massive ecosystem of interdependent PHP packages. To install and run Laravel, you need a package manager, a local server environment, and a command-line interface. In this chapter, we will walk through the exact steps to install Composer, create a new Laravel project, and boot up your local development server.
2. Learning Objectives
By the end of this chapter, you will be able to:- Understand and install Composer (PHP's package manager).
- Create a brand new Laravel application via the command line.
- Configure Laravel to connect to a MySQL database.
- Use Laravel Artisan to serve the application locally.
3. Beginner-Friendly Explanation
Imagine building a custom car. You don't manufacture the tires, the steering wheel, and the engine from scratch. You order the parts from a factory and assemble them. In PHP, Composer is the delivery truck. When you tell Composer, "I want to build a Laravel app," Composer drives to the internet, downloads the 50+ different software packages that Laravel relies on (routing, security, database connectors), and perfectly assembles them into a new folder on your computer. Once the parts are assembled, you use Artisan (Laravel's built-in mechanic) to turn the engine on.4. Step 1: Installing Prerequisites
Before installing Laravel, your computer MUST have:- 1. PHP (8.1 or higher): Installed via XAMPP (Windows/Mac) or Laravel Herd (Mac/Windows).
- 2. MySQL: Included in XAMPP.
-
3.
Composer: Go to
getcomposer.org, download the installer, and run it. (It will ask you wherephp.exeis located. If using XAMPP, it's usuallyC:\xampp\php\php.exe).
5. Step 2: Creating a New Laravel Project
Once Composer is installed, open your Terminal (Mac) or Command Prompt/PowerShell (Windows). Navigate to the folder where you want your code to live (e.g., your Desktop orhtdocs).
Run this exact command to instruct Composer to download Laravel and create a folder named my-first-app:
*Note: This will take a few minutes as Composer downloads thousands of files.*
6. Step 3: Using Laravel Artisan to Run the Server
In core PHP, we had to put our files insideC:\xampp\htdocs and use Apache.
Laravel is smarter. It has a tiny web server built into it!
In your terminal, navigate inside your new project folder:
Now, tell the Artisan mechanic to start the engine:
Your terminal will output: Server running on [http://127.0.0.1:8000].
Open your web browser and type http://127.0.0.1:8000. You will see the beautiful default Laravel welcome screen!
7. Step 4: Configuring MySQL (The .env File)
Laravel uses a hidden file called .env (Environment) to store secret database passwords. This file is never uploaded to the public internet.
- 1. Open XAMPP and make sure MySQL is running.
-
2.
Go to
http://localhost/phpmyadminand create a new database calledlaravel_db.
- 3. Open your Laravel project in VS Code.
-
4.
Find the
.envfile in the root directory. Modify the Database section:
Save the file. Laravel is now perfectly connected to your MySQL database!
8. Understanding the Directory Structure
When you open a Laravel project, the amount of folders is overwhelming. Here is what matters right now:-
app/Http/Controllers/- Where your logic goes.
-
routes/web.php- Where you define your URLs.
-
resources/views/- Where your HTML/Blade files live.
-
public/- The only folder the outside world can see. (It holds CSS/JS).
9. Best Practices
-
Never Commit
.env: The.envfile contains your super-secret database passwords. Laravel automatically configures Git to ignore this file. Never bypass this. If you put.envon GitHub, hackers will steal your database.
10. Common Mistakes
-
The "Missing Vendor Folder" Error: If you download a Laravel project from GitHub, it will not work immediately. GitHub does not store the massive
vendor/folder (the engine parts). You must open your terminal and runcomposer install. This tells Composer to read your project files and re-download all the missing parts.
11. Exercises
-
1.
Explain the role of Composer in the PHP ecosystem. Why don't we just download a
.zipfile of Laravel?
12. Coding Challenges
-
Challenge: Successfully run
composer create-project laravel/laravel task-managerin your terminal.cdinto the directory, runphp artisan serve, and view the homepage in your browser.
13. MCQs with Answers
What is Composer?
Which command is used in the terminal to start Laravel's built-in local development web server?
14. Interview Questions
-
Q: Explain the purpose of the
.envfile in a Laravel project. Why is it used, and what are the security implications of uploading it to a public repository?
- Q: If you clone an existing Laravel repository from GitHub, what is the first terminal command you must run to make the application functional, and why?
15. FAQs
Q: I get an error saying 'composer is not recognized as an internal or external command'. A: This means Windows doesn't know where Composer is installed. You need to reinstall Composer and ensure you check the box that says "Add to PATH", or manually add the Composer path to your Windows Environment Variables.16. Summary
In Chapter 2, we built our development laboratory. We learned that Composer is the magical package manager that downloads and perfectly assembles the massive Laravel framework on our computers. By utilizing thephp artisan serve command, we bypassed the need for complex Apache folder configurations. Finally, by editing the .env file, we securely tethered our new Laravel application to our local MySQL database.