How to fix Laravel command not found on macOS for bash/zsh

If you’re developing Laravel applications, you’re very likely to use a local development installation. Installing Laravel is not very simple, especially if you’re not familiar with composer or with the command line. In this tutorial, I’ll show you a simple way that will most likely fix the problem.

For this tutorial, I’ll assume that you’ve already got Composer installed (globally).

Before starting, you should determine whether you’re using bash or zsh as a command line shell. If you’ve got no idea what this is, don’t worry 😉. If you’re using a recent macOS, macOS Catalina or later, zsh is the default. If you want to check if your Mac is using bash or zsh, open terminal and type one or both of these commands:

echo $ZSH_VERSION
echo $BASH_VERSION

One of the commands, the active one, will return a version number (something like 5.8). The inactive one will return an empty line.

How to fix the Laravel command not found for zsh (macOS Catalina and newer)

To allow the use of commands starting with ‘laravel‘, you’ll need to edit your .zshrc file. This file should be in your home folder.

First, check whether the file exists:

open -e ~/.zshrc
# If you get an error, open up text editor and create 
# an empty file called '.zshrc' in your home folder.

Next, add the following code at the bottom of your .zshrc file:

PATH="$HOME/.composer/vendor/bin:$PATH"

Save the file. The changes will now be implemented on any new terminal session. Stop the current session or run the code below, and then it will work for the current session too:

source ~/.zshrc
Fixing the Laravel command not found by modifying the $PATH for zsh command line shell.

How to fix the Laravel command not found for bash (macOS Mojave and older)

We need to do something very similar for bash as for zsh. This time, we’ll edit the .bashrc file. Open it (or create it, if needed), manually or via terminal:

open ~/.bash_rc

Now add the following line at the end of the file:

export PATH="$HOME/.composer/vendor/bin:$PATH"

Here, too, the changes will only apply for a new session. So stop the current session or run the below code:

source ~/.bashrc

Why did we do this?

Laravel is installed via Composer. If you type a command like ‘laravel‘, you are referencing an executable called laravel. The command line has a variable somewhere (called $PATH), where you can add directories to look for executables. If you put an executable in one of the directories in the $PATH, you do not need to set the path to the executable / script, but you can run it by its name as a command.

Thus, if you run a command for an executable in a folder that’s not in your $PATH, you’ll get a command not found message. So, we need to add the path to this directory.

I hope this has been helpful for you. Let me know in the comments if it worked for your

Stay up to date with all things Laravel, Tailwind, WordPress & PHP

Subscribe now to my e-mail newsletter and get my latest articles and project updates delivered directly to your inbox. Never miss an update.

Image Ralph J. Smit
Ralph is a designer gone developer. He happily lives in the Netherlands. His passion for good design drove him towards development, because he felt that no-code tools were too limiting. On this blog, Ralph writes the articles he would've wanted to have during his continual developer journey. → Follow on Twitter

Comments

  • rezarahmati

    November 18, 2021

    thanks Ralph Smit i have a mac os, but when i run following command get this error "The file /Users/rezagk/.bash_rc does not exist. " open ~/.bash_rc please help me

    • Ralph Smit

      November 18, 2021

      Hey there, looks like the file wasn't created yet. You could try to run the following commands: touch ~/.bashrc open ~/.bashrc The 'touch' command creates the file first if it doesn't exist yet, after which you can open it. If that doesn't work, try opening and creating a .zshrc file: touch ~/.zshrc open ~/.zshrc

  • Php shell

    September 10, 2021

    Do you mind if I quote a couple of your articles as long as I provide credit and sources back to your website? My blog site is in the exact same niche as yours and my users would genuinely benefit from a lot of the information you provide here. Please let me know if this okay with you. Regards!

    • Ralph Smit

      September 13, 2021

      Thanks for leaving a comment here! I'm totally fine with that

Leave a reply

Your email address will not be published. Required fields are marked *

Close
Do you want more articles like this delivered straight to your inbox?

Subscribe now to my e-mail newsletter and get my latest articles and project updates delivered directly to your inbox. Never miss an update.