Introduction
Laravel remains one of the most popular PHP frameworks for developing web applications.This tutorial guides you through the steps to create a Laravel project in your current directory, delving into basic and advanced concepts alike, with practical examples.
Prerequisites
Before you start, ensure that you have the following installed on your system:
- PHP (version 7.3 or greater)
- Composer (PHP package manager)
- Node.js and NPM (Optional, but recommended for asset compilation)
Step-by-Step Instructions
Step 1: Installing Laravel Via Composer
One of the most common methods to install Laravel is through Composer, PHP’s package manager.
composer create-project --prefer-dist laravel/laravel .
This command tells Composer to create a project using the laravel/laravel
package, and the period .
at the end specifies that it should be installed into the current directory.
After running the command, Composer will download all necessary files and dependencies. Once complete, you will have a fresh Laravel installation in your current directory.
Step 2: Setting Up the Environment File
After the installation, you need to set up the environment configuration. A .env
file should already exist. In the command line, you can copy the example file using:
cp .env.example .env
Make sure to configure the .env
file to match your local environment settings, including your database connection if applicable:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database
DB_USERNAME=your_username
DB_PASSWORD=your_password
All sensitive information should be stored in the .env
file, as it is not included in version control.
Step 3: Generating the Application Key
Laravel requires an application key for session and cookie encryption.
php artisan key:generate
Running this command will generate a key and automatically update your .env
file with an APP_KEY
field.
Step 4: Directory Permissions
Directories within the storage
and bootstrap/cache
directories should be writable by your web server or Laravel will not run. If permissions are incorrect, you can change them using:
sudo chown -R $USER:www-data storage
sudo chown -R $USER:www-data bootstrap/cache
Ensure to replace $USER
with your actual username. Additionally, the www-data
group may vary based on your web server configuration.
Step 5: Running the Development Server
To start the Laravel development server, run:
php artisan serve
This command will start a server at http://localhost:8000
. You can now visit this URL in a web browser to see your brand new Laravel application.
Advanced Setup
If you want further control of the installation, Laravel allows additional commands to tailor the setup process.
To create a project with a specific version of Laravel:
composer create-project --prefer-dist laravel/laravel . "10.0.*"
Replace “5.8.*” with the desired version. This command is particularly useful when working on a project that requires a specific Laravel version.
Consider using Laravel’s Valet (for macOS) or Laravel Homestead (a Vagrant box) for a more robust development environment. These provide a complete, per-project setup that can mimic production environments more closely.
Migrating and Seeding the Database
Lastly, you’ll likely want to run your migrations to set up your initial database schema:
php artisan migrate
If you have seed data to populate your database with, you can do so with:
php artisan db:seed
For a combination of both, Laravel provides:
php artisan migrate --seed
This will migrate your databases and seed them in a single command.
Conclusion
Congratulations! You have successfully created a Laravel project right in your current directory. Laravel’s capabilities extend far beyond these initial setup steps, but you are now ready to start developing your web application. Embrace the elegant syntax and features of Laravel, and build something remarkable!