How to run laravel artisan command on server?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
How to Run Laravel Artisan Commands on Your Server: A Developer's Guide
As developers, deploying and managing applications across different environments—local machines, staging servers, and production hosts—is a daily necessity. When working with a framework as powerful as Laravel, running commands is how we manage migrations, seeders, queue jobs, and other critical maintenance tasks. The key to successfully executing these operations lies in understanding the server environment and the command-line interface (CLI).
This guide will walk you through the definitive process of running Laravel Artisan commands on your remote server, ensuring you have a solid understanding of the prerequisites and best practices.
Understanding the Environment: SSH is Your Gateway
The fundamental way to interact with any remote server is through Secure Shell (SSH). SSH provides a secure, encrypted channel for you to execute commands directly on the server's operating system. When deploying or managing a Laravel application, this is the primary tool we use.
Before attempting to run any command, ensure you have valid SSH access credentials (username and password/key) for your hosting environment.
Step 1: Connecting to the Server
Use an SSH client (like PuTTY on Windows, or the built-in Terminal on macOS/Linux) to connect to your server.
ssh username@your_server_ip
Once connected, you are operating directly within the server's command line environment.
Step 2: Navigating to the Project Directory
After logging in, you need to navigate to the root directory of your Laravel project where the artisan file resides. This is typically done using the cd command.
# Example: Navigate to the public_html or desired application folder
cd /var/www/html/your_laravel_app
Step 3: Executing the Artisan Command
Once you are inside the project root, you can run any command prefixed with php artisan. This is how you interact with Laravel's built-in command-line tools.
For your specific example command, running composer require realrashid/sweet-alert is a Composer operation, not an Artisan command. These two systems work together: Composer manages PHP dependencies, and Artisan manages the framework-specific tasks.
If you were running a standard Laravel command, for instance, to create a new controller:
php artisan make:controller PostController
This command tells the PHP interpreter (via the CLI) to execute the Artisan tool, which in turn interacts with the Laravel framework to generate the necessary file structure.
Managing Dependencies with Composer
If your goal is to install a new package—like the sweet-alert example you provided—you must use Composer, which is the dependency manager for PHP projects. You run Composer commands directly via SSH, just as you would any other system command:
# Run this command on the server inside your project directory
composer require realrashid/sweet-alert
This ensures that the required package and all its dependencies are installed into the vendor directory of your Laravel application, making them available for your application code. This dependency management is crucial for maintaining the integrity of your application structure, aligning with best practices promoted by the wider Laravel community.
Best Practices for Server Operations
- Use SSH Keys: Whenever possible, use SSH key pairs instead of passwords for authentication. This is more secure and streamlines automated deployment processes.
- Permissions Matter: Ensure that the user account you are logged in with has the necessary file permissions to write and modify files within your project directory (e.g., using
sudoor ensuring ownership is correct). - Environment Consistency: Always verify that the PHP version, Composer version, and installed extensions on the server match what you use locally. Inconsistent environments are a common source of deployment errors.
Conclusion
Running Laravel Artisan commands on a server boils down to establishing a secure SSH connection, navigating to the correct project directory, and executing the command using the php artisan prefix for framework tasks or the composer command for dependency management. By mastering this workflow, you gain full control over your application lifecycle, ensuring reliable deployment and maintenance across any hosting environment. For deeper insights into structuring and deploying robust applications, exploring resources from laravelcompany.com is highly recommended.