Symfony on Bash on Ubuntu on Windows

“Has science gone too far?”

If you’re like me and prefer Windows for all your desktop activities, but Linux for your shell, you’ve probably explored options such as Cygwin or MinGW in the past. With the recent Windows 10 Anniversary Update however, Microsoft and Canonical made a very interesting yet awkwardly named alternative available: Bash on Ubuntu on Windows.

While most of the process of setting up Symfony and it’s dependencies will be identical as you would on Ubuntu, there are a few caveats you should be aware of.

This guide will take you through the process of setting up everything required for development with the Symfony framework using the Windows Subsystem for Linux (WSL).

 

Installing WSL & Bash

First the WSL needs to be installed. To be able to do this, you need to update to Windows 10 64-bit build 14393 or later.
Next enable developer mode (through Settings > Update & Security > For developers > Developer mode).
Now you should be able to install WSL either through the Windows Features GUI (Enable Windows Subsystem for Linux (beta or through the following PowerShell command (as administrator):

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Once WSL has been enabled, you can now download & install Bash and the Ubuntu user-mode image by typing the following in a regular command prompt:

lxrun /install

You will be prompted to confirm installation, and when it is finished it will ask you for a username and a password. You will need these to run elevated commands (using sudo).

For detailed instructions on this process, refer to this MSDN article.

To switch to the bash shell, you can simply type bash in a command prompt. It will start in the same directory. Alternatively, you can launch through the start menu, which will take you to your new home directory.

Once you are in the bash prompt, I recommend updating the currently installed packages by running:

sudo apt-get update
sudo apt-get upgrade

If you are getting messages about being unable to resolve hostname, you can either ignore these or fix them by editing /etc/hosts:

sudo nano /etc/hosts

And adding your hostname (replace with your hostname, case sensitive!):

127.0.0.1 localhost

 

Installing PHP

For development purposes we won’t require a full LAMP stack, so we will just install the necessary components.
Symfony requires PHP 5.4 or higher. In this tutorial, we will install the latest version of PHP 5, but you could also use a newer version. Using the following command will install the necessary PHP components:

sudo apt-get install php5-cli php5-intl

Using php -v you can check the installed version.

Symfony requires that the date.timezone option is set, which PHP 5 doesn’t do by default.
Open the php configuration file using

sudo nano /etc/php5/cli/php.ini

Search for date.timezone using CTRL+W, uncomment the line, and add your timezone. When finished the [Date] section should look similar to this:

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Europe/Brussels

 

Installing SQLite

Now that PHP has been installed, we can install a database engine to be able to use PHP Database Objects. This is optional if you don’t plan on using a database in your project, but required if you want to be able to run the demo project.

The demo project uses SQLite, which you can install using the following command:

sudo apt-get install sqlite php5-sqlite

 

Installing Composer

Another thing I’d definitely recommend installing would be composer. Composer manages dependencies for your Symfony projects. By default, the official instructions (https://getcomposer.org/download/) install composer in your current directory, and with the .phar extension. I recommend directly installing composer in your bin folder with no extension, so you can access it from anywhere by simply typing composer. To do this, make sure you are in your home directory (cd ~) and execute the following instructions:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
php -r "unlink('composer-setup.php');"

 

Installing Symfony

Symfony can be installed with the official Linux installer. Again make sure you are in your home directory, and issue the following commands according to the official setup guide to get the installer:

sudo curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony
sudo chmod a+x /usr/local/bin/symfony

Next, we will install the demo project. Be aware that while you now have a home directory (which lives under C:\Users\\AppData\Local\lxss) you should never modify any of the files outside of WSL! Because WSL uses a layer over your existing filesystem to support Linux metadata, modifying these files outside of the WSL environment will cause corruption.
So unless you prefer working in nano, Vim, or Emacs you should store your projects in a regular Windows folder so you can work using your favorite editor.

Create a folder using Bash or Windows, and navigate to it. WSL mounts your existing drives automatically, so if your projects folder is C:\Projects you can find it in Bash under /mnt/c/Projects.

TIP:  To easily launch Bash in the current folder SHIFT + Right Click it, select Open command window here, and simply type bash.

Next, we’re going to install the Symfony demo project. Doing this is also a great way to test if everything is correctly installed. Navigate to your projects folder, and simply type symfony demo to download and install the demo project. This might take a while.

To check that all requirements for using Symfony have been met, you can issue the command:

php symfony_demo/bin/symfony_requirements

You will likely see a warning that the intl ICU version installed is outdated, however you can safely ignore this.

To test, change into the project’s directory and start the build-in web server:

cd symfony_demo
php bin/console server:start

You should now be able to access the demo through http://localhost:8000/. To stop the server, type: php bin/console server:stop.

That’s it! Now you can create and debug Symfony projects the Linux way, all while using your favorite Windows editor.

TIP: If you want to change the default terminal in PhpStorm to Bash, go to File > Settings > Tools > Terminal and change Shell Path to C:\Windows\sysnative\bash.exe.

Leave a Reply

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