How to Set Up Artisan CLI in Windows

Updated: January 18, 2024 By: Guest Contributor Post a comment

Introduction

The Artisan Command Line Interface (CLI) is an intrinsic part of the Laravel framework, providing developers with a powerful toolset for application development. Installing and setting up Artisan CLI in a Windows environment can streamline your development workflow. This guide will walk you through the steps required to install and configure the Artisan CLI for Laravel on a Windows machine.

Prerequisites

  • A Windows operating system.
  • PHP installed and configured properly.
  • Composer – a dependency manager for PHP.
  • Laravel installer.

Step-by-Step Instructions

Step 1: Install PHP

Before we begin, ensure that you have PHP installed. You can download PHP from windows.php.net/download. After downloading, extract the files to a preferred directory (e.g., C:\php).

REM Add PHP to your PATH environment variable
setx path "%path%;C:\php"

Step 2: Install Composer

Composer is required to install Laravel and manage its dependencies. If you haven’t already installed Composer, visit getcomposer.org/download/ and follow the instructions provided.

Step 3: Install Laravel and Artisan

With PHP and Composer ready, you can now globally install the Laravel installer using Composer.

composer global require laravel/installer

Ensure the Composer’s system-wide vendor bin directory is in your system’s PATH so the laravel executable can be located by your system. This path is typically %USERPROFILE%\AppData\Roaming\Composer\vendor\bin on Windows.

REM Add Composer's bin directory to your PATH
setx path "%path%;%USERPROFILE%\AppData\Roaming\Composer\vendor\bin"

Step 4: Creating a New Laravel Project

To create a new Laravel project, navigate to the directory where you want the project to live, and run the following Laravel new command:

laravel new blog

Replace ‘blog’ with whatever you want to name your project. If the above command is executed without any issues, a new directory called ‘blog’ would be created with a fresh Laravel installation, and consequently, Artisan CLI will be set up within this directory.

Step 5: Verify Artisan CLI Installation

To ensure Artisan CLI has been installed correctly, navigate to your project’s directory and run:

cd path\to\your\blog
php artisan

You should see an output listing all the available Artisan commands, which means Artisan CLI is set up and ready to be used.

Using Artisan CLI

With Artisan CLI successfully set up, let’s look at some common commands you will use frequently:

  • php artisan list: Displays a list of all available Artisan commands.
  • php artisan serve: Starts a development server at http://localhost:8000
  • php artisan make:model: Creates a new Eloquent model class.
  • php artisan make:controller: Creates a new controller class.
  • php artisan migrate: Runs the database migrations.

Customizing Artisan Commands

Beyond leveraging the provided Artisan commands, Laravel also allows you to create custom commands if you need functionality outside of what is provided. This is done via the php artisan make:command command.

php artisan make:command CustomCommand

Troubleshooting

If you experience any issues, ensure that PHP and Composer’s bin directories are added correctly to your system’s PATH. Restart your command prompt or system if changes don’t reflect immediately after editing environment variables. Check also that PHP is configured with the required extensions and that you run the command prompt with administrative privileges when setting up PATH variables.

Concluding, setting up Artisan CLI on Windows is a fairly straightforward process that can greatly enhance your Laravel development experience. Ensure you keep both PHP and Composer updated to avoid any potential issues.