Get up to 80 % extra points for free! More info:

Lesson 2 - Installation of Laravel and first project run

In the last lesson, Introduction to Laravel framework for PHP, we introduced ourselves to the Laravel PHP framework and briefly discussed what knowledge we would need.

In today's lesson we will look at how to install Laravel as the basis of our application. We will perform this installation using the Composer tool and the command line.

Necessary software

At first, we'll prepare the right software that we need.

Web server

I will assume that you have the Apache + PHP + MySQL web server installed and running. If not, I recommend installing the XAMPP package for beginners.

You will need PHP 7.2.5 or later for the current version of Laravel.

Subsequently, I would recommend some more advanced IDE for developing and working with the framework. The best choice is PhpStorm, which I use by myself and I will use it in this course. It will make work easier not only with the development itself, but also with other tasks such as the Git system. As a free alternative, you can also use NetBeans, which can also help with development unlike simpler editors. However, if we are serious about development, we should reach for more professional software.

Furthermore, for the installation itself, we will use perhaps the most common method of installation in PHP, namely the Composer tool. Laravel builds the installation and other extensions on it. Therefore, the composer is also necessary to download and install it or leave it all on PhpStorm, which also has built-in resources for working with this tool directly.

Also make sure you have the following PHP extensions installed and activated:

  • OpenSSL
  • PDO
  • Mbstring
  • Tokenizer
  • XML
  • Ctype
  • JSON
  • BCMath
  • Fileinfo

If you used the link above to install XAMPP, these extensions are automatically included. To find out if these extensions are installed and activated, you can just call the simple PHP function phpinfo() and go through the table.

phpinfo on Windows - Laravel Framework for PHP

As you can see in the picture, we already know that we have BCMath activated.

Creating a new project

We will start by creating a new project in the Laravel framework.

Installation

We will install Laravel via Composer, manually or via PhpStorm. Which method you choose is up to you.

Install manually using Composer

Composer is a third-party tool for installing and managing project dependencies in PHP in general. It will download the base of the Laravel application and all other necessary libraries. So if you already have Composer installed and ready, we can create a new Laravel project. The installer will check all the necessary dependencies. If some are missing, composer will install them (this may take a while).

Create a folder for the project, e.g. first-application, and then use the command line to run the command to create the application structure:

composer create-project --prefer-dist laravel/laravel first-application

Installation using PhpStorm

If you have decided to work in PhpStorm and you have everything installed, all that remains is to create the project itself. We will do this similarly to any other IDE, by clicking on the button to create a new project.

A setup wizard will appear in the newly opened window and we will select Composer Project as the project type. In the next steps:

  1. Fill in the name of the project, e.g. first-application with the path where it should be saved.
  2. Next set that you want to download Composer and select the PHP interpreter.
  3. Last but not least, we will choose what we will actually install using Composer. In our case, it will be the Laravel framework, specifically the laravel/laravel package. It contains the basic skeleton of the application together with the prepared components that are most commonly used, such as the user database table.
New project - Laravel Framework for PHP

If you are working with the PhpStorm IDE for the first time on a Windows operating system, it is possible that the installation will not let you go due to a missing PHP interpreter. Interpreter can be simply set
by opening the pane next to the drop-down menu and then adding the php.exe file via the drop-down menu of the plus icon at the top left corner. Then just close the pane and choose the added PHP interpreter from the drop-down menu. This process is described by the following GIF image:

Add PHP interpreter to PhpStorm - Laravel Framework for PHP

This will create the first project built on the Laravel framework with the PhpStorm IDE using the Composer tool.

The installation process can take a few minutes. Therefore, be patient.

Project launch

Once we have the project created with the Laravel framework installed, it's time to run it on our web server and see the result. For a launch, we have a choice of several options.

Option 1: Start using the embedded server

The easiest way to run a Laravel project requires no additional configuration. We run the project using the built-in web server. Just call the following command in the project folder whenever we want to work on our application:

php artisan serve

It may happen that your antivirus (especially Avast) will want to move the server.php file to the "virus chest" after loading the page. If this happens, restore it and be sure to add an exception for this file and the project folder.

Option 2: Launch with Laravel Homestead

Laravel offers the official Laravel Homestead package using the Vagrant environment. Laravel Homestead runs independently in a virtual machine, which can be a disadvantage for some as more RAM is needed and support for the virtual environment at all. On the other hand, you don't have to worry about anything and once you install it, configuring and adding new projects will be easy.

Unfortunately, explaining the installation of this environment and its configuration is beyond the scope of this course. You can read about Laravel Homestead in the official documentation, where the installation and configuration are described in detail.

Option 3: Start using the Apache server

It is clear that to run a project under an Apache server, the server needs to be running and the project folder located somewhere where Apache has access. What is not so clear is that we will need to set up a few redirects before starting.

Routing

First of all, it is necessary to realize that the directory structure of the project is built in such a way that its display takes place only after entering the public/ folder, where the index.php file is located. We will also set this folder as the root folder in Apache. As a bonus, we will set the URL address first-application.local as the access address for our project.

First you need to edit the file C:/xampp/apache/conf/extra/httpd-vhosts.conf for Windows (depending on where you installed XAMPP) or /etc/apache2/sites-available/000-default.conf for Linux. We will add the following code to the file:

<VirtualHost *:80>
    DocumentRoot "path/to/the/project/public"
    ServerName first-application.local
    <Directory />
        AllowOverride All
        Require all granted
        DirectoryIndex index.php
    </Directory>
</VirtualHost>

Then we also have to modify the hosts file with administrator rights, which can be found in C:/Windows/System32/drivers/etc/hosts for Windows or in /etc/hosts for Linux. Therefore, you must first run your text editor as an administrator to save the file. Then we just add the following line to it:

127.0.0.1       first-application.local

Now all you have to do is save and restart Apache (either via the XAMPP control panel or via the sudo service apache2 restart command in a Linux terminal).

Result

Whatever method you decided on to start the project, we come to today's result. After entering the appropriate URL into a web browser, a window similar to the one shown below will appear. For the embedded server, the URL will be http://127.0.0.1:8000/ and for Apache or Laravel Homestead http://first-application.local:

Home page of Laravel framework after installation - Laravel Framework for PHP

If you see this window, then you have done everything correctly. If for some reason you do not see this page, I recommend you to go through the instructions again or try another way to start. And if you really don't know where to go anymore, you can always use the comments below the article, when I or someone else can help you.

In the next lesson, The first application in Laravel, we'll start creating our first application in the Laravel framework. I will reveal that it will be a simple calculator, so you have something to look forward to!


 

Previous article
Introduction to Laravel framework for PHP
All articles in this section
Laravel Framework for PHP
Skip article
(not recommended)
The first application in Laravel
Article has been written for you by Lishaak
Avatar
User rating:
No one has rated this quite yet, be the first one!
Author is interested in programming mostly at web development, sometimes he does funny video edits from his vacations. He also loves memes and a lot of TV series :)
Activities