Install Laravel
In this article I will be explaining everything you need to install Laravel.
Although Laravel official Documentation page is self explanatory but some of the developers still don’t understand everything and here exactly what is explained.
Installing Laravel on Windows
Installing Laravel on MacOs
On MacOS, all you need is a text editor like phpstorm or visual studio code or whatever you choose, as well as access to your terminal or iterm if you prefer.
On the Laravel official documentation of Installing it on mac they suggest two things, a Docker Environment (if you don’t know what is Docker you can read about Docker here) and CLI called Laravel sail. To quickly summarize what we refer to as a container, which contains everything you need to get started creating Laravel apps. Think of it as Black Box which already has Php, Reddis, MySql.
Now if you want to work locally and you have these tools installed that is a great option as well, choose what you prefer.
Now if you don’t have these tools don’t worry we will installed it from scratch.
Install Homebrew, which is a package manager for macOS (or Linux) created by The Missing Package Manager.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Now install php by the following command through homebrew
brew install php
It will install the most recent version of PHP, and if you wish to install a specific version of PHP, you can do so as well.
brew install php@7.4 brew install php@7.3
Now install mysql by,
brew install mysql
It may ask you run the services of mysql to always run in the background, so run the following command to insure it is always running in the background
brew services start mysql
You can sign in to mysql as
mysql -u root
but we want a gui version to play with mysql tables not in the CLI version, so install tableplus or Seqel pro or Myqsl workbench application whatever you want but my preference will be Table Plus, install and run it.
when you run it click on the create new connection and select the database you want for example in our case in Mysql and give the following credentials as,
Host: 127.0.0.1
Port: 3306
Tag: Local
User: root
If you want to give your database a password, leave it blank.
and save with the above credentials.
Note that the above credentials will be required in your .env file if you want to build a dynamic application.
The last step is to install composer, which is a PHP dependency manager.
So install it via,
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php -r "if (hash_file('sha384', 'composer-setup.php') === '906a84df04cea2aa72f40b5f787e49f22d4c2f19492ac310e8cba5b96ac8b64115ac402c8cd292b8a03482574915d1a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" php composer-setup.php php -r "unlink('composer-setup.php');"
This will install composer locally and generate a file named composer.phar, but we want to make it global so that we can access it from everywhere, so run the following command.
mv composer.phar /usr/local/bin/composer
next is to download the Laravel Github through composer as,
composer create-project laravel/laravel example-app
This will save the laravel project to your machine in the example-app directory.
If you want to install a particular version of laravel you can also install that version by,
composer create-project --prefer-dist laravel/laravel:^7.0 example-app
so you can cd to your example-app directory via terminal and run the following command to start it’s development server
php artisan serve
Although composer is a fantastic way to install Laravel, the Laravel documentation itself provides an easier and cooler way to do it, which is the Laravel installer method.
As a result, we use composer to install the Laravel package worldwide.
composer global require laravel/installer
Now when you type laravel in your terminal and it does not work it is because your composer bin directory is not part of the system path.
so make it part of your system as,
cd ~/.composer/vendor/bin pwd
You’ll get a full path when you run the pwd command, which you can add to your bashrc file, but the easiest way is to put it in your path file as,
sudo vim /etc/paths
place the path at the end of the file
Now you will be able to access the laravel installer globally and you can install Laravel as,
laravel new example-app
It will install the most recent version of Laravel into the location that you choose.