Installing & Setting up the Symfony Framework
Screencast
Do you prefer video tutorials? Check out the Stellar Development with Symfonyscreencast series.
Technical Requirements
Before creating your first Symfony application you must:
- Install PHP 7.1 or higher and these PHP extensions (which are installed andenabled by default in most PHP 7 installations): Ctype, iconv, JSON,PCRE, Session, SimpleXML, and Tokenizer;
- Install Composer, which is used to install PHP packages;
- Install Symfony, which creates in your computer a binary called
symfony
that provides all the tools you need to develop your application locally.Thesymfony
binary provides a tool to check if your computer meets theserequirements. Open your console terminal and run this command:
- $ symfony check:requirements
Creating Symfony Applications
Open your console terminal and run any of these commands to create a new Symfonyapplication:
- # run this if you are building a traditional web application
- $ symfony new my_project_name --full
- # run this if you are building a microservice, console application or API
- $ symfony new my_project_name
The only difference between these two commands is the number of packagesinstalled by default. The —full
option installs all the packages that youusually need to build web applications, so the installation size will be bigger.
If you can't or don't want to install Symfony for any reason, run thesecommands to create the new Symfony application using Composer:
- # run this if you are building a traditional web application
- $ composer create-project symfony/website-skeleton my_project_name
- # run this if you are building a microservice, console application or API
- $ composer create-project symfony/skeleton my_project_name
No matter which command you run to create the Symfony application. All of themwill create a new my_project_name/
directory, download some dependenciesinto it and even generate the basic directories and files you'll need to getstarted. In other words, your new application is ready!
Note
The project's cache and logs directory (by default, <project>/var/cache/
and <project>/var/log/
) must be writable by the web server. If you haveany issue, read how to set up permissions for Symfony applications.
Running Symfony Applications
On production, you should use a web server like Nginx or Apache (seeconfiguring a web server to run Symfony).But for development, it's more convenient to use thelocal web server provided by Symfony.
This local server provides support for HTTP/2, TLS/SSL, automatic generation ofsecurity certificates and many other features. It works with any PHP application,not only Symfony projects, so it's a very useful development tool.
Open your console terminal, move into your new project directory and start thelocal web server as follows:
- $ cd my-project/
- $ symfony server:start
Open your browser and navigate to http://localhost:8000/
. If everything isworking, you'll see a welcome page. Later, when you are finished working, stopthe server by pressing Ctrl+C
from your terminal.
Setting up an Existing Symfony Project
In addition to creating new Symfony projects, you will also work on projectsalready created by other developers. In that case, you only need to get theproject code and install the dependencies with Composer. Assuming your team usesGit, setup your project with the following commands:
- # clone the project to download its contents
- $ cd projects/
- $ git clone ...
- # make Composer install the project's dependencies into vendor/
- $ cd my-project/
- $ composer install
You'll probably also need to customize your .env fileand do a few other project-specific tasks (e.g. creating a database). Whenworking on a existing Symfony application for the first time, it may be usefulto run this command which displays information about the project:
- $ php bin/console about
Installing Packages
A common practice when developing Symfony applications is to install packages(Symfony calls them bundles) that provide ready-to-usefeatures. Packages usually require some setup before using them (editing somefile to enable the bundle, creating some file to add some initial config, etc.)
Most of the time this setup can be automated and that's why Symfony includesSymfony Flex, a tool to simplify the installation/removal of packages inSymfony applications. Technically speaking, Symfony Flex is a Composer pluginthat is installed by default when creating a new Symfony application and whichautomates the most common tasks of Symfony applications.
Tip
You can also add Symfony Flex to an existing project.
Symfony Flex modifies the behavior of the require
, update
, andremove
Composer commands to provide advanced features. Consider thefollowing example:
- $ cd my-project/
- $ composer require logger
If you execute that command in a Symfony application which doesn't use Flex,you'll see a Composer error explaining that logger
is not a valid packagename. However, if the application has Symfony Flex installed, that commandinstalls and enables all the packages needed to use the official Symfony logger.
This is possible because lots of Symfony packages/bundles define "recipes",which are a set of automated instructions to install and enable packages intoSymfony applications. Flex keeps tracks of the recipes it installed in asymfony.lock
file, which must be committed to your code repository.
Symfony Flex recipes are contributed by the community and they are stored intwo public repositories:
- Main recipe repository, is a curated list of recipes for high quality andmaintained packages. Symfony Flex only looks in this repository by default.
- Contrib recipe repository, contains all the recipes created by thecommunity. All of them are guaranteed to work, but their associated packagescould be unmaintained. Symfony Flex will ask your permission before installingany of these recipes.Read the Symfony Recipes documentation to learn everything about how tocreate recipes for your own packages.
Symfony Packs
Sometimes a single feature requires installing several packages and bundles.Instead of installing them individually, Symfony provides packs, which areComposer metapackages that include several dependencies.
For example, to add debugging features in your application, you can run thecomposer require —dev debug
command. This installs the symfony/debug-pack
,which in turn installs several packages like symfony/debug-bundle
,symfony/monolog-bundle
, symfony/var-dumper
, etc.
By default, when installing Symfony packs, your composer.json
file shows thepack dependency (e.g. "symfony/debug-pack": "^1.0"
) instead of the actualpackages installed. To show the packages, add the —unpack
option wheninstalling a pack (e.g. composer require debug —dev —unpack
) or run thiscommand to unpack the already installed packs: composer unpack PACK_NAME
(e.g. composer unpack debug
).
Checking Security Vulnerabilities
The symfony
binary created when you install Symfony provides a command tocheck whether your project's dependencies contain any known securityvulnerability:
- $ symfony check:security
A good security practice is to execute this command regularly to be able toupdate or replace compromised dependencies as soon as possible. The securitycheck is done locally by cloning the public PHP security advisories database,so your composer.lock
file is not sent on the network.
Tip
The check:security
command terminates with a non-zero exit code ifany of your dependencies is affected by a known security vulnerability.This way you can add it to your project build process and your continuousintegration workflows to make them fail when there are vulnerabilities.
Symfony LTS Versions
According to the Symfony release process,"long-term support" (or LTS for short) versions are published every two years.Check out the Symfony roadmap to know which is the latest LTS version.
By default, the command that creates new Symfony applications uses the lateststable version. If you want to use an LTS version, add the —version
option:
- # use the most recent 'lts' version
- $ symfony new my_project_name --version=lts
- # use the 'next' Symfony version to be released (still in development)
- $ symfony new my_project_name --version=next
- # use a specific Symfony version
- $ symfony new my_project_name --version=3.3.10
- $ symfony new my_project_name --version=4.3.1
The Symfony Demo application
The Symfony Demo Application is a fully-functional application that shows therecommended way to develop Symfony applications. It's a great learning tool forSymfony newcomers and its code contains tons of comments and helpful notes.
Run this command to create a new project based on the Symfony Demo application:
- $ symfony new my_project_name --demo
Start Coding!
With setup behind you, it's time to Create your first page in Symfony.
Learn More
- Using Symfony with Homestead/Vagrant
- Configuring a Web Server
- How to Use PHP's built-in Web Server
- Upgrading a Third-Party Bundle for a Major Symfony Version
- Setting up or Fixing File Permissions
- Upgrading Existing Applications to Symfony Flex
- Symfony Local Web Server
- How to Install or Upgrade to the Latest, Unreleased Symfony Version
- Upgrading a Major Version (e.g. 3.4.0 to 4.1.0)
- Upgrading a Minor Version (e.g. 4.0.0 to 4.1.0)
- Upgrading a Patch Version (e.g. 4.1.0 to 4.1.1)