Skip to main content

Installation Script

Creating an installation script for your Laravel application can automate tedious setup tasks like environment configuration, dependency installation, and database migration. Depending on your needs, you can create a simple bash script (for Linux/macOS) or a custom Artisan command (to distribute with your application). 

Method 1: Bash Installation Script (Linux/macOS)
A bash script is ideal for setting up a freshly cloned repository on a new server or local environment. 
  1. Create the file: In your project root, create a file named install.sh.
  2. Add the script logic:
    bash
    #!/bin/bash
    # install.sh
    
    echo "Starting Laravel Installation..."
    
    # 1. Install PHP dependencies
    composer install --no-interaction --prefer-dist --optimize-autoloader
    
    # 2. Setup Environment File
    if [ ! -f .env ]; then
        cp .env.example .env
        echo ".env file created from .env.example"
    fi
    
    # 3. Generate Application Key
    php artisan key:generate
    
    # 4. Install Frontend Dependencies
    npm install
    npm run build
    
    # 5. Run Database Migrations
    php artisan migrate --force
    
    # 6. Set Permissions
    chmod -R 775 storage bootstrap/cache
    chown -R www-data:www-data storage bootstrap/cache
    
    echo "Installation Complete!"
    
  3. Make it executable: Run chmod +x install.sh in your terminal.
  4. Run it: Execute with ./install.sh. 

Method 2: Custom Artisan Command (Recommended for Distribution)
If you are building a product for others, a custom Artisan command like php artisan app:install provides a more integrated experience. 
  1. Generate the command:
    bash
    php artisan make:command InstallApplication
    
  2. Configure the command: Open app/Console/Commands/InstallApplication.php and define the logic:
    • Signature: protected $signature = 'app:install';
    • Logic: Use Artisan::call() to run setup tasks.
    php
    public function handle()
    {
        $this->info('Installing Application...');
    
        $this->call('key:generate');
        $this->call('migrate', ['--force' => true]);
        $this->call('db:seed');
        $this->call('storage:link');
    
        $this->info('Application installed successfully!');
    }
    
     

Key Components of a Laravel Installer
To ensure your script is robust, it should handle the following: 
  • Environment Validation: Check if the system meets Laravel System Requirements (e.g., PHP version, required extensions like mbstring or openssl).
  • Dependency Management: Ensure composer install and npm install are executed.
  • Storage Permissions: The storage and bootstrap/cache directories must be writable by the web server.
  • Database Setup: Automate php artisan migrate --seed to prepare the schema and initial data. 

Advanced Options
  • Web-Based Installers: For non-technical users, you can use packages like LaravelInstaller which provide a WordPress-like wizard
Advanced Options
  • Web-Based Installers: For non-technical users, you can use packages like LaravelInstaller which provide a WordPress-like wizard in the browser.
  • Automated Hosting Templates: Some providers like Hostinger offer one-click templates that automate the entire OS and Laravel stack setup.
  • Laravel Herd: For local development on macOS or Windows, Laravel Herd provides a pre-configured environment that eliminates the need for manual installation scripts.