Your First Steps with Laravel: A Beginner’s Guide 🚀
Ready to dive into the world of web development with one of the most elegant and powerful PHP frameworks? Laravel is a fantastic choice, and getting started is easier than you might think. This guide will walk you through setting up a new Laravel application from scratch, covering all the essential tools and commands you’ll need.
Prerequisites: What You Need to Get Started
Before you install Laravel, you need to ensure your system has a few key components. These are the foundation of your development environment.
- PHP: Laravel is a PHP framework, so you need a compatible version installed. For the latest versions of Laravel, you’ll need PHP 8.1 or higher. You can check your PHP version by running
php -vin your terminal. - Composer: Composer is the dependency manager for PHP. It’s used to install and manage all the libraries and packages your Laravel application will need. If you don’t have it, you can download it from the official Composer website.
- A Local Server: While Laravel has a built-in development server, you’ll need a way to run your PHP code. Popular choices include Laravel Herd (for macOS and Windows), which provides a complete development environment out of the box, or you can use a stack like XAMPP or configure a server like Nginx or Apache yourself.
Step 1: Install a New Laravel Application
Once you have the prerequisites in place, you can create a new Laravel project. There are two common methods: using the Laravel Installer or Composer.
Method 1: The Laravel Installer (Recommended for multiple projects)
This method is faster and great if you plan to create many Laravel applications.
- Install the Laravel Installer globally on your system using Composer: Bash
composer global require laravel/installer - Create a new project with the
laravel newcommand: Bashlaravel new my-appReplacemy-appwith your desired project name. This command creates a new directory with a fresh Laravel installation and all its dependencies.
Method 2: Composer create-project
This is a good alternative if you don’t want to install the Laravel Installer globally.
- Run the following command in your terminal:Bash
composer create-project laravel/laravel my-appThis command downloads the latest stable version of Laravel and sets up the new project in a directory namedmy-app.
Step 2: Configure Your Environment
After installation, the most important file you’ll interact with is the .env file located at the root of your project. This file holds all your environment-specific configurations, such as database credentials and API keys.
- Open the
.envfile in your code editor. - Update your database credentials to match your local setup. For example, for a MySQL database:Code snippet
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=my_app_db DB_USERNAME=root DB_PASSWORD=Make sure to create the database (my_app_dbin this example) in your local database management tool (like phpMyAdmin or Sequel Pro).
Step 3: Run Your Application
Now you’re ready to see your new Laravel application in action.
- Navigate into your project directory:Bash
cd my-app - Run the local development server using the
artisancommand-line tool that comes with
Laravel: Bashphp artisan serve
- Open your web browser and go to
http://127.0.0.1:8000(orhttp://localhost:8000). You should see the official Laravel welcome page! 🎉
Next Steps
Now that your Laravel application is up and running, here are a few things you can do to take it a step further:
- Database Migrations: Laravel’s migrations allow you to version-control your database schema. Run
php artisan migrateto create the default tables for users, passwords, and more. - Models, Views, and Controllers (MVC): Start building your application by learning about the MVC architecture. Use the
php artisan make:model,php artisan make:controller, andphp artisan make:viewcommands to generate boilerplate code. - Routing: Define your application’s routes in the
routes/web.phpfile to handle incoming requests and direct them to the appropriate controller actions.