Installing Symfony

Create a Symfony 8.1 project with the Symfony CLI or Composer, check your PHP first, and choose minimal or webapp.

Syntaxsymfony new my_project --version="8.1.*" --webapp

Symfony ships as Composer packages, so installing a project is a Composer operation. The Symfony CLI is an optional extra: a single binary named symfony that wraps project creation, a local web server and environment checks. It is a developer tool on your machine, not a dependency of your application.

Check the machine first

symfony check:requirements reports whether your PHP can run Symfony. Symfony 8.1 requires PHP 8.4 or higher; this course uses PHP 8.5. The required extensions are Ctype, iconv, PCRE, Session, SimpleXML and Tokenizer.

One project, two commands

These produce the same project. The second is the longhand the first runs for you.

symfony new my_project --version="8.1.*"

composer create-project symfony/skeleton:"8.1.*" my_project

Minimal or webapp

The skeleton alone is small: a kernel, a front controller, configuration, a console. That suits an API, a worker or a console application. Adding --webapp installs the packages a browser-facing application needs as well — templating, forms, validation, security, database, profiler. Starting minimal costs nothing, because composer require webapp installs the same set later.

symfony new my_project --version="8.1.*" --webapp

composer create-project symfony/skeleton:"8.1.*" my_project
cd my_project
composer require webapp

What arrives

Composer writes vendor/, a .env file, bin/console and the directory layout. php bin/console about then prints the Symfony version, the environment and the PHP version actually in use.

Example

Example · php
// public/index.php
// Written by Composer when the skeleton is installed. Shown exactly as
// it arrives: this is the only PHP file a web server ever executes.

use App\Kernel;

// Created by the symfony/runtime Composer plugin. It builds a runtime,
// resolves the arguments of the closure below, calls it, and runs the
// returned application.
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

// $context carries the environment variables, so APP_ENV and APP_DEBUG
// decide which configuration the kernel loads.
return function (array $context): Kernel {
    return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
};

When to use it

  • Start a new Symfony 8.1 project pinned to that version rather than whatever is newest.
  • Diagnose a project that will not boot by checking the PHP version and extensions first.
  • Add templating and forms to a project that was created as a minimal API skeleton.

More examples

Checking requirements before and after creating the project

Run this when a project works on one machine and not another. The requirements check names the missing extension or the wrong PHP version directly, instead of letting it surface later as a confusing error inside a component.

Example · bash
# Before: is this machine able to run Symfony at all?
symfony check:requirements

# After creating a project, run it again from inside the project so it
# also reads the project's own constraints.
cd my_project
symfony check:requirements

# What the application itself reports about its environment.
php bin/console about

The generated .env file

The file every new project starts with. APP_ENV selects which configuration loads, so flipping it to prod locally is the quickest way to reproduce a production-only problem.

Example · bash
# .env
# Committed to the repository. It holds defaults, never secrets.

###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=
###< symfony/framework-bundle ###

# Machine-specific overrides go in .env.local, which is git-ignored.
# The ###> ###< markers are owned by Composer recipes: edit between
# them and a package update may rewrite your change.

Installing the webapp packages after the fact

Use this when a service that began as a JSON API grows a browser-facing admin area. Nothing has to be recreated; webapp is a metapackage whose only job is to require a list of other packages.

Example · bash
# Started minimal, now the project needs to render HTML pages.
composer require webapp

# Or pick only what is needed, one pack at a time.
composer require twig
composer require symfony/form symfony/validator
composer require --dev symfony/profiler-pack

Discussion

  • Be the first to comment on this lesson.