Laravel is one of the most popular PHP frameworks, loved by developers for its elegant syntax and powerful features. In this comprehensive guide, you'll learn three different methods to create your first Laravel project and set up your development environment properly.
Prerequisites
Before we start, make sure you have the following installed on your system:
- PHP 8.2 or higher
- Composer (PHP dependency manager)
- A code editor (VS Code, PHPStorm, etc.)
- Basic command line knowledge
Method 1: Create Laravel Project Using Composer
This is the most traditional and widely-used method. Composer is PHP's dependency manager, and Laravel can be installed directly through it.
Step 1: Check PHP and Composer Installation
First, verify that PHP and Composer are installed:
php -v
composer -V
You should see version information for both. If not, install them first.
Step 2: Create Project with Composer
Navigate to your desired directory and run:
composer create-project laravel/laravel my-first-app
This command will:
- Download Laravel and all dependencies
- Create a new directory called "my-first-app"
- Set up the initial project structure
Step 3: Navigate to Your Project
cd my-first-app
Step 4: Start the Development Server
php artisan serve
Your Laravel application is now running at http://localhost:8000
Method 2: Using Laravel Installer
The Laravel Installer is a global Composer package that provides a faster way to create new Laravel projects.
Step 1: Install Laravel Installer Globally
composer global require laravel/installer
Step 2: Add Composer's Global Bin to Your PATH
For macOS/Linux, add this to your ~/.bashrc
or ~/.zshrc
:
export PATH="$HOME/.composer/vendor/bin:$PATH"
For Windows, add this to your system PATH:
%USERPROFILE%\AppData\Roaming\Composer\vendor\bin
Step 3: Create a New Laravel Project
laravel new my-second-app
The installer will ask you to choose:
- Starter kit (Breeze, Jetstream, or none)
- Testing framework (Pest or PHPUnit)
- Database (MySQL, PostgreSQL, SQLite, etc.)
- Git repository initialization
Step 4: Navigate and Serve
cd my-second-app
php artisan serve
Method 3: Using Laravel Herd (Recommended for macOS)
Laravel Herd is the fastest way to get started with Laravel development on macOS. It's a native application that includes PHP, Nginx, and all necessary tools.
Step 1: Download and Install Herd
Visit https://herd.laravel.com and download Herd for macOS.
Step 2: Install Herd
Open the downloaded file and drag Herd to your Applications folder. Launch Herd from your Applications.
Step 3: Configure Herd
Herd automatically:
- Installs PHP (multiple versions available)
- Sets up Nginx
- Configures automatic site serving
- Provides a GUI for management
Step 4: Create Project with Herd
Open your terminal and run:
cd ~/Herd
laravel new my-herd-app
Your site is automatically available at http://my-herd-app.test
(no need for php artisan serve
!)
Herd CLI Commands
Herd comes with useful CLI commands:
# Switch PHP versions
herd php 8.3
# Open a site in browser
herd open my-herd-app
# List all sites
herd list
# Park a directory (auto-serve all projects)
herd park
Environment Configuration
After creating your project with any method, you need to configure the environment file.
Step 1: Copy Environment File
cp .env.example .env
Step 2: Generate Application Key
php artisan key:generate
Step 3: Configure Database
Open the .env
file and update database settings:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_database
DB_USERNAME=root
DB_PASSWORD=your_password
For SQLite (simpler for beginners):
DB_CONNECTION=sqlite
# DB_HOST=127.0.0.1
# DB_PORT=3306
# DB_DATABASE=laravel
# DB_USERNAME=root
# DB_PASSWORD=
Then create the database file:
touch database/database.sqlite
Step 4: Run Migrations
php artisan migrate
Project Structure Overview
Your Laravel project contains these important directories:
my-first-app/
├── app/ # Application logic
│ ├── Models/ # Database models
│ ├── Http/ # Controllers, Middleware
│ └── Providers/ # Service providers
├── routes/ # Route definitions
│ ├── web.php # Web routes
│ └── api.php # API routes
├── resources/ # Views, CSS, JS
│ └── views/ # Blade templates
├── database/ # Migrations, seeders
├── public/ # Public assets
├── config/ # Configuration files
└── .env # Environment variables
Essential Laravel Commands
Here are some commands you'll use frequently:
# Start development server
php artisan serve
# Create a controller
php artisan make:controller UserController
# Create a model with migration
php artisan make:model Post -m
# Run migrations
php artisan migrate
# Rollback migrations
php artisan migrate:rollback
# Clear cache
php artisan cache:clear
php artisan config:clear
php artisan route:clear
# List all routes
php artisan route:list
# Open tinker (REPL)
php artisan tinker
Installing Additional Tools
Node.js and NPM
For frontend assets, install Node.js:
# Install dependencies
npm install
# Run development build
npm run dev
# Run production build
npm run build
# Watch for changes
npm run watch
Laravel Breeze (Authentication)
Add authentication scaffolding:
composer require laravel/breeze --dev
php artisan breeze:install
npm install
npm run dev
php artisan migrate
Comparison: Which Method Should You Use?
Method | Best For | Pros | Cons |
---|---|---|---|
Composer | All platforms | Universal, reliable, CI/CD friendly | Slower, no GUI |
Laravel Installer | Frequent Laravel dev | Fast, interactive setup | Extra installation step |
Herd | macOS users | All-in-one, automatic serving, GUI | macOS only |
Common Issues and Solutions
Issue: Port 8000 Already in Use
php artisan serve --port=8080
Issue: Permission Denied on storage/
chmod -R 775 storage bootstrap/cache
Issue: Class "PDO" not found
Enable PDO extension in php.ini
:
extension=pdo_mysql
extension=pdo_sqlite
Next Steps
Now that you have Laravel installed, here's what to learn next:
- Routing - Learn how to create routes in
routes/web.php
- Controllers - Organize your application logic
- Blade Templates - Create dynamic views
- Eloquent ORM - Work with databases easily
- Middleware - Add request filtering
- Authentication - Secure your application
Conclusion
Congratulations! You've successfully created your first Laravel project. Whether you chose Composer for reliability, Laravel Installer for speed, or Herd for convenience, you're now ready to build amazing web applications.
Remember, the Laravel documentation at laravel.com/docs is your best friend. It's comprehensive, well-written, and always up-to-date.
Happy coding! 🚀