DEV Community

Nelson Orina
Nelson Orina

Posted on

Getting Started with Laravel.

So, you've heard about Laravel, the most popular PHP framework for a reason. It's elegant, powerful, and makes web development a joy. But starting can seem daunting. Worry not! This guide will walk you through creating your very first Laravel application, from setting up your environment to running a "Hello, World" page.

What We'll Cover:

  1. Setting Up Your Environment
  2. Installing Laravel
  3. Understanding the Project Structure
  4. Running Your Development Server
  5. Creating Your First Route & View

Step 1:Setting Up Your Environment
Before we can write any Laravel code, we need the right tools. Laravel requires PHP, a package manager called Composer, and some PHP extensions.

For Ubuntu/Linux Users
If you're using Ubuntu or another Debian-based distribution, open your terminal and follow these steps:

1.Update Your Package List

sudo apt update
Enter fullscreen mode Exit fullscreen mode

2.Install PHP with Required Extensions
Laravel requires several PHP extensions. Install them all with :

sudo apt install php php-cli php-fpm php-json php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-bcmath php-json
Enter fullscreen mode Exit fullscreen mode

3.Install Composer

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer
Enter fullscreen mode Exit fullscreen mode

4.Install and Configure a Database
For MySQL:

sudo apt install mysql-server mysql-client
sudo mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode

For PostgreSQL:

sudo apt install postgresql postgresql-contrib
Enter fullscreen mode Exit fullscreen mode

5.Verify Your Installation

php --version
composer --version
Enter fullscreen mode Exit fullscreen mode

You should see version numbers for both.

For Windows Users
The easiest way is to use Laragon:

  1. Download and install Laragon(choose the "Full" version)

  2. It automatically includes PHP, Composer, MySQL, and more

  3. Launch Laragon and you're ready to go!

For Mac Users
Use Laravel Herd for the simplest setup:

  1. Download and install Laravel Herd
  2. It automatically manages PHP versions and includes Composer 3 . It is incredibly simple and just works

Manual Installation (Any OS)

  1. PHP:Download from php.net

2.Composer:Follow instructions at getcomposer.org

3.Database:Install MySQL, PostgreSQL, or SQLite

Verify everything is working:

php --version
composer --version
Enter fullscreen mode Exit fullscreen mode

Step 2:Installing Laravel
Now for the fun part! We'll use Composer to create a new Laravel project.

1.Open your terminal
2.Navigate to the directory where you want to keep your projects (e.g., cd ~/Sites or cd C:\Apache\htdocs).
3.Run the following command to create a new project named my-first-blog:

composer create-project laravel/laravel my-first-blog
Enter fullscreen mode Exit fullscreen mode

This command will download all the necessary Laravel files and dependencies. It might take a few minutes.
Once it's done navigate into your new directory:

cd my-first-blog
Enter fullscreen mode Exit fullscreen mode

Step 3:A Quick Look at the Project Structure
Let's open the my-first-blog folder in any code editor(like VSCode). Here are the key folders you will work with:

  • app/: The heart of your application. This is where you'll write your models, controllers, and core logic.
  • config/: Contains all the configuration files for your app(database, mail, etc.).
  • database/: Houses your database migrations, seeders, and factories.
  • public/: The web server's document root. Your index.php file is here. This is the only folder accessible to the public.
  • resources/: Contains your views(Blade templates), CSS, and JavaScript (if using Vite).
  • routes/: This is where you define your application's endpoints. The web.php file is where we'll start.

Step 4:Running Your Development Server
Laravel comes with a built-in development server that's perfect for local testing.

Make sure you are in your project's root directory (my-first-blog) in the terminal and run:

php artisan serve
Enter fullscreen mode Exit fullscreen mode

Artisan is Laravel's command-line tool.
The terminal will output something like:

Starting Laravel development server: http://127.0.0.1:8000
Enter fullscreen mode Exit fullscreen mode

Open your web browser and go to http://127.0.0.1:8000. You should see the default Laravel welcome page.
Congratulations! Your Laravel application is now live.

Step 5:Creating Your First Route and View
Let's move beyond the default page and create a simple "Hello, World!" page.

1.Define a Route:
Open the routes/web.php file. You'll see a default route. Let's add a new one.

<?php

use Illuminate\Support\Facades\Route;

//This is the default route that shows the welcome page
Route::get('/', function(){
   return view('welcome');
});

//This is our new route
Route::get('/hello', function(){
   return "Hello, World!";
});
Enter fullscreen mode Exit fullscreen mode

Save the file. Now, go to http://127.0.0.1:8000/hello in your browser. You should see the plain text "Hello, World!".

2.Create a View (A Proper HTML Page):
Returning plain text is fine, but let's create a proper HTML page.

  1. Go to the resources/views/ folder.
  2. Create a new file called hello.blade.php.
  3. Add some simple HTML to this file:
<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>My First Laravel Page</title> 
   </head>
   <body>
      <h1>Hello from my first Laravel View</h1>
      <p>This is HTML, served from a Blade template.</p>
   </body> 
</html>
Enter fullscreen mode Exit fullscreen mode

3.Update the Route to Use the View:
Now, go back to routes/web.php and change the /hello route to return this view instead of the plain text.

Route::get('/hello',function(){
   return view('hello');//This points to resources/views/hello.blade.php
});
Enter fullscreen mode Exit fullscreen mode

Refresh your browser at http:/127.0.0.1:8000/hello. You should now see your styled HTML page!

What's Next?
You've successfully set up your environment, installed Laravel, and created your first custom page! From here, you can expore:

  • Blade Templating for more dynamic views

  • Controllers to organize your code better

  • Database & Eloquent ORM to work with data

Top comments (0)