Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Ubuntu - Create a Laravel Project

Prerequisites

Install composer if you haven't already.

Install the necessary PHP extensions:

sudo apt install -y \
  openssl php8.0-curl libapache2-mod-php8.0 php8.0-mbstring \
  php8.0-mysql php8.0-xml php8.0-zip php8.0-pdo

Steps

Create folder for your project and navigate into it.

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

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.3|^8.0",
        "fideloper/proxy": "^4.4",
        "fruitcake/laravel-cors": "^2.0",
        "guzzlehttp/guzzle": "^7.0.1",
        "laravel/framework": "^8.40",
        "laravel/tinker": "^2.5"
    },
    "require-dev": {
        "facade/ignition": "^2.5",
        "fakerphp/faker": "^1.9.1",
        "laravel/sail": "^1.0.1",
        "mockery/mockery": "^1.4.2",
        "nunomaduro/collision": "^5.0",
        "phpunit/phpunit": "^9.3.3"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "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"
        ]
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

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: 20th May 2021
First published: 16th August 2018