In this tutorial we will learn how to install laravel on your local machine.
How many ways you can install/run laravel project?
it is very crucial that before you get straight to installing laravel you ask your self first how I am going to run this project locally? This will help you better decide what is correct way of installing laravel project locally.
Followings are some of the methods that you can use to run laravel project locally:
- Run laravel app using local apache/nginx
- Run laravel app using docker
- Run laravel app using vagrant box
- Run laravel app using artisan serve command (using local php server)
Alright now that we know how we would run our app it can help us what things do we need to install before we begin installing laravel. If you are planning to run project using apache/nginx than you need to have either apache or nginx installed locally first.
If you are planning to run your app via docker then you would have to install docker before you install laravel app. If you are going to run your app via homestead or vagrant you have to install following components first:
- Virtual Box
- Vagrant
How to install Laravel?
Now, we can get straight to installing laravel app locally. You need to have following things installed before you install laravel:
- PHP 8
- Composer
If you do not know how to install composer locally click on the following tutorial:
Once you have composer installed globally on your operating system run 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 may also install Laravel by issuing the Composer create-project
command in your terminal:
composer create-project --prefer-dist laravel/laravel projectName
Running freshly install laravel project
Let's run our first laravel project using artisan command:
# change directory to laravel root folder cd ProjectName # run following command to run local php server php artisan serve
Install and Run laravel using Docker and Sail
Make sure you have docker installed before you run following command or it wont work. We will install laravel sail in order to run our laravel app in a container environtment.
Laravel Sail is a light-weight command-line interface for interacting with Laravel's default Docker development environment. Following command will run remote bash script locally and install laravel project 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
To learn more about different sail commands follow tutorial below:
How to install/run laravel using homestead or vagrant box?
Vagrant provides a simple, elegant way to manage and provision Virtual Machines, boxes are completely disposable. If something goes wrong, you can destroy and re-create the box in minutes!
You need to install following things first in order to run your laravel project on vagrant box:
Once you have above requirements installed 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 your Homestead.yaml
file indicates which Vagrant provider should be used: virtualbox
or parallels
:
provider: virtualbox
Thats is a basic setup to peform advance settings follow the tutorial provided below:
How to add proper permission to your laravel project?
Once you have newly created project you need to assign proper folder permissions to prevent any errors related to storage or logs folder.
Run following commands to setup 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
That is it. Hope you like my tutorial thank you for reading this article.