Easy way to install Laravel 8 + Homestead

And setting them up to work with PHPStorm and XDebug tool

Today I will tell you how I deploy a new project for Laravel development on Vagrant (Laravel Homestead). I will not tell you what I install and why. This post is just a simple tutorial how to run a new Laravel application in 10 minutes.

If you find something that can be improved, feel free to write about it in the comments or select and click Ctrl+Enter.

Requirements

This tutorial is not about installing these things, I hope you can install it by yourself (if you don't have it already):

Step 1. Install Laravel

  1. Open your console, go to your project root folder (using cd command). Terminal in PhpStorm doing this automatically when you launch it.
  2. Run composer create-project --prefer-dist laravel/laravel . to download the Laravel (do not remove this dot at the end).

Step 2. Install HomeStead

  1. After the Laravel download is complete, run composer require laravel/homestead --dev to download the Homestead.
  2. Now we need to install Homestead by running a command.
    For Windows: vendor\\bin\\homestead make
    For Linux/macOS: php vendor/bin/homestead make
  3. (Optional) If you want to developing on your custom local domain, open Homestead.yaml (which in your project root folder), find sites section and change map parameter from homestead.test to your domain (for example: test.loc).
    If you did that, you need to add new entry 192.168.10.10 test.loc in your /etc/hosts file (in Windows: C:\Windows\System32\drivers\etc). Replace test.loc with your domain.
  4. Open .env file in your project root folder and change database connection settings:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# For MySQL DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret   # For PostgreSQL DB_CONNECTION=pgsql DB_HOST=127.0.0.1 DB_PORT=5432 DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret
  1. Start the VM by running vagrant up.

Step 3. Configuring PHPStorm

  1. Activate Laravel plugin (Settings > Plugins > Laravel > Activate). Then Settings > Languages & Frameworks > Laravel > Enable plugin for this project.
  2. Connect to our database by using credentials what are in of .env file with adding the 0 number to the end of the port.
  1. Go to Settings > Languages & Frameworks > PHP and change PHP Language Level to your current PHP version (you can find this out by logging in to your Vagrant VM via SSH: vagrant ssh > php -v > exit).
    After that, at the same page in PHPStorm, find the CLI Interpreter selector and then click on [...] > [+] (add new) > From Docker, Vagrant… > Vagrant > OK.
  2. Go to Settings > Command Line Tool Support > Click on the [+] (add new).
    In Chose Tool param select "Tool Based On Symphone Console" > OK > In alias type artisan, in Path To Script find artisan.php file in your project root folder.
  3. Mark some folders in PHPStorm by clicking on the right button at the folder > Mark Directory as.
    public folder - Resource root
    tests folder - Test Sources Root
    vendor & storage folders - Excluded
  4. (Optional) Change your code style to Laravel (Settings > Editor > Code Style > PHP > Set from (blue link on the right) > Laravel).

Step 4. Install Laravel Ide Helper

  1. Run composer require --dev barryvdh/laravel-ide-helper
  2. Open /config/app.php and add Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class inside Package Service Providers section.
  3. Open your composer.json file and put this code inside "scripts" section:
1 2 3 4 5 6 7 8 9 10 11 12 13
"post-update-cmd": [ "Illuminate\\Foundation\\ComposerScripts::postUpdate", "@ide-helper" ], "ide": [ "@ide-helper" ], "ide-helper": [ "@php artisan clear-compiled", "@php artisan ide-helper:generate", "@php artisan ide-helper:meta", "@php artisan ide-helper:models -N" ]
  1. Run composer ide-helper

Step 5. Configuring XDebug

  1. Go to Settings > Languages & Frameworks > PHP > Servers > Click on [+] (add new).
  2. In Name and Host parameter insert your domain.
  3. Check the Use path mappings parameter.
  4. Set Absolute Path value:
    /home/vagrant/code/ for project root folder
    /home/vagrant/code/public/ for /public folder

Step 6. Some other things

  1. Open .gitignore file in your project root folder and put it in:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
# Laravel gitignore /node_modules /public/hot /public/storage /storage/*.key /vendor /.idea /.vagrant Homestead.json Homestead.yaml npm-debug.log .env   # Larave IDE Helper _ide_helper.php _ide_helper_models.php .phpstorm.meta.php   # misc .DS_Store reports/* !reports/.gitkeep npm-debug.log yarn-error.log .phpunit.result.cache /user-customizations.sh /postgres_backup /mysqldump.sql.gz /mysql_backup /aliases /after.sh /Homestead.yaml /Homestead.json/.idea /vendor /.vagrant phpunit.xml .buildpath .settings .project
  1. Run php artisan storage:link to make a symbolic link from /public/storage/ to /storage/app/public/.

That's all 😉

(Optional) Also I usually...

  1. Install Laravel Debugbar by running composer require barryvdh/laravel-debugbar --dev.
  2. Generate auth files by running composer require laravel/ui and then php artisan ui <type> --auth. Replace <type> with type that you need (bootstrap, vue, react).
  3. Install Laravel Telescope by running composer require laravel/telescope and then php artisan telescope:install and then php artisan migrate.

VM control commands

Homestead is actually Vagrant, so we can use Vagrant commands to control it:

  • vagrant up to start the VM
  • vagrant halt to terminate the VM
  • vagrant ssh to connect to the VM via SSH (exit to exit from SSH mode)
  • vagrant destroy -f <name/ID of VM> to remove the VM
  • vagrant global-status to show all existing VM's

Comments

Spelling error report

The following text will be sent to our editors: