Skip to content
justgreatwebsites.com
User Avatar
May 6th, 2023
·
9 min read

Install Statamic using Composer and Laravel Sail

Here is a simplified method of installing Statamic, based on the insightful YouTube video by Jonas Siewertsen, the creator of statamictutorials.com and several noteworthy Statamic plugins such as Statamic LiveWire and Statamic Live Search. Jonas' approach diverges from those mentioned in Statamic's official documentation, and I personally found it to be the most streamlined process for getting Statamic up and running. We will utilize tools like Composer (a PHP dependency manager), Docker Desktop, and Laravel Sail.

For this tutorial, I am using Ubuntu within the Windows Subsystem for Linux (WSL2) on Windows 11. WSL enables developers to install Linux distributions (e.g., Ubuntu) and access Linux applications, utilities, and Bash command-line tools directly on Windows.

Please note that all commands will be executed within a Bash terminal session and require both PHP and Composer. Before proceeding, ensure that Ubuntu has both PHP and Composer installed.

Here's an outline of the essential steps for setting up your first Laravel/Statamic application using Laravel Sail and Docker:

  1. Clone the default Statamic repository

  2. Add Laravel Sail as a requirement

  3. Install Laravel Sail

  4. Configure a few settings, and you're all set!

Install Docker

If you haven't already, visit docker.com to download, install, and launch the Docker Desktop app. On Windows this is very straightforward, however if you’re running an OS like Linux Mint, I recommend installing Docker Engine instead as it provides the best experience on Linux. You can follow along with Docker’s own documentation on how to Install Docker Engine for your particular Linux distro.

Update system packages

Before starting the installation, ensure your system's package lists are updated to avoid potential issues. Run sudo apt-get update to refresh the list of available packages. If there are any outdated packages, consider upgrading them with sudo apt-get upgrade. This step helps prevent conflicts and ensures a smoother installation process.

Create a Statamic Application with Composer

The Laravel documentation offers helpful information on using Composer. Instead of using composer create-project laravel/laravel example-app as the docs suggest, we'll employ a slightly different command that includes statamic/statamic for creating our Statamic application. Open your terminal and enter the following command, replacing site_name with your desired application name—this command will also create a project folder with the same name.

composer create-project statamic/statamic site_name

Require and Install Laravel Sail

Next, we'll install Laravel Sail into our existing application. For more information, refer to the Laravel documentation on Installing Sail Into Existing Applications. We can cd into our new project folder then run the following:

composer require laravel/sail --dev
php artisan sail:install

Select 0 for MySQL (even though we won't be using it in this case, which we'll address later). The install process takes a while, so go grab yourself a snack.

Configuring Laravel Sail

Once the install has completed, open the Statamic documentation on "How to Install Statamic with Docker" and navigate to the section titled Statamic Docker-Composer File. Copy the example code for the docker-compose.yml file and replace your local file's default contents (essentially, we're removing unnecessary components like MySQL).

The docs then instruct you to run ./vendor/bin/sail up, but we'll create an alias for the sail up command first (see below) to avoid entering the full path each time.

Create an Alias for the sail up Command

Edit your .bashrc file and add the following:

# Laravel Sail
alias sail='[ -f sail ] && sh sail || sh vendor/bin/sail'

Reload your Bash configuration by entering the command source ~/.bashrc. Now you can simply run sail up without entering the full path. Later, to close your Laravel Sail instance and remove your Docker container, use the sail down command.

Change the Default Port inside docker-compose.yml

Edit the code and replace ${APP_PORT:-80} with a port of your choice, such as 8090:80 (in my case, the default port was already in use by Apache server). Now, if you run sail up -d (the -d is for "detached" so the process runs in the background) and visit localhost:8090 in your browser, you should see your new Statamic app.

Optional — Change PHP versions

Inside your docker-compose.yml file, you'll find something like the following:

services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.2

To view the supported runtimes, go to /vendor/laravel/sail/runtimes within your project files, where they will be listed. If you change the runtime, you'll need to run sail up --build to rebuild the Docker container (this may take some time).

How to Run Commands Using the Statamic CLI

Since we're executing commands within a Laravel Sail instance, use sail artisan (not php please or php artisan as mentioned in the Statamic docs) to display a list of supported commands with the Statamic CLI. For instance, to create a user, run sail artisan statamic:make:user.

Statamic Installation Recap

To wrap up, this post has shown you a more straightforward way to install Statamic using Composer and Laravel Sail. We've gone through the steps of setting up Docker, creating a Statamic site with Composer, and getting Laravel Sail configured. We also touched on optional stuff like changing PHP versions and running commands with the Statamic CLI.

By following this guide, you should now have a fully functional Statamic application up and running using Laravel Sail and Docker. This easier approach lets you spend less time on setup and more time on building awesome features for your app. I hope you enjoyed this tutorial and found it useful. Happy coding!