How to Install Laravel with Composer: A Beginner-Friendly Tutorial
Here’s a step-by-step guide to installing Laravel using Composer:
1. Ensure Composer is Installed
First, verify that Composer is installed on your system. If not, follow these steps:
Install Composer:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php composer-setup.php php -r "unlink('composer-setup.php');" sudo mv composer.phar /usr/local/bin/composer
Check Composer Installation:
composer --version
2. Create a New Laravel Project
Run the following command to create a new Laravel project:
composer create-project --prefer-dist laravel/laravel my-laravel-app
This will create a directory named my-laravel-app
containing your Laravel application.
3. Navigate to the Project Directory
Move into your newly created Laravel project folder:
<span class="hljs-built_in">cd</span> my-laravel-app
4. Set Permissions
Make sure the storage
and bootstrap/cache
directories are writable:
<span class="hljs-built_in">chmod</span> -R 775 storage bootstrap/cache
5. Configure the .env
File
Copy the example .env
file and set up your application environment variables:
<span class="hljs-built_in">cp</span> .env.example .<span class="hljs-built_in">env</span>
Generate an application key:
php artisan key:generate
6. Run the Development Server
Start Laravel’s built-in development server:
php artisan serve
The application will be accessible at:
http://localhost:8000/
7. Set Up the Database
If your application requires a database, configure it in the .env
file:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=my_database DB_USERNAME=my_user DB_PASSWORD=my_password
Run database migrations:
php artisan migrate
8. Optional Commands
your code here
Clear Application Cache:
php artisan cache:clear
Update Composer Dependencies:
composer update
Your Laravel application is now ready to use! If you encounter any issues, feel free to ask for help. 😊