Laravel Installation and Setup
30 April 2025 | Category: Laravel
Setting up Laravel is straightforward, thanks to its well-documented process and developer-friendly tools. This guide provides a plagiarism-free, detailed walkthrough for installing and configuring Laravel on your system, ensuring you’re ready to start building modern web applications. Whether you’re using Windows, macOS, or Linux, we’ll cover the essentials, including prerequisites, installation methods, and initial setup, tailored for beginners and seasoned developers alike.
Prerequisites
Before installing Laravel, ensure your system meets the following requirements:
- PHP: Version 8.1 or higher (Laravel 10, as of 2025, requires PHP 8.1+).
- Composer: PHP’s dependency manager for installing Laravel and its packages.
- Web Server: Apache or Nginx (or a local development server like Laravel Sail or Valet).
- Database: MySQL, PostgreSQL, SQLite, or SQL Server (optional, depending on your project).
- Extensions: PHP extensions like
mbstring
,xml
,curl
,zip
,bcmath
,ctype
,json
,tokenizer
, andopenssl
. - Node.js and NPM (optional): For front-end asset compilation (e.g., Laravel Breeze or Vite).
- Git: For version control and cloning Laravel repositories.
Ensure you have a code editor (e.g., VS Code, PhpStorm) and a terminal for running commands.
Step 1: Install PHP and Required Extensions
- Download and Install PHP:
- Windows: Download PHP from php.net or use a package manager like XAMPP/WAMP.
- macOS: Install via Homebrew:
brew install php@8.1
. - Linux: Use your package manager, e.g.,
sudo apt install php8.1 php8.1-cli php8.1-fpm
(Ubuntu).
- Verify PHP Installation:
Runphp -v
in your terminal. You should see the PHP version (e.g., PHP 8.1.x). - Install Required Extensions:
- On Ubuntu:
sudo apt install php8.1-mbstring php8.1-xml php8.1-curl php8.1-zip php8.1-bcmath
. - On macOS: Most extensions are included with Homebrew’s PHP.
- On Windows: Enable extensions by uncommenting lines (e.g.,
extension=mbstring
) inphp.ini
.
- On Ubuntu:
- Check Extensions:
Runphp -m
to confirm all required extensions are loaded.
Step 2: Install Composer
Composer is essential for managing Laravel’s dependencies.
- Download Composer:
- Visit getcomposer.org and follow the instructions.
- Windows: Run the Composer installer.
- macOS/Linux: Use the command:
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
- Verify Installation:
Runcomposer --version
. You should see the Composer version (e.g., Composer 2.x.x).
Step 3: Install Laravel
Laravel can be installed globally or per project. We’ll cover the per-project method using Composer, as it’s the most common approach.
- Create a New Laravel Project:
Open your terminal, navigate to your desired directory (e.g.,cd ~/Sites
), and run:composer create-project laravel/laravel my-laravel-app
Replacemy-laravel-app
with your project name. This command downloads Laravel and its dependencies. - Alternative: Laravel Installer (Optional):
- Install the Laravel Installer globally:
composer global require laravel/installer
- Create a project:
laravel new my-laravel-app
- Ensure Composer’s global bin is in your PATH (e.g.,
~/.composer/vendor/bin
).
- Install the Laravel Installer globally:
- Navigate to Project Directory:
cd my-laravel-app
Step 4: Configure the Environment
Laravel uses a .env
file to manage environment-specific settings (e.g., database, app key).
- Copy the Example Environment File:
Laravel includes a.env.example
file. Create a.env
file:cp .env.example .env
- Generate Application Key:
Run the following Artisan command to generate a unique app key:php artisan key:generate
This updates theAPP_KEY
in.env
. - Edit .env File (Optional):
Open.env
in your editor and configure settings like:APP_NAME
: Your application name.APP_URL
: Your app’s URL (e.g.,http://localhost:8000
).DB_CONNECTION
: Database type (e.g.,mysql
,sqlite
).DB_HOST
,DB_PORT
,DB_DATABASE
,DB_USERNAME
,DB_PASSWORD
: Database credentials.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_db DB_USERNAME=root DB_PASSWORD=your_password
Step 5: Set Up a Local Development Server
Laravel includes a built-in server for development, but you can also use Apache, Nginx, or Docker-based solutions like Laravel Sail.
- Using Laravel’s Artisan Server:
Run:php artisan serve
This starts a server athttp://localhost:8000
. Visit this URL in your browser to see Laravel’s welcome page. - Using Laravel Sail (Docker):
- Ensure Docker and Docker Compose are installed.
- Laravel Sail is included in new projects. Publish Sail’s configuration:
php artisan sail:install
- Start Sail:
./vendor/bin/sail up
- Access your app at
http://localhost
.
- Using Valet (macOS):
- Install Laravel Valet via Composer:
composer global require laravel/valet
. - Run
valet install
and link your project:valet link my-laravel-app
. - Access at
http://my-laravel-app.test
.
- Install Laravel Valet via Composer:
- Apache/Nginx:
- Point your server’s document root to
my-laravel-app/public
. - Configure a virtual host to handle requests (refer to Laravel’s documentation for server-specific configs).
- Point your server’s document root to
Step 6: Verify Installation
- Open your browser and visit
http://localhost:8000
(or your configured URL). - You should see Laravel’s default welcome page, confirming a successful setup.
- If you encounter errors, check:
- PHP version and extensions (
php -v
,php -m
). - Permissions: Ensure
storage
andbootstrap/cache
directories are writable (chmod -R 775 storage bootstrap/cache
). .env
configuration: Verify the app key and database settings.
- PHP version and extensions (
Step 7: Optional Setup for Front-End Development
Laravel uses Vite for asset compilation (e.g., CSS, JavaScript).
- Install Node.js and NPM:
- Download from nodejs.org or use a package manager:
- Ubuntu:
sudo apt install nodejs npm
. - macOS:
brew install node
.
- Ubuntu:
- Download from nodejs.org or use a package manager:
- Install Dependencies:
Run:npm install
- Run Vite Development Server:
npm run dev
This compiles assets and enables hot-module replacement. - Build for Production (later, when deploying):
npm run build
Step 8: Database Setup (Optional)
If your project uses a database:
- Create a Database:
- For MySQL:
mysql -u root -p
, thenCREATE DATABASE laravel_db;
. - For SQLite: Create a file (e.g.,
touch database/database.sqlite
).
- For MySQL:
- Run Migrations:
Laravel includes default migrations. Run:php artisan migrate
This creates tables defined indatabase/migrations
.
Troubleshooting Common Issues
- Composer Errors: Ensure Composer is updated (
composer self-update
) and PHP meets requirements. - Permission Issues: Run
chmod -R 775 storage bootstrap/cache
andchown -R www-data:www-data .
(for Apache/Nginx). - Database Connection Errors: Verify
.env
credentials and ensure the database server is running. - Port Conflicts: If
php artisan serve
fails, specify a different port:php artisan serve --port=8001
.
What’s Next?
With Laravel installed, you’re ready to explore its features:
- Create routes in
routes/web.php
. - Build views using Blade in
resources/views
. - Define models and relationships with Eloquent in
app/Models
. - Use Artisan for scaffolding:
php artisan make:controller
,php artisan make:model
, etc.
This setup provides a solid foundation for your Laravel projects. Dive into our tutorial series to learn routing, controllers, Blade templating, and more, as you build dynamic, scalable applications!