The "Laravel command not found" error occurs because the system cannot locate the executable in your current PATH. This is often caused by an incorrect working directory or a misconfigured shell profile. Verify your installation location and ensure your environment variables are correctly set to resolve the issue.
Understanding the Error Context
When you install Laravel via Composer, it places the laravel binary in a specific directory within your user space. By default, Composer installs global packages into the ~/.composer/vendor/bin directory on Linux and macOS systems. If your terminal session does not recognize this path, any attempt to run laravel new project-name will fail with a "command not found" message.
This problem frequently arises after accidentally changing the terminal's working directory to a location that lacks the necessary permissions or context. For instance, if you navigate deep into a nested project structure without adjusting your environment variables, the shell may lose track of global tools. It is crucial to distinguish between the local project dependencies and the global CLI tools provided by Composer.
Verifying Your Installation Path
The first step in troubleshooting is to confirm where Composer has actually placed the Laravel installer. You can check this by running the following command in your terminal:
which laravel
If the command returns nothing or outputs a path that seems incorrect, your PATH variable is likely incomplete. On most systems, the correct path should resemble /home/yourusername/.composer/vendor/bin/laravel. If you see a different path, such as one inside a temporary folder or a vendor directory of a specific project, you are looking at a local installation rather than the global one.
To find the exact location manually, you can use the find command to search your home directory for the executable:
find ~/.composer -name "laravel" -type f 2>/dev/null
Once you identify the correct path, note it down. You will need this information to update your shell configuration file in the next section. This diagnostic step ensures you are not guessing but working with concrete file system data.
Updating Shell Configuration
Most Linux distributions use Bash or Zsh as their default shell. To make the laravel command available globally, you must add the Composer bin directory to your PATH environment variable. Open your shell configuration file using a text editor like nano or vim.
For Bash users, edit the ~/.bashrc or ~/.profile file. For Zsh users, edit ~/.zshrc. Add the following line to the end of the file:
export PATH="$HOME/.composer/vendor/bin:$PATH"
After saving the file, you must reload the configuration for the changes to take effect immediately. Run the following command:
source ~/.bashrc
Or for Zsh:
source ~/.zshrc
Now, try running laravel --version. If it returns the version number, the path has been successfully corrected. This method ensures that every new terminal window will automatically include the Laravel installer in its search scope.
Checking for Directory Confusion
Sometimes the issue is not just about the PATH variable but also about the current working directory. If you recently changed directories using cd, you might be in a folder that conflicts with the global tool execution. Always return to your home directory or a clean workspace before testing global commands.
You can verify your current directory by running pwd. If you are deep inside a project, navigate back up using cd ~. Then, attempt to create a new project in a parent directory to test if the command works. This helps isolate whether the error is environmental or contextual.
Additionally, ensure that there are no typos in your command. The command is strictly laravel, not Laravel or Laravel-cli. Case sensitivity matters in Unix-based systems. If you previously tried php artisan outside of a Laravel project, you might have encountered similar errors, as artisan is project-specific and not globally installed.
Alternative Verification Methods
If updating the PATH does not resolve the issue, consider reinstalling the global package. Sometimes, the initial installation might have failed silently due to permission issues. You can force a reinstallation by running:
composer global require laravel/installer
After the process completes, clear any cached binaries if necessary. On some systems, Composer caches executable paths. Running composer clear-cache can help refresh the internal state. Finally, verify the installation again with which laravel to ensure the symlink or executable is correctly linked.
For more detailed insights on terminal path management, you might find our guide on handling Debian terminal path mistakes helpful. It covers scenarios where directory changes lead to persistent configuration errors. You can read more here: Linux: Debian Terminal Path Change Mistake when Changing Laravel Directory.
Frequently Asked Questions
Why does 'which laravel' return nothing?
This indicates that the executable is not in any directory listed in your current PATH environment variable. You likely need to add the Composer vendor bin directory to your shell configuration file as described in the article.
Can I use Laravel without installing the global installer?
Yes, you can create projects using Composer directly by running composer create-project laravel/laravel myproject. This method bypasses the need for the global laravel command entirely and is often preferred for strict dependency management.
What if I am using PowerShell on Windows?
On Windows, Composer typically adds the global bin directory to your system PATH during installation. If it fails, you may need to manually add %USERPROFILE%\AppData\ComposerVendor\bin to your system environment variables and restart your terminal.
How do I know if I am in the right directory?
Run pwd to print the working directory. If you are inside a subdirectory of a Laravel project, global commands might behave unexpectedly. Navigate to your home directory (cd ~) to ensure a neutral environment for testing.
Is it safe to modify .bashrc or .zshrc?
Yes, these files are standard configuration files for Linux and macOS shells. Adding export statements for PATH is a common and safe practice. However, always backup these files before editing to prevent accidental loss of other configurations.