How to Set Up Artisan CLI on Mac

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

Introduction

The Artisan Command Line Interface (CLI) is the name of the command-line tool for Laravel, a popular PHP framework. It provides a number of helpful commands for use during your application’s development. In this guide, we will walk through the details of setting up Artisan for a Laravel project on a Mac. Whether you are a seasoned developer or just starting with Laravel, understanding how to use Artisan can significantly speed up your development process.

Prerequisites

  • A Mac computer
  • PHP installed (version should be compatible with the Laravel version you want to use)
  • Composer, PHP’s dependency manager
  • MySQL or another database system (optional, but often necessary for full application development)

Step-by-Step Instructions

Step 1: Install Homebrew

Homebrew is a package manager for Mac, which simplifies the installation of software. Open your terminal and input the following:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Follow the on-screen instructions to complete the installation of Homebrew.

Step 2: Install PHP

If you haven’t already installed PHP on your Mac, use Homebrew to install it with the following command:

brew install php

This command will install the latest PHP version available through Homebrew. To check whether PHP is installed correctly, run:

php -v

Step 3: Install Composer

Composer is required for managing Laravel’s dependencies. To install Composer, use the command:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"

You can make Composer globally accessible by moving it to /usr/local/bin:

mv composer.phar /usr/local/bin/composer

Step 4: Install Laravel and Artisan

With Composer installed, you can now create a new Laravel project:

composer create-project --prefer-dist laravel/laravel myLaravelApp

Navigate into your application directory:

cd myLaravelApp

Laravel’s Artisan CLI will be automatically installed as part of the Laravel framework. To see a list of all available Artisan commands, run:

php artisan list

Step 5: Using Artisan Commands

Artisan has many commands that help automate tasks. For example, to create a new controller, you could run:

php artisan make:controller MyController

To serve your application on the localhost, use Artisan’s serve command:

php artisan serve

This will start a development server at http://127.0.0.1:8000.

Step 6: Managing the Database with Artisan

If you’re using a database, which is common for most Laravel applications, you can use Artisan to handle migrations:

php artisan migrate

This will run the database migrations that come with a fresh Laravel application, setting up the tables needed for user authentication and password resets by default.

If you need to create a new database migration, use the make:migration command:

php artisan make:migration create_my_table --create=my_table

This will create a new migration file that you can edit to define your table’s columns and data types.

Step 7: Troubleshooting

If you encounter any issues with installation or using any of the Artisan commands, check the following:

  • Ensure that the PHP and Composer are correctly installed and added to your PATH.
  • Verify that you are within your Laravel application directory when attempting to use Artisan commands.
  • Make sure that the required permissions are set on your Laravel storage and bootstrap/cache directories.

For more detailed error information, check the Laravel and Artisan documentation or use the -v flag with any Artisan command to increase the verbosity of the output.

Conclusion

Setting up Artisan CLI on a Mac involves installing and configuring a few key elements such as PHP, Composer, and Laravel. Artisan is a vital tool in the Laravel ecosystem for streamlining common tasks, and with it properly set up, development can become faster and more efficient.

Remember, Laravel’s documentation is a valuable resource, so don’t forget to refer to it frequently for guidance on working with Artisan. With these steps, you should now have a fully functional Artisan CLI to help accelerate your development process on a Mac.