How to Set Up a Laravel Project

24 Dec

1. Install Composer

Laravel uses Composer to manage dependencies, so you need to install it first.

  • Visit Composer's official website and follow the installation instructions for your operating system.

  • You can verify that Composer is installed correctly by running the following command in the terminal:

    bash Copy code composer --version    

2. Install PHP

Laravel requires PHP, usually PHP 8.0 or higher. Ensure that PHP is installed on your machine. You can check by running:

bash Copy code php --version

If PHP is not installed, download and install it from PHP's official website, or use a package manager like Homebrew (macOS), apt (Ubuntu), or XAMPP/WAMP (Windows).

 

 

3. Install Laravel Installer (Optional)

Laravel provides a command-line tool called the Laravel Installer. You can install it globally using Composer:

bash Copy code composer global require laravel/installer

Ensure the Composer global bin directory is in your system's PATH to run laravel commands from the terminal.

To verify the installation:

bash Copy code laravel --version

4. Create a New Laravel Project

Now, you can create a new Laravel project. There are two ways to do this:

Option 1: Using the Laravel Installer (if installed)

bash Copy code laravel new project-name

Option 2: Using Composer (recommended if the Laravel installer is not installed)

bash Copy code composer create-project --prefer-dist laravel/laravel project-name

This command will create a new Laravel project named project-name in the current directory.

5. Navigate to Your Project

Go to your newly created Laravel project folder:

bash Copy code cd project-name

6. Set Up Environment Configuration

Laravel uses an .env file for environment-specific configurations like database settings, mail services, and others.

  • Copy the .env.example file to .env:

    bash Copy code cp .env.example .env
  • You can now configure the settings in the .env file, like database connections and application key.

7. Generate Application Key

Laravel requires an application key for encryption and security. Generate it by running the following command:

bash Copy code php artisan key:generate

This will set the APP_KEY in your .env file.

8. Set Up Your Database (Optional)

If your project uses a database (such as MySQL, SQLite, etc.), you need to configure it in the .env file.

For example, for MySQL:

ini Copy code DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_database_name DB_USERNAME=your_username DB_PASSWORD=your_password

Make sure to create the database first if it doesn’t exist.

9. Run Migrations (Optional)

If your project includes database migrations, you can run them with the following command:

bash Copy code php artisan migrate

10. Serve Your Application

Laravel comes with a built-in development server. To start the server and access your project, run:

bash Copy code php artisan serve

By default, the server will run at http://localhost:8000.

11. Optional: Install Frontend Dependencies

If you're working with frontend tools (like Vue, React, or Tailwind CSS), you can use Laravel Mix. Install the necessary Node.js dependencies:

bash Copy code npm install

Then you can compile assets by running:

bash Copy code npm run dev

For production, you can use:

bash Copy code npm run production

12. Start Developing

At this point, your Laravel environment is ready, and you can start building your application!

Additional Tips:

  • Routes: You can define your application’s routes in the routes/web.php file.
  • Controllers: Create controllers with php artisan make:controller ControllerName.
  • Models: Create models with php artisan make:model ModelName.
  • Testing: Use the built-in testing capabilities by running php artisan test.

This guide covers the basics of getting Laravel up and running. For more advanced setup and customization, refer to the Laravel documentation.

Tags Laravel Blog