How to Install Laravel in XAMPP: A Step-by-Step Guide

  • Nepsyscode


Laravel is one of the most popular PHP frameworks used for building robust web applications. It simplifies complex tasks like routing, authentication, and database management. To start developing Laravel projects locally, you can use XAMPP, a free and open-source cross-platform web server solution. In this guide, we’ll walk you through the steps to install Laravel on your XAMPP setup.

Prerequisites:

  • XAMPP Installed: Make sure XAMPP is installed and running on your system. You can download it from here.
  • Composer Installed: Composer is required to install Laravel. Download and install it from here.

Step 1: Start XAMPP

First, make sure that Apache and MySQL services are running in XAMPP.

  1. Open XAMPP Control Panel.
  2. Click Start next to Apache and MySQL to launch them.

Step 2: Install Composer

Composer is the tool used to manage Laravel dependencies. You need to install it globally to download Laravel.

  1. Go to the official Composer website and download the Composer setup file.
  2. Follow the installation wizard, ensuring that you select the php.exe path from your XAMPP installation (e.g., C:\xampp\php\php.exe) during setup.
  3. Once installed, open a terminal (CMD) and type composer to verify the installation. You should see the Composer version output.

Step 3: Set Up Laravel Project in XAMPP’s htdocs Folder

Now, we will create a Laravel project inside XAMPP’s htdocs folder, where all your web files are stored.

  1. Navigate to your XAMPP htdocs folder (e.g., C:\xampp\htdocs).
  2. Open a terminal (CMD) in the htdocs folder by holding Shift + Right-click inside the folder and choosing Open Command Window Here.
  3. Run the following command to create a new Laravel project:
    composer create-project --prefer-dist laravel/laravel your_project_name

    Replace your_project_name with the desired name of your Laravel project. Composer will download and set up Laravel in a new directory.

Step 4: Configure Environment File

After the Laravel installation completes, you need to configure the environment file to ensure Laravel connects to the database correctly.

  1. Navigate to the newly created Laravel project folder inside htdocs.
  2. Find the .env file and open it in a text editor.
  3. Modify the following lines to match your XAMPP database setup:
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=your_database_name
    DB_USERNAME=root
    DB_PASSWORD=
    • DB_DATABASE: Create a database in phpMyAdmin and enter its name here.
    • DB_USERNAME: Usually root in XAMPP.
    • DB_PASSWORD: Leave this empty if your MySQL doesn’t have a password (which is the default in XAMPP).

Step 5: Set Up Virtual Host (Optional)

Instead of accessing your Laravel project through a URL like localhost/your_project_name/public, you can set up a virtual host for a cleaner URL like http://your-project.local.

  1. Open C:\xampp\apache\conf\extra\httpd-vhosts.conf and add the following lines at the end:
    <VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot "C:/xampp/htdocs/your_project_name/public"
    ServerName your-project.local
    <Directory "C:/xampp/htdocs/your_project_name">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
    </Directory>
    </VirtualHost>
  2. Open C:\Windows\System32\drivers\etc\hosts in a text editor (with administrator privileges) and add this line at the end:
    127.0.0.1 your-project.local
  3. Restart Apache from the XAMPP Control Panel.

You can now access your Laravel project by typing http://your-project.local in your browser.

Step 6: Run the Laravel Application

If you don’t want to set up a virtual host, you can still access your Laravel app via localhost.

  1. Open a terminal in the Laravel project folder.
  2. Run the following command to start the built-in Laravel development server:
    php artisan serve
  3. Open your browser and go to:
    http://localhost:8000

    Your Laravel application should now be running and accessible.

Step 7: Access Your Laravel Application

You can now access your Laravel app either through the virtual host (http://your-project.local) or by using the built-in server (http://localhost:8000).

Conclusion

By following these steps, you’ve successfully installed Laravel in XAMPP. Whether you’re a beginner or an experienced developer, Laravel’s elegant syntax and comprehensive features make it easier to build powerful web applications. With XAMPP, you can now work on Laravel projects locally and start creating your next application with ease.