Skip to Content

How to Setup A Laravel Development Environment

Laravel is a fantastic and versatile PHP framework for web development, celebrated for its cool features and ease of use. Setting up a Laravel environment is easy, from installing the essential software to configuring your workspace. This guide will be your trusty companion, making the process a breeze!

Ad - Web Hosting from SiteGround - Crafted for easy site management. Click to learn more.
Advertising Disclosure: I am compensated for purchases made through affiliate links. Click here for details.

Prerequisites

Before setting up Laravel, ensure you have the following installed on your system:

  • Composer: A dependency manager for PHP.
  • PHP: Laravel requires PHP 8.0 or higher.
  • Web Server: Apache or Nginx.
  • Database: MySQL, MariaDB, PostgreSQL, SQLite, or SQL Server.

Step 1: Install Composer

Composer is a dependency manager for PHP that Laravel relies on. To install Composer, follow these steps:

# composer --version

Step 2: Install Laravel

Now, you can install Laravel globally or create a new Laravel project directly with the following command:

# composer global require laravel/installer

Make sure the Composer global bin directory is added to your system's PATH. On Unix-based and Mac systems, you can add the following line to your ~/.bashrc, ~/.bash_profile, or ~/.zshrc file:

export PATH="$HOME/.composer/vendor/bin:$PATH"

For Windows, add %USERPROFILE%\AppData\Roaming\Composer\vendor\bin to your system's PATH environment variable.

Step 3: Create A New Laravel Project

To create a new Laravel project, navigate to a directory of your choosing and run the following command:

# laravel new project-name

Or you can use Composer to create a new project:

# composer create-project --prefer-dist laravel/laravel project-name

Step 4: Configure Your Web Serve

Laravel includes a built-in development server, but for a more production-like environment, configure Apache or Nginx.

Apache Configuration

  • 1. Enable the mod_rewrite module.
  • 2. Create a virtual host configuration file for your Laravel project:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /path/to/laravel/public

<Directory /path/to/laravel/public>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
  • 3. Update your /etc/hosts file to map example.com to 127.0.0.1:
127.0.0.1 example.com

Nginx Configuration

  • 1. Create a server block configuration file for your Laravel project:
server {
listen 80;
server_name example.com;
root /path/to/laravel/public;

index index.php index.html;

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location ~ /\.ht {
deny all;
}
}
  • 2. Update your /etc/hosts file to map example.com to 127.0.0.1:
127.0.0.1 example.com

Step 5: Configure Your Database

  • 1. Create a new database for your Laravel application.
  • 2. Update the .env file in the root of your Laravel project with your database credentials:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=[database]
DB_USERNAME=[username]
DB_PASSWORD=[password]
Make sure to change the database name, username, and password to the correct values.

Step 6: Run Migrations

Laravel uses migrations, or version control for your database schema, to manage database changes. To run the default migrations and set up your database schema, run the following command:

# php artisan migrate

Step 7: Start the Development Server

To launch the built-in development server, go to your Laravel project directory and execute the following command:

# php artisan serve

The development server runs at http://localhost:8000 by default.

Step 8: Configure Environment Variables

Laravel uses an .env file to manage environment variables. Ensure you configure the following settings in your .env file:

  • APP_ENV: Set to local for development.
  • APP_DEBUG: Set to true for development.
  • APP_URL: Set to the URL of your application (e.g., http://localhost:8000).

Step 9: Install Frontend Dependencies (Optional)

If your Laravel application uses frontend dependencies like Vue.js, React, or Bootstrap, you'll need Node.js and npm (Node Package Manager) installed. To install Node.js and npm:

# node --version
# npm --version

To install frontend dependencies, navigate to your Laravel project directory and run:

# npm install

You can then compile your assets using Laravel Mix, a free and open-source tool that simplifies defining webpack build steps for Laravel applications:

# npm run dev

Step 10: Additional Configuration (Optional)

Depending on your project requirements, you may need additional configuration:

  • Mail Configuration: Update the mail settings in your .env file.
  • Caching: Configure cache settings in config/cache.php.
  • Queueing: Configure queue settings in config/queue.php.
  • Logging: Configure logging settings in config/logging.php.

Conclusion

And there you have it! Here, you learned how to set up a Laravel development environment by installing Composer, creating a web server, configuring your database, and running migrations. As you get more comfortable, you’ll discover advanced features like service providers, middleware, and custom commands. Happy coding!

Posted by: Josh Rowe
Created: May 24, 2024

Comments

There are no comments yet. Start the conversation!

Add A Comment

Comment Etiquette: Wrap code in a <code> and </code>. Please keep comments on-topic, do not post spam, keep the conversation constructive, and be nice to each other.