Setting up Composer

This lesson is a short guide for Mac/Linux users. If you work on Windows 10, read the article Web developer environment on Windows about preparing your system for Laravel development.

Open a terminal and go to your home directory, for example:

cd /Users/<USER_NAME>/

Run the command below to download Composer. It creates a Phar (PHP Archive) file named composer.phar:

curl -sS https://getcomposer.org/installer | php

Now move composer.phar to /usr/local/bin/ and set permissions so you can run it without sudo every time:

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

You can also do the same with one command:

php composer-setup.php --install-dir=/usr/local/bin --filename=composer

Set permissions for Composer:

sudo chmod 755 /usr/local/bin/composer

Important: renaming composer.pharcomposer is required.

Creating an alias for Composer

Next, add an alias for Composer:

nano ~/.bash_profile

This opens a Nano editor where you can set user preferences. Add this line:

alias composer="php /usr/local/bin/composer"

Then run this so the changes take effect:

source ~/.bash_profile

Check that it works:

composer --version

If you see the version output, Composer is installed correctly.

Installing the Laravel installer

Run in the terminal:

composer global require laravel/installer

I got this error:

laravel/installer v3.0.1 requires ext-zip * -> the requested PHP extension zip is missing from your system.

Check your PHP version:

php -v
# for example: 7.3.9
brew install php@7.3
brew link php@7.3

On Linux use:

# install the zip extension matching your PHP version
sudo apt-get install php7.3-zip

After that there were more messages about symfony/console. I went into ~/.composer/, removed composer.lock and the vendor/symfony/filesystem/ folder, then set the version in composer.json:

{
    "require": {
        "symfony/filesystem": "^5.0",
        "laravel/installer": "^3.0"
    }
}

I ran composer global require laravel/installer again and it worked:


Important: Make sure your shell profile includes the path to Composer:

Creating a Laravel application

Create an app named blog. Run:

laravel new blog

A directory with project files should be created and Composer should run.

Configuring the site

Copy the example env file .env.example.env:

cd blog/ && cp .env.example .env

Generate a unique application key:

php artisan key:generate

Depending on your OS (Windows/Linux/Mac), start a local server. Below are two options: Valet on Mac and Artisan on Linux.

Installing Valet

Install Valet for a convenient Mac workflow:

composer global require laravel/valet

From your project directory run:

valet install   # install and verify Valet
valet link      # create a symlink so the site opens as blog.test
valet restart   # restart Valet

The site should open at http://blog.test.

Starting a local server with Artisan

In the blog folder run:

php artisan serve

Artisan starts the site on localhost by default:

Setting up the database

For simplicity I used a regular MySQL install:

brew install mysql
mysql.server start   # start the server

Alternatively, if you prefer MariaDB (I had issues getting MariaDB working smoothly):

brew uninstall mysql && brew install mariadb   # install
brew services start mariadb                    # start MariaDB on boot
mysql.server start                             # start MariaDB once