Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I get "laravel: command not found" on Ubuntu 20.04

I did a fresh install of Ubuntu 20.04 LTS (Focal Fossa), installed Apache, PHP, MySQL and PHP Composer seemingly without issue. However, I still cannot get laravel -V to give me a version number.

I have looked at a multitude of YouTube videos and tried my interpretation of the recommendations found here on Stack Overflow. How can I fix it?

Here's the output on my shell $PATH.

like image 956
FredKearnes Avatar asked Apr 23 '20 19:04

FredKearnes


1 Answers

You must add PHP Composer binaries folder to your $PATH if you'd like to call the binaries globally.


A) Make sure you have the latest Laravel installer:

composer global require laravel/installer

B) Add composer bin folder to your $PATH:

  1. Edit your .bashrc file: gedit $HOME/.bashrc

  2. Add the following line: export PATH="$PATH:$HOME/.config/composer/vendor/bin"

C) Use the source command to force Ubuntu to reload your .bashrc file:

source $HOME/.bashrc

D) Try to output Laravel installer's version:

laravel -V


Additional explanations as requested:

To execute a command from the Linux terminal, you need to tell Linux where the program is located.

For example, you could have launched Laravel installer using the full path: $HOME/.config/composer/vendor/bin/laravel -V

But instead, you wanted to be able to call the laravel -V command directly because you don't want to type the full path every time.

Since you are on Ubuntu, the default shell program is Bash. You need to tell Bash where to look when you type a command. In this case, you want Bash to look in the $HOME/.config/composer/vendor/bin/ folder.

The configuration file for Bash is a hidden file called .bashrc located in the user home folder. Bash stores the list of special folders in a variable called $PATH. To add a new folder, we simply added it to the $PATH variable.

If you type echo $PATH in your terminal, Bash will output the content of the $PATH variable and you will see a list of folders.

Now you may ask: "Why did I have to do this? I usually don't have to mess with my Bash configuration". Yes, this is because you usually install Ubuntu packages and they are configured to work out of the box. In this case, you installed a composer package in your home directory and it's therefore up to you to configure it the way you want.

like image 98
LobsterBaz Avatar answered Oct 30 '22 09:10

LobsterBaz