Laravel Framework

Installation & Environment Setup

15 min Lesson 2 of 45

Prerequisites

Before installing Laravel, ensure your system meets the following requirements:

System Requirements

  • PHP 8.1 or higher with the following extensions:
    • BCMath PHP Extension
    • Ctype PHP Extension
    • cURL PHP Extension
    • DOM PHP Extension
    • Fileinfo PHP Extension
    • JSON PHP Extension
    • Mbstring PHP Extension
    • OpenSSL PHP Extension
    • PCRE PHP Extension
    • PDO PHP Extension
    • Tokenizer PHP Extension
    • XML PHP Extension
  • Composer - PHP dependency manager
  • Node.js & NPM - For frontend asset compilation
  • Database - MySQL 5.7+, PostgreSQL 10+, SQLite 3.8.8+, or SQL Server 2017+
Quick Check: Run php -v to check your PHP version, and composer -V to verify Composer is installed. If you see version numbers, you're good to go!

Installing Composer

Composer is a dependency manager for PHP, similar to npm for Node.js. Laravel uses Composer to manage its dependencies and packages.

Installing Composer on Different Platforms

Windows

1. Download Composer-Setup.exe from https://getcomposer.org/download/ 2. Run the installer 3. Follow the installation wizard 4. Verify installation by opening Command Prompt and typing: composer --version

macOS

# Using Homebrew brew install composer # Or download and install manually php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php composer-setup.php --install-dir=/usr/local/bin --filename=composer php -r "unlink('composer-setup.php');" # Verify installation composer --version

Linux

# Download and install Composer globally curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer sudo chmod +x /usr/local/bin/composer # Verify installation composer --version

Creating a New Laravel Project

There are two primary methods to create a new Laravel project: using the Laravel Installer or using Composer directly.

Method 1: Using Laravel Installer (Recommended)

The Laravel Installer is a convenient tool that creates new Laravel projects quickly.

# Install Laravel Installer globally via Composer composer global require laravel/installer # Make sure Composer's global bin is in your PATH # Add this to your ~/.bashrc or ~/.zshrc: export PATH="$HOME/.composer/vendor/bin:$PATH" # Create a new Laravel project laravel new blog # This creates a new directory called "blog" with Laravel installed

Method 2: Using Composer Create-Project

Alternatively, you can create a new Laravel project directly using Composer:

# Create a new Laravel project named "blog" composer create-project laravel/laravel blog # Navigate into the project directory cd blog # Start the development server php artisan serve
What Just Happened? Composer downloaded Laravel and all its dependencies into a new directory called "blog". This process may take a few minutes depending on your internet connection.

Specifying Laravel Version

To install a specific version of Laravel:

# Install Laravel 10.x (latest 10.x version) composer create-project laravel/laravel:^10.0 blog # Install Laravel 9.x composer create-project laravel/laravel:^9.0 blog # Install latest version (default) composer create-project laravel/laravel blog

Project Structure Overview

After creating your Laravel project, you'll see the following structure:

blog/ ├── app/ # Application core code ├── bootstrap/ # Framework bootstrap files ├── config/ # All configuration files ├── database/ # Migrations, factories, seeders ├── public/ # Public assets and index.php ├── resources/ # Views, raw CSS/JS, language files ├── routes/ # All route definitions ├── storage/ # Logs, cache, compiled views ├── tests/ # Automated tests ├── vendor/ # Composer dependencies (don't edit) ├── .env # Environment configuration ├── artisan # Artisan CLI ├── composer.json # PHP dependencies ├── package.json # Node dependencies └── vite.config.js # Vite configuration

Configuring the Environment (.env)

The .env file contains environment-specific configuration. This is where you configure database connections, mail settings, and other sensitive information.

Understanding .env Variables

# Application Settings APP_NAME=Laravel # Your application name APP_ENV=local # Environment: local, production, staging APP_KEY=base64:xxx... # Encryption key (auto-generated) APP_DEBUG=true # Debug mode (false in production!) APP_URL=http://localhost # Application URL # Database Connection DB_CONNECTION=mysql # Database driver DB_HOST=127.0.0.1 # Database host DB_PORT=3306 # Database port DB_DATABASE=laravel # Database name DB_USERNAME=root # Database username DB_PASSWORD= # Database password # Cache & Session CACHE_DRIVER=file # Cache driver: file, redis, memcached SESSION_DRIVER=file # Session driver QUEUE_CONNECTION=sync # Queue driver # Mail Settings MAIL_MAILER=smtp # Mail driver MAIL_HOST=mailhog # Mail server MAIL_PORT=1025 # Mail port MAIL_USERNAME=null # Mail username MAIL_PASSWORD=null # Mail password MAIL_ENCRYPTION=null # Mail encryption

Generating Application Key

The APP_KEY is used for encryption. Laravel automatically generates this when you create a new project, but if it's missing:

# Generate a new application key php artisan key:generate # This updates the APP_KEY in your .env file
Production Warning: Never commit your .env file to version control! It contains sensitive credentials. Always use .env.example as a template for others to create their own .env file.

Database Configuration

Laravel supports multiple database systems. Let's configure MySQL as an example:

Creating a Database

# Using MySQL command line mysql -u root -p # Create database CREATE DATABASE laravel_blog CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; # Create database user (optional) CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'secure_password'; GRANT ALL PRIVILEGES ON laravel_blog.* TO 'laravel_user'@'localhost'; FLUSH PRIVILEGES; EXIT;

Updating .env with Database Credentials

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_blog DB_USERNAME=laravel_user DB_PASSWORD=secure_password

Testing Database Connection

# Run migrations to test connection php artisan migrate # If successful, you'll see migration tables created # If it fails, check your database credentials in .env

Using SQLite for Development

SQLite is perfect for quick local development without setting up a full database server:

# Create SQLite database file touch database/database.sqlite # Update .env DB_CONNECTION=sqlite # Remove or comment out other DB_ variables # Run migrations php artisan migrate

Starting the Development Server

Laravel includes a built-in development server powered by PHP:

# Start server on default port (8000) php artisan serve # Output: # Starting Laravel development server: http://127.0.0.1:8000 # Start server on custom port php artisan serve --port=8080 # Start server on custom host php artisan serve --host=0.0.0.0 --port=8000 # This makes your app accessible from other devices on your network
Access Your App: Open your browser and visit http://localhost:8000. You should see the Laravel welcome page. If you see this, congratulations! Laravel is successfully installed and running.

Alternative: Using PHP Built-in Server

# Navigate to public directory cd public # Start PHP built-in server php -S localhost:8000 # Note: This method doesn't support artisan commands

Installing Frontend Dependencies

Laravel uses Vite for asset compilation. You'll need Node.js and NPM installed.

Installing Node.js

# Check if Node.js is installed node -v npm -v # If not installed, download from https://nodejs.org/ # Or use a package manager: # macOS (using Homebrew) brew install node # Ubuntu/Debian sudo apt install nodejs npm # Windows: Download installer from nodejs.org

Installing NPM Dependencies

# Install all dependencies from package.json npm install # This creates node_modules directory and installs: # - Vite (build tool) # - Laravel Vite Plugin # - PostCSS # - Axios (HTTP client)

Running Vite Development Server

# Start Vite development server (hot module replacement) npm run dev # In a separate terminal, start Laravel server php artisan serve # Your assets will now compile automatically when you make changes

Building Assets for Production

# Build and minify assets for production npm run build # This creates optimized, versioned assets in public/build/

Laravel Sail (Docker Development Environment)

Laravel Sail provides a Docker-based development environment with MySQL, Redis, Mailhog, and more pre-configured.

Creating a Project with Sail

# Create new Laravel project with Sail curl -s "https://laravel.build/blog" | bash # Choose services when prompted (MySQL, Redis, etc.) # Navigate into project cd blog # Start Sail containers ./vendor/bin/sail up # Access application at http://localhost

Using Sail Commands

# Start Sail in background ./vendor/bin/sail up -d # Stop Sail ./vendor/bin/sail down # Run artisan commands via Sail ./vendor/bin/sail artisan migrate # Run composer commands ./vendor/bin/sail composer require package/name # Access MySQL shell ./vendor/bin/sail mysql # Create shell alias for convenience (add to ~/.bashrc or ~/.zshrc) alias sail='./vendor/bin/sail' # Now you can use: sail up, sail artisan, etc.
When to Use Sail? Use Laravel Sail if you want a consistent development environment across your team, or if you prefer Docker containers over installing PHP, MySQL, etc. directly on your machine.

Common Configuration Tasks

Setting Up File Permissions

# On Linux/macOS, Laravel needs write permissions to storage and cache chmod -R 775 storage bootstrap/cache # If you encounter permission issues, you may need: sudo chown -R $USER:www-data storage bootstrap/cache

Creating Symbolic Link for Storage

To make uploaded files accessible from the web:

# Create symbolic link from public/storage to storage/app/public php artisan storage:link # This allows you to access files like: # http://yourapp.com/storage/user-avatars/avatar.jpg

Configuring Timezone

# In config/app.php 'timezone' => 'UTC', // Change to your timezone # Common timezones: # 'America/New_York' # 'Europe/London' # 'Asia/Tokyo' # 'Australia/Sydney' # Or set in .env (Laravel 11+) APP_TIMEZONE=America/New_York

Configuring Locale

# In config/app.php 'locale' => 'en', // Default locale 'fallback_locale' => 'en', // Fallback locale

Development Tools & IDE Setup

Recommended IDEs

  • PhpStorm: Full-featured PHP IDE with excellent Laravel support
  • VS Code: Free, lightweight editor with Laravel extensions
  • Sublime Text: Fast editor with Laravel-specific packages

VS Code Extensions for Laravel

# Recommended VS Code extensions: 1. Laravel Extension Pack (by Winnie Lin) 2. Laravel Blade Snippets 3. Laravel Snippets 4. PHP Intelephense 5. Laravel Artisan 6. DotENV 7. Laravel goto view 8. Laravel Extra Intellisense

Laravel IDE Helper

Generate IDE helper files for better autocomplete and intellisense:

# Install IDE Helper package (development only) composer require --dev barryvdh/laravel-ide-helper # Generate helper files php artisan ide-helper:generate php artisan ide-helper:models php artisan ide-helper:meta # Add to .gitignore _ide_helper.php _ide_helper_models.php .phpstorm.meta.php

Practice Exercise 1: Fresh Installation

Complete these tasks to practice Laravel installation:

  1. Create a new Laravel project called "my-blog"
  2. Configure the database connection in .env
  3. Run migrations to create default tables
  4. Start the development server and verify it works
  5. Install NPM dependencies and run the dev server

Practice Exercise 2: Environment Configuration

Modify your .env file:

  1. Change the APP_NAME to "My Awesome Blog"
  2. Set APP_DEBUG to false temporarily and visit the site (then set it back to true)
  3. Change the database from MySQL to SQLite and run migrations
  4. Create a storage symbolic link

Practice Exercise 3: Artisan Exploration

Run these artisan commands and observe the results:

  1. php artisan list - See all available commands
  2. php artisan route:list - View all registered routes
  3. php artisan tinker - Enter interactive shell, try: App\Models\User::count()
  4. php artisan about - View application overview

Troubleshooting Common Issues

Issue: "No application encryption key has been specified"

# Solution: Generate application key php artisan key:generate

Issue: Database connection errors

# Check database credentials in .env # Verify database server is running # Test connection manually: php artisan tinker >>> DB::connection()->getPdo();

Issue: Permission denied errors

# Fix storage permissions chmod -R 775 storage bootstrap/cache sudo chown -R $USER:www-data storage bootstrap/cache

Issue: "Vite manifest not found"

# Solution: Run Vite development server npm run dev # Or build assets npm run build

Summary

In this lesson, you've learned how to:

  • Install Composer and verify system requirements
  • Create a new Laravel project using multiple methods
  • Configure the .env file for database and application settings
  • Start the Laravel development server
  • Install and compile frontend assets with Vite
  • Set up Laravel Sail for Docker-based development
  • Configure common settings like timezone and file permissions
  • Troubleshoot common installation issues

Now that your development environment is set up, you're ready to start building Laravel applications! In the next lesson, we'll dive into routing fundamentals.