Installing Symfony in MAMP on OS X

Jun 28, 2010   //   by the guru   //   Mac, MAMP, PHP, Symfony  //  14 Comments

At the time of writing I am on a MacBook Pro with OS X 10.5.8, MAMP PRO 1.9 and Symfony 1.4.

MAMP? Have you got it and is it installed? If not why not? MAMP is for me the best development environment (Mac, Apache, MySQL, PHP) for the Mac and for £20 you can’t go wrong. It installs with minimum fuss and gives you a pretty interface to manage your local sites.

If you need MAMP in your life visit the MAMP website.

I will now assume you have MAMP installed and we are ready to go. I will also assume that you know of the Symfony PHP framework as you are here reading this. If you need more information on it visit the Symfony website. All I will say in this article is rapid web development!

Before you do anything it is very important that we rejig the PHP libraries used by MAMP. MAMP uses its own libraries and not the standard libraries installed as default in Mac OS X. This may seem trivial, but if you want more that just PHP or if you want to add a number of PHP extensions like Imagick for example then this is critical. The issues boil down to the different versions using 32bit and 64bit compilations. I discuss this in more details in “Installing Image Magick and Imagick for PHP for MAMP”.

1. Creating a Host

Once you have MAMP up and running make sure you have setup a new host for the site.

MAMP Pro Hosts Page

MAMP Pro Hosts Page

When you visit your new site in your Browser you should be able to see a standard MAMP page.

2. Moving PHP

Firstly move the current PHP to a different location (we don’t need to delete it altogether):

sudo mv /usr/bin/php /usr/bin/php-old

Sudo is a command used to perform operations with the security privileges of the super user and is required for a lot of the main system changes we will be making. You will need to enter your login password when requested by terminal.

Now create a link to MAMPs PHP folder in the /usr/bin/php folder. This uses symlinks (or symbolic links) and tricks the system into thinking the PHP is there without moving MAMPs PHP files.

sudo ln -s /Applications/MAMP/bin/php5.3/bin/php /usr/bin/php

Please note that I have installed MAMP and configured it to use PHP 5.3. If you have not chosen to use version 5.3 just replace that in the command with whatever you have used (probably php5).

3. Moving PEAR

Move the current installation of PEAR to a different location:

sudo mv /usr/bin/pear /usr/bin/pear-old

You may not have PEAR installed in which case skip the above step. You will still need to setup the link below though.

sudo ln -s /Applications/MAMP/bin/php5.3/bin/pear /usr/bin/pear

At this point it will be also worth doing some house cleaning! Update your PEAR channel by entering the following:

pear channel-update pear.php.net

4. Moving PECL

Move the current installation of PECL to a different location:

sudo mv /usr/bin/pecl /usr/bin/pecl-old

You may not have PECL installed in which case skip the above step. You will still need to setup the link below though.

sudo ln -s /Applications/MAMP/bin/php5.3/bin/pecl /usr/bin/pecl

5. Adding the Paths

Now we need to update the export path, so we can use the PHP, PEAR and PECL commands. For more information on this see “Updating Your Export Path in OS X”.

Open your local profile in vi by entering:

vi ~/.profile

Then add the following line into your file:

export PATH=/Applications/MAMP/bin/php5.3/bin:$PATH

Don’t forget to replace the version 5.3 with whatever version you have used.

Also remember that these paths won’t take affect until you close down terminal and re-open it. To test your paths are working type the following:

pear list-all

If you get an error then your paths are not set correctly. Drop me a comment and I will try and help.

6. Fixing MAMP Permissions

Unfortunately, there is a small known bug with the permissions used by the PHP, PEAR and PECL files in MAMP. Luckily for you I have done the hair pulling and can steer you through this. To fix the issue simply enter:

chmod -R 755 /Applications/MAMP/bin/php5.3/bin/

Again don’t forget to replace the version 5.3 with whatever version you have used.

7. Installing Symfony

You could install Symfony using the PEAR library, but I am just not comfortable with this. Any Symfony sites you build in this way then use the core files from the PEAR version of Symfony. If you use PEAR you can become stuck with that version. The reason being is that if you build a website using Symfony 1.0 and then decide for the next project you want to develop in Symfony 1.4 you will have to upgrade the PEAR library from version 1.0 to 1.4 What then happens to the site using the core files for version 1.0? I know that you can freeze the Symfony files, so the PEAR files aren’t used, but personally I just find this messy.

The PEAR Way

As always this is only my opinion on the matter, so if you do want to install the latest version of Symfony using PEAR you can do so by doing the following:

pear channel-discover pear.symfony-project.com
pear install symfony/symfony

From here you can follow the installation instructions from Symfony or you can follow my way below through the whole process.

…or I Did It My Way!

This is also how the developers at Symfony prefer you to do it. In their words: “It is not the recommended way of installing symfony, as you should prefer a dedicated installation for each of your projects.”.

My personal preference is to download the Symfony files directly from the Symfony site and go from there. You can download the latest version from the installation page at http://www.symfony-project.org/installation. I downloaded the file as a tgz file and saved it to the root directory of my local site. Create a new set of folders called lib/vendor:

cd /Applications/MAMP/htdocs/newsite/
mkdir -p lib/vendor

Remember to replace “newsite” with the directory of where your new site is stored locally.

You can create your own folder instead of lib/vendor, but this is deemed good practice and from the official Symfony instructions. Next move the tar file into the lib/vendor folder and extract it.

mv symfony-1.4.5.tgz lib/vendor/symfony-1.4.5.tgz
tar -xzvf lib/vendor/symfony-1.4.5.tgz

Then we need to rename the extracted folder to something simpler and remove the tar file:

mv lib/vendor/symfony-1.4.5 lib/vendor/symfony
rm lib/vendor/symfony-1.4.5.tgz

8. Creating a Symfony Project

Now we have installed Symfony we need to setup the project!

Make sure you are in terminal and you are in the directory of your new site. When you are there create a new project:

cd /Applications/MAMP/htdocs/newsite/
php lib/vendor/symfony/data/bin/symfony generate:project PROJECT_NAME

Remember to replace “newsite” with the directory of where your new site is stored locally. Replace PROJECT_NAME with the name of your project.

9. Configuring the Database

These next instructions are on the basis that you are going to use Doctrine instead of Propel. Doctrine and Propel are two database extraction layers that allow you to easily access and manipulate your database. Propel was used in the earlier versions of Symfony, but the developers of Symfony are now pushing Doctrine and I tend to agree with them. If you want to use Propel you can find out more how to setup Symfony with Propel by visiting the Symfony Installation Guide page.

./symfony configure:database "mysql:host=localhost;dbname=dbname" root password

There are many ways you can setup your database to work with Symfony. For more information on this see “Creating and Updating Your Database in Symfony” article.

10. Creating Your Application

Then you need to setup your first application:

./symfony generate:app frontend

You would think this would do now, but there is two more things you need to do.

  1. Go back to MAMP and update the Disk location of the local site to now point to the “web” folder. It would have been something like “/Applications/MAMP/htdocs/newsite”. It now needs to be “/Applications/MAMP/htdocs/agentdunas/web” as the web folder in Symfony is the document root folder.
  2. Apply the changes to MAMP PRO, which should result in Apache and MySQL being restarted.

Now if you go to the site you should see a nice formatted Symfony welcome page. Unfortunately, you won’t as Symfony’s default files are not yet tied up to our web folder! Fear not though a simple symlink should do the trick.

Not So Pretty Symfony Welcome Page

Not So Pretty Symfony Welcome Page

Try this:

ln -s /Applications/MAMP/htdocs/newsite/lib/vendor/symfony/data/web/sf/ web/sf

You should now see:

The Correct Symfony Welcome Page

The Correct Symfony Welcome Page

That was a long one! As always drop me any questions or suggestions.

14 Comments

  • OMG Thankyou thankyou thankyou. I’ve been trying to install memcache for who know’s how long and this did the trick

    “6. Fixing MAMP Permissions
    Unfortunately, there is a small known bug with the permissions used by the PHP, PEAR and PECL files in MAMP. Luckily for you I have done the hair pulling and can steer you through this. To fix the issue simply enter:

    chmod -R 755 /Applications/MAMP/bin/php5.3/bin/ “

  • Thank you Adam. This was the only clear tutorial I found on google.
    Now I look forward to learning symfony.

  • hi,

    @step 10, i make iptoux$ ln -s /Applications/MAMP/htdocs/blog/lib/vendor/symfony/data/web/sf/ web/sf

    but i become an error
    ln: web/sf: No such file or directory
    iptoux$

    whats wrong?, setting up mamp and symfony on mac is very strange :D

  • When you run the command you need to be in your project directory (looking at your directories should be /Applications/MAMP/htdocs/blog). If I have your directory setup right this should also work:

    ln -s /Applications/MAMP/htdocs/blog/lib/vendor/symfony/data/web/sf/ /Applications/MAMP/htdocs/blog/web/sf

    Hope that helps.

  • thanks! works greate

  • Great blog! much appreciated.

    Sent from my iPhone 4G

  • FYI, your website won’t look ideal within Opera.

  • Thanks so much for this information … I’m already doing some of these suggestions but there are many others that are new to me.

  • Interesting article. Were did you got all the information from?

  • Keep posting stuff like this i really like it! Good job friend!

  • Wow! What an eye opener this post has been for me. Very much appreciated, bookmarked, I can’t wait for more!

  • You are professional.

  • Thanks for the feedback, got the information from a number of snippets out there on the web and a whole load of hair pulling trying different things :-)

  • Thanks for letting me know, I will take a look.

Leave a comment