June 18, 2024

How to Install Symfony Framework in Linux

Feature Image

Symfony is one of the best full-stack PHP frameworks, with over six hundred thousand developers using it actively. Notably, it’s used in the development of many popular websites and web applications, including Spotify, Dailymotion, and Trivago.

In this tutorial, you’ll learn how to install the Symfony framework on your Linux system.

Features of Symfony

Symfony framework enables you to develop complex websites, web applications, and microservices on your Linux system rapidly by providing an advanced toolbox and tons of reusable components. Here are some of the best features of Symfony:

  • Open source, fast, flexible, and well-documented framework
  • A large active community of developers, testers, users, integrators, and more
  • Operates on the MVC (Model-View-Controller) model
  • Numerous reusable and decoupled Symfony components
  • Full-featured database classes
  • Utilizes Composer as its dependency manager
  • Organized directory structure
  • Flexible URI routing
  • Cache management and error logging
  • Object-Oriented Programming (OOP) architecture

Pre-requisites for Installing Symfony

As mentioned earlier, Symfony is a PHP framework, so you need to install PHP and some other dependencies on your system before installing it.

Firstly, add the PHP repository to the list of repositories in your system by running the following commands according to your Linux distro:

#debian-based system
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php
 
#Fedora-based system
sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y
sudo dnf install https://rpms.remirepo.net/fedora/remi-release-$(rpm -E %fedora).rpm -y
sudo dnf module enable php:remi-7.4 -y
Add PHP Repository to Linux distro

Next, update your system repositories and install PHP 8.2 along with other required dependencies:

# Debian-based system
sudo apt update
sudo apt install php php-cli php-common php-xml libpcre3 git zip unzip -y
 
# Fedora-based system
sudo dnf update
sudo dnf install php8.2 php8.2-cli php8.2-common php8.2-xml libpcre3 git zip unzip -y
Install PHP and other dependencies

This will install all the required packages on your system.

Note: Instead of PHP 8.2, you can install any higher PHP version.

Finally, to verify the installed PHP version on your system, run the php -v command:

Php Version

Installing Symfony on Linux

You need to install Symfony CLI and Composer to use the Symfony framework seamlessly.

Install Symfony CLI

To download and install Symfony, you can use either the wget or curl command:

# Install Symfony via wget
wget https://get.symfony.com/cli/installer -O - | bash
 
# Install Symfony via curl
curl -sS https://get.symfony.com/cli/installer | bash
Install Symfony

Next, add Symfony to your system’s PATH environment variable and apply the changes:

export PATH="$HOME/.symfony5/bin:$PATH"
source ~/.bashrc
Define Symfony PATH

This way, Symfony is installed and configured successfully.

Install Composer

Afterward, you should install Composer, the dependency manager for PHP, which you’ll use while developing your projects.

For this purpose, first download the Composer installer script and verify its integrity by matching the hash:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

Then, run the following commands to install Composer and remove its installer script from your system:

php composer-setup.php
php -r "unlink('composer-setup.php');"
Composer Installation

Finally, you can run the symfony check:req command to ensure that Symfony is all set for you to start developing your first web application:

Check Requirment

Here, you can observe in the above output that you’re all set for the development.

Create Your First Web Application

Before creating the first web application in Symfony, it’s always considered a good practice to configure git on your system. For this purpose, run the following git commands to configure your git username and email:

git config --global user.email "email_address"
git config --global user.name "full_name"
Config Git

To Keep in Mind: Replace your email and username in the above commands.

You’re finally ready to create your first Symfony web application.

For instance, to create a web application named “mte” you will run the below command:

Create web application

However, if you want to create a microservice or API instead of a web application, execute the following command:

After the successful creation of your project, navigate to its directory and start the Symfony server:

cd mte/
symfony server:start
start Symfony web Server

Notably, you can observe that the web server is listening at http://127.0.0.1:8000. Now, open your favorite web browser and access the server by entering this listening address.

Symfony web application welcome page

Finally, You can start coding the first page of your web application. Happy coding!

Debug your Web Application

During the development of a web application, you’ll require various tools, especially debugging tools, to improve your efficiency and productivity. Although Symfony offers some built-in debugging tools like Profiler, you have the freedom to integrate the best debugging tool with it to get a customized debugging experience.

Image credit: Nimrach Chaudhry. All alterations and screenshots by Nimrah Chaudhry.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Nimrah Chaudhry

With 2+ years of experience, I’m a technical writer holding a Bachelor’s in Software Engineering and Cyber Security Certification. I’m passionate about simplifying Linux complexities into bite-sized wisdom for the community.

Leave a Reply

Your email address will not be published. Required fields are marked *