Installing Laravel with XAMPP
This guide provides a detailed, step-by-step approach to installing Laravel, one of the most popular PHP frameworks, using XAMPP (Apache, MySQL, and PHP). This setup allows you to develop and test your Laravel applications in a local environment.
Prerequisites
Before you begin, ensure you have the following installed on your machine:
- XAMPP: A free and open-source cross-platform web server solution stack package that includes Apache HTTP Server, MariaDB (a fork of MySQL), and interpreters for scripts written in the PHP and Perl programming languages.
- Composer: A dependency manager for PHP essential for managing Laravel's libraries and dependencies.
Step 1: Download and Install XAMPP
Download XAMPP:
- Go to the XAMPP official website.
- Choose the version compatible with your operating system (Windows, macOS, or Linux) and download the installer.
Install XAMPP:
- Run the installer by double-clicking on it.
- If prompted by a security warning, click "Run."
- In the XAMPP setup wizard, click "Next."
- Select the components to install. Ensure Apache and MySQL are checked. You can also include PHP and phpMyAdmin if desired.
- Click "Next."
- Choose the installation directory (default is usually
C:\xampp
). Click "Next." - Select your preferred language (English is default). Click "Next."
- Click "Next" again to start the installation process.
- Once the installation is complete, you can choose to start the XAMPP Control Panel immediately. Click "Finish."
Start XAMPP:
- Open the XAMPP Control Panel from your desktop or start menu.
- In the control panel, you will see a list of modules. Click the "Start" button next to Apache and MySQL. Ensure both services are running (indicated by green highlights).
Step 2: Install Composer
Composer is essential for managing Laravel's dependencies. Here’s how to install it:
Download Composer:
- Visit the Composer official website.
- Scroll down to the Windows Installer section and download the Composer-Setup.exe file.
Install Composer:
- Run the downloaded
Composer-Setup.exe
file. - Follow the installation prompts. When asked to set up the PHP path, browse to your XAMPP installation directory and select
php.exe
, typically found inC:\xampp\php\php.exe
. - Complete the installation process.
- Run the downloaded
Verify Composer Installation:
- Open the Command Prompt by pressing
Win + R
, typingcmd
, and hittingEnter
. - Type the following command to check if Composer was installed correctly:
- If installed successfully, you will see the Composer version displayed.
- Open the Command Prompt by pressing
Step 3: Create a New Laravel Project
Now that Composer is installed, you can create a new Laravel project.
Open Command Prompt:
- Press
Win + R
, typecmd
, and hitEnter
to open the Command Prompt.
- Press
Navigate to the XAMPP
htdocs
Directory:- Type the following command to change the directory to the
htdocs
folder where XAMPP serves files:
- Type the following command to change the directory to the
Create a New Laravel Project:
- Use Composer to create a new Laravel project (replace
myproject
with your desired project name): - This command will download and install a new Laravel application in a folder named
myproject
.
- Use Composer to create a new Laravel project (replace
Step 4: Configure Environment
Set Up Environment Variables:
- Change to your project directory:
- Copy the example environment file to create a new
.env
file:
- Change to your project directory:
Generate Application Key:
- Run the following command to generate an application key:
- This key is essential for securing user sessions and other encrypted data.
- Run the following command to generate an application key:
Configure Database:
- Open the
.env
file in a text editor (like Notepad or Visual Studio Code). - Find the database configuration section and update it to match your database settings:
- Note: Before proceeding, create a new database in phpMyAdmin:
- Open your web browser and go to
http://localhost/phpmyadmin
. - Click on Databases at the top.
- Enter the name of your database (e.g.,
mydatabase
) and click Create.
- Open your web browser and go to
- Open the
Step 5: Run the Laravel Development Server
- Start Laravel Development Server:
- In the Command Prompt, while still in your Laravel project directory, run:
- This will start the Laravel development server. You will see a message indicating that the server is running, typically at
http://localhost:8000
.
- In the Command Prompt, while still in your Laravel project directory, run:
Step 6: Access Your Laravel Application
- Open your web browser and navigate to
http://localhost:8000
to view your Laravel application. You should see the Laravel welcome page, indicating that everything is set up correctly.
Step 7: Additional Configuration (Optional)
If you want to access your Laravel project via XAMPP’s localhost (e.g., http://localhost/myproject
), you can set up a virtual host.
Open Apache Configuration File:
- In XAMPP Control Panel, click on the Config button next to Apache and select httpd-vhosts.conf.
- Add the following lines to define your virtual host (adjust paths accordingly):
Edit Hosts File:
- Open the hosts file as an administrator. Navigate to
C:\Windows\System32\drivers\etc\hosts
. - Add the following line to map the virtual host:
- Open the hosts file as an administrator. Navigate to
Restart Apache:
- Go back to the XAMPP Control Panel and restart Apache for the changes to take effect.
Step 8: Access Your Laravel Application via Virtual Host
- Open your web browser and type
http://myproject.local
to access your Laravel application through the virtual host.
Troubleshooting Tips
Apache Not Starting:
- If Apache doesn’t start, check if another application (like Skype) is using port 80. You can change Apache’s port in the XAMPP Control Panel by clicking the Config button next to Apache and selecting httpd.conf. Look for the line
Listen 80
and change it toListen 8080
. Then, access your site athttp://localhost:8080
.
- If Apache doesn’t start, check if another application (like Skype) is using port 80. You can change Apache’s port in the XAMPP Control Panel by clicking the Config button next to Apache and selecting httpd.conf. Look for the line
Database Connection Issues:
- If you encounter issues connecting to the database, double-check your
.env
file for accuracy and ensure the database exists in phpMyAdmin.
- If you encounter issues connecting to the database, double-check your
Permission Issues (Linux/Mac):
- If you’re on Linux or macOS, you might encounter permission issues. You can resolve this by running:
Conclusion
You have successfully installed Laravel using XAMPP! You can now start building your web applications with Laravel in a local development environment. This setup provides a powerful framework for developing robust and scalable web applications. If you have any further questions or encounter any issues, please ask for assistance!