Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Ubuntu - Create a Laravel Project

Steps

Create folder for your project and navigate into it.

PROJECT_NAME="my-project"
mkdir $PROJECT_NAME
cd $PROJECT_NAME

Install composer if you haven't already.

Install Laravel Through Composer

Initialize the project through composer.

composer create-project laravel/laravel . --prefer-dist

That will install the latest version of laravel. If you wish to install a specific version of laravel, use:

composer create-project laravel/laravel . 5.8 --prefer-dist

If running Ubuntu 18.04, you probably want to have the following packages installed: php7.2-cli php7.2-bcmath php7.2-mysql php7.2-xml php7.2-mbstring php7.2-json.

Composer.json File

You should now have the Laravel framework in place. I would recommend taking a look at the composer.json file (below) and adding your own packages under require at this point.

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "require": {
        "php": "^7.1.3",
        "fideloper/proxy": "^4.0",
        "laravel/framework": "5.8.*",
        "laravel/tinker": "^1.0"
    },
    "require-dev": {
        "beyondcode/laravel-dump-server": "^1.0",
        "filp/whoops": "^2.0",
        "fzaninotto/faker": "^1.4",
        "mockery/mockery": "^1.0",
        "nunomaduro/collision": "^2.0",
        "phpunit/phpunit": "^7.5"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    }
}

Test it Out!

If you have php installed locally, you can try it out immediately by running the php local webserver like so:

php -S localhost:8080 public/index.php

Then navigate to localhost:8080 in your browser and you should see this:

The reason for using port 8080 is that it doesn't require admin privileges. If you want, you can use prefix the command above with sudo and use port 80 instead.

Docker

I have created a publicly available dockerized version of Laravel that you can use to quickly get started and deployed if you like.

To run my dockerized version of Laravel, use the following commands:

git clone https://github.com/programster/dockerized-laravel.git
cd dockerized-laravel
composer update
cd docker
bash build.sh

Wait for it to finish building before running the container.

bash run-container.sh

References

Last updated: 16th February 2020
First published: 16th August 2018