How to Install Laravel Using Composer (Complete Guide)

Laravel is one of the most popular PHP frameworks for web development. This comprehensive guide will walk you through installing Laravel using Composer, the dependency manager for PHP.

What You’ll Need Before Getting Started

Before installing Laravel, ensure your system meets these requirements:

  • PHP 8.1 or higher
  • Composer installed globally
  • A web server (Apache, Nginx, or PHP’s built-in server)
  • MySQL, PostgreSQL, SQLite, or SQL Server database

Step 1: Install PHP and Required Extensions

Windows Users

  1. Download PHP from php.net
  2. Extract to C:\php
  3. Add C:\php to your system PATH
  4. Enable required extensions in php.ini:
extension=openssl
extension=pdo_mysql
extension=mbstring
extension=tokenizer
extension=xml
extension=ctype
extension=json
extension=bcmath
extension=fileinfo

Mac Users (using Homebrew)

brew install php
brew install composer

Linux Users (Ubuntu/Debian)

sudo apt update
sudo apt install php php-cli php-fpm php-mysql php-xml php-zip php-curl php-mbstring php-tokenizer php-bcmath php-fileinfo

Step 2: Install Composer

Windows

  1. Download Composer-Setup.exe from getcomposer.org
  2. Run the installer
  3. Follow the setup wizard

Mac/Linux

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer

Verify Composer Installation

composer --version

Step 3: Install Laravel Using Composer

Method 1: Using Composer Create-Project (Recommended)

composer create-project laravel/laravel my-laravel-app

This command will:

  • Create a new directory called “my-laravel-app”
  • Download Laravel and all dependencies
  • Set up the basic project structure

Method 2: Using Laravel Installer

First, install the Laravel installer globally:

composer global require laravel/installer

Add Composer’s global bin directory to your PATH:

Windows:

set PATH=%PATH%;%APPDATA%\Composer\vendor\bin

Mac/Linux:

export PATH="$PATH:$HOME/.composer/vendor/bin"

Create a new Laravel project:

laravel new my-laravel-app

Step 4: Navigate to Your Project Directory

cd my-laravel-app

Step 5: Set Up Environment Configuration

Copy Environment File

cp .env.example .env

Generate Application Key

php artisan key:generate

Configure Database Settings

Edit the .env file and update these database settings:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password

Step 6: Set Directory Permissions

Linux/Mac Users

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

Step 7: Run Your Laravel Application

Using PHP Built-in Server

php artisan serve

Your application will be available at http://localhost:8000

Using Valet (Mac Users)

composer global require laravel/valet
valet install
valet park

Using Homestead (All Platforms)

Add to Homestead.yaml:

sites:
    - map: myapp.test
      to: /home/vagrant/code/my-laravel-app/public

Common Installation Issues and Solutions

Issue 1: Composer Memory Limit

Problem: “Fatal error: Allowed memory size exhausted”

Solution:

php -d memory_limit=-1 /usr/local/bin/composer create-project laravel/laravel my-app

Issue 2: Permission Denied Errors

Problem: Cannot write to storage or cache directories

Solution:

sudo chmod -R 777 storage bootstrap/cache

Issue 3: Missing PHP Extensions

Problem: “Extension not found” errors

Solution: Install missing extensions:

# Ubuntu/Debian
sudo apt install php-ext-name

# CentOS/RHEL
sudo yum install php-ext-name

Verify Your Laravel Installation

Check Laravel Version

php artisan --version

Run Basic Commands

  1. List available Artisan commands:
php artisan list
  1. Create a controller:
php artisan make:controller HomeController
  1. Run migrations:
php artisan migrate

Laravel Project Structure Overview

Your new Laravel project includes these key directories:

  • app/ – Core application code
  • bootstrap/ – Framework bootstrap files
  • config/ – Configuration files
  • database/ – Database migrations and seeds
  • public/ – Web server document root
  • resources/ – Views, raw assets, language files
  • routes/ – Route definitions
  • storage/ – Logs, compiled templates, sessions
  • vendor/ – Composer dependencies

Next Steps After Installation

  1. Set up your database:
php artisan migrate
  1. Create your first route in routes/web.php:
Route::get('/hello', function () {
    return 'Hello, Laravel!';
});
  1. Generate authentication scaffolding:
composer require laravel/ui
php artisan ui bootstrap --auth
npm install && npm run dev

Performance Optimization Tips

Enable Caching in Production

php artisan config:cache
php artisan route:cache
php artisan view:cache

Optimize Composer Autoloader

composer install --optimize-autoloader --no-dev

Troubleshooting Checklist

If you encounter issues:

  • [ ] Verify PHP version (8.1+)
  • [ ] Check all required extensions are installed
  • [ ] Ensure proper directory permissions
  • [ ] Confirm database connection settings
  • [ ] Verify Composer is updated
  • [ ] Clear application cache: php artisan cache:clear

Conclusion

You’ve successfully installed Laravel using Composer! Your development environment is ready for building modern web applications. The Laravel framework provides excellent documentation and a vibrant community to help you on your development journey.

Start exploring Laravel’s features like Eloquent ORM, Blade templating, and Artisan CLI to build powerful web applications efficiently.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top