Laravel is a powerful PHP framework that simplifies web development with its elegant syntax and robust features. To leverage its full potential, it’s essential to set up a local development environment for Laravel. Follow these steps to get started on your computer.
Prerequisites
Ensure you have the following installed on your computer:
- PHP: Laravel requires PHP 7.3 or higher.
- Composer: A dependency manager for PHP.
Step 1: Install Composer
- Download the Composer installer from getcomposer.org.
- Run the installer and follow the instructions.
- Verify the installation by running
composer --version
in your terminal.
Step 2: Install Laravel
Once Composer is installed, run the following command to install Laravel globally:
|
|
Add the Composer bin directory to your system’s PATH to run the laravel
command from anywhere in your terminal.
Step 3: Create a New Laravel Project
Create a new Laravel project by running:
|
|
Replace my-laravel-app
with your desired project name. This creates a new Laravel project in the my-laravel-app
directory.
Set Up a Local Development Server
Using PHP’s Built-in Server
Navigate to your project directory and run:
|
|
This starts a local development server at http://localhost:8000
.
Using Laravel Valet (macOS)
- Install Homebrew if not already installed.
- Run:
|
|
- Navigate to your project directory and link it with Valet:
|
|
Your application will be available at http://my-laravel-app.test
.
Using Laravel Homestead
- Install Vagrant and VirtualBox.
- Add the Laravel Homestead box:
|
|
- Clone the Homestead repository and set it up:
|
|
- Configure
Homestead.yaml
to point to your project directory and start Homestead:
|
|
Your application will be accessible at http://homestead.test
.
Using Docker
- Install Docker.
- In your Laravel project directory, run:
|
|
- Start the Docker containers:
|
|
Your application will be available at http://localhost
.
Configure Your Laravel Application
Environment Configuration
Update the .env
file with your local environment settings, such as database credentials and application key.
Database Setup
Configure your database settings in the .env
file:
|
|
Run migrations to create the necessary tables:
|
|
Setting up a local development environment for Laravel is straightforward. By installing the necessary tools and configuring your environment properly, you’ll be able to develop and test your Laravel applications efficiently. Whether using PHP’s built-in server, Valet, Homestead, or Docker, each method provides a robust solution for local development.