You can nudge your life in a new direction every day.

Creating a Laravel Project from Scratch on my Local Machine

Submitted by admin on Tue, 03/09/2021 - 17:16

Since I just bought a new hybrid gaming coding machine and  need to setup my developer environment, I thought I'd walk through setting up a new Laravel Project from scratch.

I am using Ubuntu 20.04.

If you are unsure you can check your Ubuntu version with the following command:

lsb_release -a

You will want to check if php is installed:

 php -v

Assuming it is not you will need to install php:

apt show php

sudo apt install php7.4 php-fpm

Using the second install package, php-fpm, will prevent apt from installing Apache2 which is unnecessary. It doesn't hurt anything if you do install Apache2 but why if you don't need it.

Install Composer

Composer is a package manager for PHP. 

You can install composer using apt, Advanced Package Tool, or do it using the download instructions on Composer's website.

At the time of this writing, if you use apt you will install Composer v1 where as if you follow the instructions on Composer's website you will download Composer v2.

$ apt show composer

Package: composer
Version: 1.10.1-1

$ apt install composer

Honestly either would be fine.

For what it's worth I chose to follow the install instructions on Composer's website. Warning: You should use the instruction as written here. This can and likely will be out of date.

 

$ cd ~

$ mkdir development

$ cd development

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

$ php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

$ php composer-setup.php

$ php -r "unlink('composer-setup.php');"

I moved my Composer PHAR file to  a directory that is part of your PATH and rename it so it is a executable command

sudo mv composer.phar /usr/local/bin/composer

I good way to check this is to move to another directory and run the following:

$ cd ..
$ composer --version
Composer version 2.0.11 2021-02-24 14:57:23

Installing necessary package

If you were to try to create a Laravel project from the command line at this point you'd run into a few errors that prevent the project from being created.

There are  a few dependencies you need to install

To resolve each dependency are error run the following:

The zip extension and unzip command are both missing, skipping.

$ sudo apt install zip unzip php-zip

require ext-mbstring * -> it is missing from your system. Install or enable PHP's mbstring extension

$ sudo apt-get install php-mbstring

require ext-dom * -> it is missing from your system. Install or enable PHP's dom extension.

$  sudo apt install php-dom

 

Create Laravel project with Composer

 

Alright we should be at a point where we can install a Laravel project with composer.

 

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

You can then start a PHP development server at http://localhost:8000 and launch your new Laravel application with this command:

php artisan serve

But we aren't done yet.

 

Install npm on your local machine

Most modern applications make use of either a separate front end framework or at the very least a Javascript components so you will need npm for Laravel to work it's magic. 

At the time of writing, the Node.js version included in the Ubuntu 20.04 repositories is 10.19.0, but we need v12 or higher

First let's check if Node.js and npm are installed, if not then we will need to install them

node -v
npm -v

 So how do we install node 12

Error: You are using an unsupported version of Node. Please update to at least Node v12.14

We will use the NodeSource repository. NodeSource. is a company focused on providing enterprise-grade Node support.

$  cd ~/development
$  curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
$  sudo apt install nodejs
$  node --version
v14.16.0
$  npm --version

6.14.11

Ok so now we can begin to use npm and Laravel Mix

$ sudo npm install
$  npm run dev

webpack compiled successfully