Setting up Laravel involves installing its dependencies, configuring the environment, and ensuring everything is ready for development. Here’s a step-by-step guide:
1. System Requirements
Before installing Laravel, ensure your system meets the following requirements:
- PHP: 8.1 or later
- Composer: Dependency manager for PHP
- Web Server: Apache, Nginx, or Laravel’s built-in development server
- Database: MySQL, PostgreSQL, SQLite, or SQL Server
2. Install Laravel
Step 1: Install Composer
- Download Composer from getcomposer.org.
- Verify installation:
composer --version
Step 2: Install Laravel
You can install Laravel in one of two ways:
Option A: Using Laravel Installer
- Install Laravel globally:
composer global require laravel/installer
2. Create a new Laravel project:
laravel new project_name
3. Navigate to your project directory:
cd project_name
Option B: Using Composer Create-Project
- Create a new Laravel project:
composer create-project --prefer-dist laravel/laravel project_name
2. Navigate to your project directory:
cd project_name
3. Set Up the Environment
Step 1: Configure .env
- Open the
.env
file in the root of your project. - Update database details:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password
Step 2: Generate Application Key
Run the following command to set the application key:
php artisan key:generate
4. Set Up a Web Server
Option A: Laravel Development Server
Run the built-in server:
php artisan serve
- Access your app at:
http://localhost:8000
.
Option B: Using XAMPP/WAMP
- Place your Laravel project in the
htdocs
folder (for XAMPP) or the appropriate folder for WAMP. - Update the Apache configuration to point to the
public
folder of your Laravel project.
Option C: Using Nginx
- Configure your Nginx virtual host to point to the
public
folder. - Example Nginx configuration:
server {
listen 80;
server_name yourdomain.com;
root /path-to-your-project/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
5. Migrate Database
If your Laravel project uses a database, migrate the database tables:
php artisan migrate
6. Install Additional Dependencies
If your project requires specific libraries, install them using Composer:
composer require package_name
7. Test the Application
Visit your Laravel application in a browser to verify everything is working:
- Local:
http://localhost:8000
- Server: Your domain or IP address.
8. Optional: Frontend Assets
If your project uses frontend tools like Vite, run:
npm install
npm run dev
For production builds:
npm run build
Would you like help configuring a specific feature, such as authentication, routing, or API setup?