How many ways you can install/run a Laravel project?

The first step to start working on any Laravel project is to create and install a new Laravel app. There are various ways to do this. Let’s look over the best ways to install/run Laravel Project.

At times we need to run our project locally, sometimes on server, or on a different operating system like Mac, where we need to install with docker. So, there are multiple methods to install and run Laravel to fulfill these goals.

  • Run Laravel app using local apache/nginx
  • Run Laravel app using docker
  • Run Laravel app using artisan serve command (php local server)
  • Run Laravel app using vagrant box

We now know the various ways we can install our Laravel app. Let’s come to the perquisites required for installation. To run a Laravel project on local or apache (localhost) server, we should first have the apache or nginx installed in our system.

To run the Laravel app on docker, you need to first install the docker.

For running the app using vagrant box, you need to first install the following components.

  • Vagrant
  • Virtual Box

How to install Laravel?

To install a Laravel application locally, you need to have following things installed in your system:

  • PHP 8
  • Composer

Once you have composer installed globally on your operating system, you can run the following commands:

# install laravel installer
composer global require laravel/installer

# once composer installs laravel dependency you need
# to make sure you are able to run laravel command
# let's add laravel to our path variable
sudo nano ~/.bash_profile

# add following line to your open file for linux users
export PATH=$HOME/.config/composer/vendor/bin:$PATH

# for mac user add following line
$HOME/.composer/vendor/bin

# save the changes and source the file
sudo source ~/.bash_profile

# close the terminal and re-open and run following command
laravel new projectName

# if some reason above command does not work try this
$HOME/.composer/vendor/bin/laravel new projectName

Alternatively, you can also install Laravel by using the Composer create-project command in your terminal as shown below:

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

Running newly installed Laravel project

To run a newly installed Laravel project locally, you need to navigate to your project’s directory and pass the artisan command as shown below.

# change directory to laravel root folder
cd ProjectName

# run the command below to start local php server
php artisan serve

Install Laravel using Docker and Sail

Note: ensure to have docker and Laravel Sail installed that helps set up a docker environment in Laravel project.

Laravel Sail is a light-weight command-line interface which helps to interact with Laravel’s default Docker development environment. The command shown below will locally run a remote bash script and install Laravel using Sail.

# install laravel using curl
# change example-app to your project name
curl -s "https://laravel.build/example-app" | bash

# once installed run docker containers using
./vendor/bin/sail up

# to destroy running container run
./vendor/bin/sail down

Install Laravel using Homestead or Vagrant box

Vagrant provides a simple and elegant way to manage and use Virtual Machines, the boxes here are completely disposable. If something goes wrong, you can destroy and re-create a box in minutes!

Once you have requirements stated in section 1 too use vagrant box, you can install homestead using following command:

# download remote repo locally
git clone https://github.com/laravel/homestead.git ~/Homestead

# change directory to homestead
cd ~/Homestead

# checkout latest version
git checkout release

// macOS / Linux...
bash init.sh

// Windows...
init.bat​

The provider key in a Homestead.yaml file indicates which Vagrant provider should be used, i.e. virtualbox or parallels:

provider: virtualbox

This helps create a basic set up to use virtual or vagrant box to install Laravel.

Adding proper permissions to Laravel Project

After creating a new Laravel project, you can assign proper folder permissions that prevents any errors related to storage or logs.

The following commands helps to to set correct project permissions:

# Using ACL on a System that Supports chmod +a (macOS)
HTTPDUSER=$(ps axo user,comm | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\  -f1)
sudo chmod +a "$HTTPDUSER allow delete,write,append,file_inherit,directory_inherit" storage
sudo chmod +a "$(whoami) allow delete,write,append,file_inherit,directory_inherit" storage

# Using ACL on a System that Supports setfacl (Linux/BSD)
HTTPDUSER=$(ps axo user,comm | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\  -f1)
sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX storage
sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX storage

Hope you like my tutorial and understood well the various ways to install a Laravel Application!