Signup/Sign In
LAST UPDATED: SEPTEMBER 16, 2021

Setting Up the Laravel Project Environment

Technology #PHP#Laravel

    Laravel applications are installed and managed via Composer, which is a popular PHP dependency manager. In this article, we will learn about Composer, a tool used for dependency management, setting up Laravel project and running Laravel application.

    What is Composer?

    Composer is a tool for dependency management in PHP. It allows us to declare the libraries on which our project depends and it will manage(install/update) them for you.

    Composer is not a package manager in the same sense as Yum or Apt are. It deals with packages or libraries, but it manages them on a per-project basis, installing them in a directory inside our project. By default, it does not install anything globally.

    Composer requires PHP 5.3.2+ to run. A few sensitive PHP settings and compile flags are also required, but when using the installer we will be warned about any incompatibilities.

    To install packages from sources instead of simple zip archives, you will need git, svn, fossil or hg depending on how the package is version controlled.


    Installing Composer

    We can install Composer to a specific directory by using the --install-dir option and additionally rename it as well using the --filename option:

    For Linux :

    php composer-setup.php --install-dir=bin –filename=composer

    For Windows:

    Download and run Composer-Setup.exe file. It will install the latest Composer version and set up your PATH so that you can call composer from any directory in your command line.


    Creating a Laravel Project

    There are two ways to create a new Laravel application.

    1.Via use of Composer

    Following is the syntax:

    $ composer create-project laravel/laravel [foldername]

    Or use following syntax:

    $ composer create-project --prefer-dist laravel/laravel [foldername]

    Let's take a few examples using the above syntax,

    $ composer create-project laravel/laravel my_laravelapp1

    Or use following command,

    $ composer create-project --prefer-dist laravel/laravel my_laravelapp1

    Do replace [foldername] with the name of the directory you want your new Laravel application installed to. It must not exist before installation. You may also need to add the Composer executable to your system path. Composer executable helps to create the required prerequisites to run the Laravel application.

    If you want to create a Laravel project using a specific version of the framework, you can provide a version pattern, otherwise, your project will use the latest available version.

    If you wanted to create a project in Laravel 5.2 for example, you would run following:

    $ composer create-project --prefer-dist laravel/laravel 5.2.*

    You can change the version as per your requirement.

    Why use the --prefer-dist argument?

    There are two ways of downloading a package: source and dist. For stable versions Composer will use the dist by default. The source is a version control repository.

    If --prefer-source is enabled, Composer will install from source if there is one.

    Whereas --prefer-dist is the opposite of --prefer-source, and tells Composer to install from dist if possible. This can speed up installs substantially on build servers and in other use cases where you typically do not run vendor updates. It also allows for avoiding problems with GitHub if you do not have a proper setup.

    2.Via use of the Standard Laravel installer

    Laravel provides a helpful command-line utility to quickly create Laravel applications. First, install the installer by using the following instruction:

    $ composer global require laravel/installer

    You have to make sure that the Composer binaries folder is within your $PATH variable to execute the Laravel installer with the php pharfile.

    To check if it already is in your $PATH variable use the following command,

    echo $PATH

    If everything is correct, the output should look something like this:

    Users/yourusername/.composer/vendor/bin

    If not available then, edit your .bashrc or, if you're using ZSH, your .zshrc so it contains the path to your Composer vendor directory. This procedure should be followed by the Linux users or ubuntu users.

    Once installed, this command will create a fresh Laravel installation in the directory which you have specified at specific destination location.

    laravel new [foldername]

    For example:

    laravel new laravelapp1

    You can also use a . (a dot) in place of [foldername] to create the project in the current working directory without making a sub-directory.


    Running the Laravel application

    Laravel comes bundled with a PHP-based web server which can be started by running the following command,

    $ php artisan serve

    Here artisan serve is the default webserver to interpret the Laravel application files.

    By default, the HTTP server will use port 8000, but if the port is already in use or if you want to run multiple Laravel applications at once, you can use the --port flag to specify a different port, for example:

    $ php artisan serve --port=8080

    The HTTP server will use localhost as the default domain for running the application, but you can use the --host flag to specify a different address:

    $ php artisan serve --host=192.168.0.100 --port=8080

    Using a different server

    If you prefer to use a different web server software like XAMPP, WAMP, LAMP, MAMP, AMPS, IIS-7 then some configuration files are provided for you inside the public directory of your project, for example, .htaccess for Apache and web.config for ASP.NET etc. For other software such as NGINX, you can convert the Apache configurations using various online tools and then use them to run the server and the application.

    The framework also needs the web server user to have write permissions on the following directories:

    /storage and /bootstrap/cache

    On Unix based operating systems this can be achieved by the following command,

    sudo chown -R www-data:www-data storage bootstrap/cache
    
    sudo chmod -R ug+rwx storage bootstrap/cache


    Conclusion

    I hope this article helps you with the basic setup of Laravel project, setting up composer, creating your first Laravel project and running the Application. If you face any issue, do comment and let us know.

    You may also like:

    I am a Software Engineer and freelancer with 7 years of experience
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS