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

Published March 2, 2021
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

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

Leave a reply

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