Featured image of post How to Set Up a Local Development Environment for Laravel

How to Set Up a Local Development Environment for Laravel

Learn how to set up a local development environment for Laravel with our comprehensive guide. From installation to configuration, we've got you covered with best practices and tools for an efficient Laravel local setup.

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

  1. Download the Composer installer from getcomposer.org.
  2. Run the installer and follow the instructions.
  3. 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:

1
composer global require laravel/installer

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:

1
laravel new my-laravel-app

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:

1
php artisan serve

This starts a local development server at http://localhost:8000.

Using Laravel Valet (macOS)

  1. Install Homebrew if not already installed.
  2. Run:
1
2
composer global require laravel/valet
valet install
  1. Navigate to your project directory and link it with Valet:
1
2
cd my-laravel-app
valet link

Your application will be available at http://my-laravel-app.test.

Using Laravel Homestead

  1. Install Vagrant and VirtualBox.
  2. Add the Laravel Homestead box:
1
vagrant box add laravel/homestead
  1. Clone the Homestead repository and set it up:
1
2
3
git clone https://github.com/laravel/homestead.git Homestead
cd Homestead
bash init.sh
  1. Configure Homestead.yaml to point to your project directory and start Homestead:
1
vagrant up

Your application will be accessible at http://homestead.test.

Using Docker

  1. Install Docker.
  2. In your Laravel project directory, run:
1
2
composer require laravel/sail --dev
php artisan sail:install
  1. Start the Docker containers:
1
./vendor/bin/sail up

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:

1
2
3
4
5
6
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database
DB_USERNAME=your_username
DB_PASSWORD=your_password

Run migrations to create the necessary tables:

1
php artisan migrate

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.