Learn How to Install Laravel On Ubuntu Mac Step By Step with this guide.
If you are beginner on laravel and want to install laravel on your mac or ubuntu linux machine then this tutorial perfectly fits for your requirements.
For installing laravel on mac or ubuntu, we require PHP and composer on our machine.
So follow all the below steps to create your first basic project on laravel.
Step 1. Checking PHP version
First of all, let us check current installed php version on your machine. So open up your terminal.
Inside it, write down the following command and hit enter.
php -v
This command will give us the current version of the php. In my case I got the following results.
PHP 7.2.14 (cli) (built: Jan 12 2019 05:22:49) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.14, Copyright (c) 1999-2018, by Zend Technologies
So, I have version number 7.2.14 on my system. For installing laravel, we need to have minimum version of PHP as described in the official documentation.
To check official recommended version, click here to go to official documentation.
If your machine have older version than the official recommendations, then you need to update php version. Otherwise no need to worry.
How to update PHP ?
You can have several ways to update your php version. For example, using homebrew or may be directly from the terminal.
For mac
If you are using Apple Mac, then I highly recommend you to use homebrew for updating PHP.
Go to the homebrew page using this link. Here, you will find one command to install homebrew on your mac.
Following is the command
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Hit above command on your terminal and press enter. System will download homebrew and will install it automatically.
After this, type the below command to install PHP version 7.1,
brew install php@7.1
You can replace 7.1 with the version number, you want to install.
Now again type command php -v . This time you should get the proper php version. If not then do little research on internet and try until you get proper php version in the results of php -v
For Ubuntu or Linux
To install php 7.3 version, type the below command in terminal
sudo apt-get install python-software-properties sudo add-apt-repository ppa:ondrej/php sudo apt-get update sudo apt-get install -y php7.3
You in the last line, you can replace 7.3 with desired version.
Again check php version with help of, php -v command.
It is necessary to get greater or equal php version then the official recommendation in the results of “php -v” command.
Once your php version is ok with official version of laravel documentation, you are ready to install composer.
Step 2. Installing Composer
First of all, download the composer using the below command
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php -r "if (hash_file('sha384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" php composer-setup.php php -r "unlink('composer-setup.php');"
You can check the official documentation for downloading composer here.
You should get the below lines when you have successfully installed composer.
Composer (version 1.8.0) successfully installed to: Users/hardik/code/composer.phar Use it: php composer.phar
Now let us put our composer in the global directory. For this, run the below command
mv composer.phar /usr/local/bin/composer
You can find this command on this official link.
Now if you fire the below command
composer
then you should get the below output
➜ ~ composer ______ / ____/___ ____ ___ ____ ____ ________ _____ / / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/ / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/ /_/ Composer version 1.8.0 2018-12-03 10:31:16 Usage: command [options] [arguments] Options: -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question --profile Display timing and memory usage information --no-plugins Whether to disable plugins. -d, --working-dir=WORKING-DIR If specified, use the given directory as working directory. -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug Available commands: about Shows the short information about Composer. archive Creates an archive of this composer package. browse Opens the package's repository URL or homepage in your browser. check-platform-reqs Check that platform requirements are satisfied. clear-cache Clears composer's internal package cache. clearcache Clears composer's internal package cache. config Sets config options. create-project Creates new project from a package into given directory. depends Shows which packages cause the given package to be installed. diagnose Diagnoses the system to identify common errors. dump-autoload Dumps the autoloader. dumpautoload Dumps the autoloader. exec Executes a vendored binary/script. global Allows running commands in the global composer dir ($COMPOSER_HOME). help Displays help for a command home Opens the package's repository URL or homepage in your browser. i Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json. info Shows information about packages. init Creates a basic composer.json file in current directory. install Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json. licenses Shows information about licenses of dependencies. list Lists commands outdated Shows a list of installed packages that have updates available, including their latest version. prohibits Shows which packages prevent the given package from being installed. remove Removes a package from the require or require-dev. require Adds required packages to your composer.json and installs them. run-script Runs the scripts defined in composer.json. search Searches for packages. self-update Updates composer.phar to the latest version. selfupdate Updates composer.phar to the latest version. show Shows information about packages. status Shows a list of locally modified packages, for packages installed from source. suggests Shows package suggestions. u Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file. update Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file. upgrade Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file. validate Validates a composer.json and composer.lock. why Shows which packages cause the given package to be installed. why-not Shows which packages prevent the given package from being installed.
Ok, so till now, we have got the latest php version and have installed composer.
Step 3. Installing the Laravel
Write the below command in terminal
composer global require laravel/installer
It will take little bit of time to download and install laravel.
Once this process is finished, we need to set up environment variable $PATH.
$PATH on Mac
If you are using mac, then your $PATH should like this according to documentation
$HOME/.composer/vendor/bin
Now I am using oh my zsh in my terminal. If you are also using this then run below command
sudo vi ~/.zshrc
If you are not using oh my zsh, then you can try something like
sudo vi ~/.bashrc or sudo vi ~/.bash_profile
Once you hit above command, you will be asked for password and then a text something like below will be opened
# If you come from bash you might have to change your $PATH. # export PATH=$HOME/bin:/usr/local/bin:$PATH # Path to your oh-my-zsh installation. export ZSH="/Users/hardikparsania_mac/.oh-my-zsh" # Set name of the theme to load --- if set to "random", it will # load a random theme each time oh-my-zsh is loaded, in which case, # to know which specific one was loaded, run: echo $RANDOM_THEME # See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes ZSH_THEME="robbyrussell" # Set list of themes to pick from when loading at random # Setting this variable when ZSH_THEME=random will cause zsh to load # a theme from this variable instead of looking in ~/.oh-my-zsh/themes/ # If set to an empty array, this variable will have no effect. # ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" ) # Uncomment the following line to use case-sensitive completion. # CASE_SENSITIVE="true" # Uncomment the following line to use hyphen-insensitive completion. # Case-sensitive completion must be off. _ and - will be interchangeable. # HYPHEN_INSENSITIVE="true" "~/.zshrc" 100L, 3603C
Now press i so that you will be able to edit above file.
Write down below line
export PATH=$HOME/.composer/vendor/bin:$PATH
below # export PATH=$HOME/bin:/user………
Then press Esc (escape) key, so that you can save the changes. For saving changes, press (:wq) this will written at the end of the file.
So new text should look like below
# If you come from bash you might have to change your $PATH. # export PATH=$HOME/bin:/usr/local/bin:$PATH export PATH=$HOME/.composer/vendor/bin:$PATH # Path to your oh-my-zsh installation. export ZSH="/Users/hardikparsania_mac/.oh-my-zsh" # Set name of the theme to load --- if set to "random", it will # load a random theme each time oh-my-zsh is loaded, in which case, # to know which specific one was loaded, run: echo $RANDOM_THEME # See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes ZSH_THEME="robbyrussell" # Set list of themes to pick from when loading at random # Setting this variable when ZSH_THEME=random will cause zsh to load # a theme from this variable instead of looking in ~/.oh-my-zsh/themes/ # If set to an empty array, this variable will have no effect. # ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" ) # Uncomment the following line to use case-sensitive completion. # CASE_SENSITIVE="true" # Uncomment the following line to use hyphen-insensitive completion. :wq
You can see that I have added a line below # export PATH=$HOME/bin:/user……… and also :wq is there at the end.
Now press enter and your $PATH is now updated with composer.
$PATH on Ubuntu
If you are using mac, then your $PATH should like this according to documentation
$HOME/.config/composer/vendor/bin
Now hit following command
sudo nano .bashrc
Then system will ask you for password and then it will show you some texts.
At the end of this text, type
export PATH=”$PATH:$HOME/.config/composer/vendor/bin”
and then save.
Step 4. What if I am facing issue in environmental variable $PATH
Setting this variable is the little tricky process for both mac and ubuntu.
If you are facing issues then there is one solution for this.
Whenever you open your terminal simply run the below command on mac
export PATH=$HOME/.composer/vendor/bin:$PATH
and if you are on ubuntu then run
export PATH="$PATH:$HOME/.config/composer/vendor/bin"
Now you should be able to create new laravel project.
One drawback of this solution is that our settings will be destroyed when you close your terminal. So you need to repeat this process every time you open new session of terminal.
Now let us check if laravel is successfully installed or not. For this, simply type
laravel and hit enter.
You should get below lines
➜ ~ laravel Laravel Installer 2.0.1 Usage: command [options] [arguments] Options: -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug Available commands: help Displays help for a command list Lists commands new Create a new Laravel application. ➜ ~
If you are getting above lines then congratulations, you have successfully installed laravel on your machine!!