How to install Laravel Sanctum via Composer?

28.01.2025

Laravel Sanctum is a simple package for API token authentication in Laravel. It provides a simple way to authenticate and manage API tokens for your application. In this article, we will discuss how to install Laravel Sanctum via Composer.

How to remove a package from Laravel using PHP Composer ...

Step 1: Install Laravel

If you haven’t already, make sure you have Laravel installed on your system. You can install Laravel by running the following Composer command:

The ULTIMATE Laravel Nova Tutorial [Walkthrough w/ Examples …
Nov 16, 2022 … … laravel.com/docs/4.0/installation.html – Composer Install: https://nova.laravel … Laravel API Crash Course With Sanctum | Laravel Sanctum …
composer create-project --prefer-dist laravel/laravel projectName

Step 2: Install Sanctum Package

Once you have Laravel installed, you can install the Sanctum package using Composer. Run the following command in your terminal:

composer require laravel/sanctum

Step 3: Run Sanctum Installation Command

After installing the Sanctum package, run the following Artisan command to publish the Sanctum configuration and migration files:

php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"

Step 4: Run Migrations

Next, run the migration command to create the necessary tables for Sanctum:

php artisan migrate

Step 5: Add Sanctum’s Middleware

After running the migrations, you need to add Sanctum’s middleware to your API routes. Open your app/Http/Kernel.php file and add the following line to the $middlewareGroups array:

'api' => [     \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,     'throttle:api',     \Illuminate\Routing\Middleware\SubstituteBindings::class, ],

Step 6: Configure Sanctum

You can configure Sanctum in the config/sanctum.php file. This file allows you to customize the behavior of Sanctum according to your needs.

Step 7: Using Sanctum for API Authentication

Now that you have installed and configured Sanctum, you can start using it for API authentication in your Laravel application. You can generate API tokens, authenticate users, and protect your API routes using Sanctum’s features.

By following these steps, you can easily install Laravel Sanctum via Composer and set up API token authentication in your Laravel application.

Yan Hadzhyisky

fullstack PHP+JS+REACT developer