Laravel

Laravel is a powerful and popular PHP framework that simplifies and accelerates web application development. Its elegant syntax, robust features like Eloquent ORM, Blade templating, and built-in security tools help developers create efficient and scalable apps. With strong community support and extensive documentation, Laravel is an ideal choice for both novice and experienced developers

Laravel Installation and Setup

30 April 2025 | Category:

Setting up Laravel is straightforward, thanks to its well-documented process and developer-friendly tools. This guide provides a plagiarism-free, detailed walkthrough for installing and configuring Laravel on your system, ensuring you’re ready to start building modern web applications. Whether you’re using Windows, macOS, or Linux, we’ll cover the essentials, including prerequisites, installation methods, and initial setup, tailored for beginners and seasoned developers alike.


Prerequisites

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

  • PHP: Version 8.1 or higher (Laravel 10, as of 2025, requires PHP 8.1+).
  • Composer: PHP’s dependency manager for installing Laravel and its packages.
  • Web Server: Apache or Nginx (or a local development server like Laravel Sail or Valet).
  • Database: MySQL, PostgreSQL, SQLite, or SQL Server (optional, depending on your project).
  • Extensions: PHP extensions like mbstring, xml, curl, zip, bcmath, ctype, json, tokenizer, and openssl.
  • Node.js and NPM (optional): For front-end asset compilation (e.g., Laravel Breeze or Vite).
  • Git: For version control and cloning Laravel repositories.

Ensure you have a code editor (e.g., VS Code, PhpStorm) and a terminal for running commands.


Step 1: Install PHP and Required Extensions

  1. Download and Install PHP:
    • Windows: Download PHP from php.net or use a package manager like XAMPP/WAMP.
    • macOS: Install via Homebrew: brew install php@8.1.
    • Linux: Use your package manager, e.g., sudo apt install php8.1 php8.1-cli php8.1-fpm (Ubuntu).
  2. Verify PHP Installation:
    Run php -v in your terminal. You should see the PHP version (e.g., PHP 8.1.x).
  3. Install Required Extensions:
    • On Ubuntu: sudo apt install php8.1-mbstring php8.1-xml php8.1-curl php8.1-zip php8.1-bcmath.
    • On macOS: Most extensions are included with Homebrew’s PHP.
    • On Windows: Enable extensions by uncommenting lines (e.g., extension=mbstring) in php.ini.
  4. Check Extensions:
    Run php -m to confirm all required extensions are loaded.

Step 2: Install Composer

Composer is essential for managing Laravel’s dependencies.

  1. Download Composer:
    • Visit getcomposer.org and follow the instructions.
    • Windows: Run the Composer installer.
    • macOS/Linux: Use the command:php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php composer-setup.php php -r "unlink('composer-setup.php');" sudo mv composer.phar /usr/local/bin/composer
  2. Verify Installation:
    Run composer --version. You should see the Composer version (e.g., Composer 2.x.x).

Step 3: Install Laravel

Laravel can be installed globally or per project. We’ll cover the per-project method using Composer, as it’s the most common approach.

  1. Create a New Laravel Project:
    Open your terminal, navigate to your desired directory (e.g., cd ~/Sites), and run: composer create-project laravel/laravel my-laravel-app Replace my-laravel-app with your project name. This command downloads Laravel and its dependencies.
  2. Alternative: Laravel Installer (Optional):
    • Install the Laravel Installer globally:composer global require laravel/installer
    • Create a project:laravel new my-laravel-app
    • Ensure Composer’s global bin is in your PATH (e.g., ~/.composer/vendor/bin).
  3. Navigate to Project Directory: cd my-laravel-app

Step 4: Configure the Environment

Laravel uses a .env file to manage environment-specific settings (e.g., database, app key).

  1. Copy the Example Environment File:
    Laravel includes a .env.example file. Create a .env file: cp .env.example .env
  2. Generate Application Key:
    Run the following Artisan command to generate a unique app key: php artisan key:generate This updates the APP_KEY in .env.
  3. Edit .env File (Optional):
    Open .env in your editor and configure settings like:
    • APP_NAME: Your application name.
    • APP_URL: Your app’s URL (e.g., http://localhost:8000).
    • DB_CONNECTION: Database type (e.g., mysql, sqlite).
    • DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, DB_PASSWORD: Database credentials.
    Example for MySQL: DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_db DB_USERNAME=root DB_PASSWORD=your_password

Step 5: Set Up a Local Development Server

Laravel includes a built-in server for development, but you can also use Apache, Nginx, or Docker-based solutions like Laravel Sail.

  1. Using Laravel’s Artisan Server:
    Run: php artisan serve This starts a server at http://localhost:8000. Visit this URL in your browser to see Laravel’s welcome page.
  2. Using Laravel Sail (Docker):
    • Ensure Docker and Docker Compose are installed.
    • Laravel Sail is included in new projects. Publish Sail’s configuration:php artisan sail:install
    • Start Sail:./vendor/bin/sail up
    • Access your app at http://localhost.
  3. Using Valet (macOS):
    • Install Laravel Valet via Composer: composer global require laravel/valet.
    • Run valet install and link your project: valet link my-laravel-app.
    • Access at http://my-laravel-app.test.
  4. Apache/Nginx:
    • Point your server’s document root to my-laravel-app/public.
    • Configure a virtual host to handle requests (refer to Laravel’s documentation for server-specific configs).

Step 6: Verify Installation

  • Open your browser and visit http://localhost:8000 (or your configured URL).
  • You should see Laravel’s default welcome page, confirming a successful setup.
  • If you encounter errors, check:
    • PHP version and extensions (php -v, php -m).
    • Permissions: Ensure storage and bootstrap/cache directories are writable (chmod -R 775 storage bootstrap/cache).
    • .env configuration: Verify the app key and database settings.

Step 7: Optional Setup for Front-End Development

Laravel uses Vite for asset compilation (e.g., CSS, JavaScript).

  1. Install Node.js and NPM:
    • Download from nodejs.org or use a package manager:
      • Ubuntu: sudo apt install nodejs npm.
      • macOS: brew install node.
  2. Install Dependencies:
    Run: npm install
  3. Run Vite Development Server: npm run dev This compiles assets and enables hot-module replacement.
  4. Build for Production (later, when deploying): npm run build

Step 8: Database Setup (Optional)

If your project uses a database:

  1. Create a Database:
    • For MySQL: mysql -u root -p, then CREATE DATABASE laravel_db;.
    • For SQLite: Create a file (e.g., touch database/database.sqlite).
  2. Run Migrations:
    Laravel includes default migrations. Run: php artisan migrate This creates tables defined in database/migrations.

Troubleshooting Common Issues

  • Composer Errors: Ensure Composer is updated (composer self-update) and PHP meets requirements.
  • Permission Issues: Run chmod -R 775 storage bootstrap/cache and chown -R www-data:www-data . (for Apache/Nginx).
  • Database Connection Errors: Verify .env credentials and ensure the database server is running.
  • Port Conflicts: If php artisan serve fails, specify a different port: php artisan serve --port=8001.

What’s Next?

With Laravel installed, you’re ready to explore its features:

  • Create routes in routes/web.php.
  • Build views using Blade in resources/views.
  • Define models and relationships with Eloquent in app/Models.
  • Use Artisan for scaffolding: php artisan make:controller, php artisan make:model, etc.

This setup provides a solid foundation for your Laravel projects. Dive into our tutorial series to learn routing, controllers, Blade templating, and more, as you build dynamic, scalable applications!