Installing Symfony2 on Ubuntu
Posted on October 23rd, 2015
Installing Symfony2 on Ubuntu
Symfony has grown to be one of the most sort after PHP frameworks. Symfony has many advantages compared to other frameworks such as CodeIgniter and Cake. Installing Symfony is relatively easy. This document will guide you through one of the couple of ways of installing Symfony in Ubuntu 14.04. Installing Symfony in Ubuntu as mentioned earlier is easy. Ubuntu comes with PHP preinstalled but is not the latest version. The following step will guide you through the installations of the framework and its dependencies.
- Installing PHP
Using Ubuntu’s package manager we will install the latest version of PHP; that is, PHP5.5. Open the terminal and type the following command:
$ sudo apt-get install php5
- Configure Coding Standards
It is very important to configure the Symfony2 coding standards which are based on the PSR PHP coding standards which are supposed to make coding easy for you. Following the standards, it is necessary to install the PHP code sniffer library:
$sudo apt-get install php-codesniffer
Once code sniffer is installed; go to OpenSKy github repository where they’ll show you how to configure the Symfony2 coding standards: https://github.com/opensky/Symfony2-coding-standard Step One: We install Git, this is so as to be able to clone their repository$ sudo apt-get install git
Step Two: after installing Git, clone their repo into the php code sniffer root. From the terminal, navigate to the code sniffer root
$ cd /usr/share/php/PHP/CodeSniffer/Standards/
From the code sniffer root clone the repo
$ sudo git clone git://github/opensky/Symfony2-coding-standard.git Symfony2
Step Three: Next we need to install a few more dependencies such as Intl and Xdebug. To do this we will use the apt search tool to search for PHP5
$ sudo apt-cache search php5
The package we are looking for is php5-xdebug, locate it and install it
$ sudo apt-get install php5-xdebug
Also, install the Intl dependency
$ sudo apt-get install php5-intl
Next we set Symfony2 as the default coding standard
$ sudo phpcs –-config-set default_standard Symfony2
Once this is done then we are ready to move to the next step. - Installing Composer
https://getcomposer.org/download/
Composer is a dependency manager for PHP that controls our library versions and their sources. Before we install composer we need to install Curl
$ sudo apt-get install curl
Before we install composer, we will need to install the php json extension, you can search it by typing:
$ sudo apt-cache search php5
locate the php json extension and install it
$ sudo apt-get install php-json
Then install composer, you can copy it from the composer download page, the command below:
$ curl -sS https://getcomposer.org/installer | php
This will install composer to the home directory but we need to move it to the global directory using the following command
$ sudo mv composer.phar /usr/local/bin/composer
Voila! We are now ready to go - Creating a new project
Next we want to create a new project using composer and set it to the latest Symfony release while extending the long term support
Change to the public folder
$ cd Public
Create project
$ composer create-project symfony/framework-standard-edition projectName “2.5.*”
Note: At the time of writing this tutorial, Symfony 2.5 was the latest release.
Composer will download and install the latest Symfony and give it our project name. When this is done it will asks us to set our parameters {database driver, host, ports, database name, password and mailer} from the parameters.yml file. For the purposes of this tutorial we will accept the default values, for real projects however, it is recommended that we change these values to suit our project set up including the secret token. To accept the default values, just press enter upon each prompt.
Voila!We are now ready to code with our empty project ready and set up. Hold up, before we start coding I recommend you use PhpStorm. PhpStorm is an intelligent PHP IDE that supports PHP5.5; it is a product of jetBrains. - Setting up PhpStorm
PhpStorm requires Open JDK to work; we’ll install the jdk:
$ sudo apt-get install openjdk-7-jdk
There is just a few more things that we need to do so us to enjoy the benefits of this powerful IDE:
– Disable spelling – this will save us the unnecessary code markups
Search for spelling as indicated by the first red balloon, then check the boxes in spelling as per the second red balloon
– Exclude some directories from indexing – app/cache and app/logs
– Select the PHP interpreter
– Set up PHP code sniffer – to enable the IDE to validate our code against the Symfony2 coding standards
Our IDE is ready - Starting the web server
Once we have installed everything we can now start the web server. If we have just installed PHP, like in our case, first we need to set the time zone. To do this open the PHP config file:
$ sudo gedit /etc/php5/cli/php.ini
search for time zone
Then uncomment the line by removing the semi-colon at the beginning of the line. We then set our default time zone. You can check from http://php.net/manual/en/timezones.php for a list of standard time zone. In our case we’ll choose Belem date.timezone = America/Belem
Now, start the server
$ php app/console server: run
There is just a few more things we need to do. Once the server is running, we can test our project, however we need to tweak:
– Xdebug nesting limit to 250
$ sudo gedit /etc/php5/cli/conf.d/20-xdebug.ini
Add the option: xdebug_max_nesting_level = 250, the save and quit
– Install PDO drivers
Search for available drivers:
$ sudo apt-cache search pdo
locate php5-mysql
$ sudo apt-get install php5-mysql
Hurray!!! The development environment is now ready for Symfony. Restart the server:
$ php app/console server:run