This blog is about C++, CakePHP, Linux, PEAR, PHP, Pyrus, Apache, and MySQL and related topics.

Tuesday, December 16, 2008

How to install Smarty template

My first attempt to install the Smarty Template Engine was to follow the basic installation instructions from the Smarty manual. The very first sentence says to: "Install the Smarty library files which are in the /libs/ sub directory of the distribution." What? I was lost right from the get-go. I'm one of those guys that likes clarity. This is not clarity.

So, I decided to go the easy route. Since I have Ubuntu Linux 8.10 running on my computer, the easy way would be to install the Smarty Template Engine package using the Synaptic Package Manager (System > Administration > Synaptic Package Manager). I did a quick search for Smarty and sure enough it was there so I installed it. This was very easy to do.

So, going back to the basic installation instructions I created the following script and tried to run it.

// NOTE: Smarty has a capital 'S'
$smarty = new Smarty();

And this was the result:

: require_once(Smarty.class.php) [function.require-once]: failed to open stream: No such file or directory in /var/www/smartytest.php on line 3

Fatal error: require_once() [function.require]: Failed opening required 'Smarty.class.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/smartytest.php on line 3

I was prepared for this since the instructions warned me this might happen and it also gave four possible solutions.

Again I decided to go with the easy solution and set the include_path as follows:

; Paths and Directories ;

; *nix: "/path1:/path2"
include_path = ".:/usr/share/php:/usr/local/lib/Smarty-v.e.r/libs/"

; Windows: "\path1;\path2"
include_path = ".;c:\php\includes;c:\webroot\libs\Smarty-v.e.r\libs\"

This didn't work either, I got the same error message. So I figured it must be in a different folder and all I have to do is find the folder. Going back into the Synaptic Package Manager I located Smarty again and by right clicking Smarty and selecting properties I was able to see the location of the installed files. I thought that with the correct path I would get Smarty up and running. Nope.

I searched in the forums at and didn't find anything useful.

I searched the Smarty forums and found this thread. There lokie gives four steps to installing Smarty but again I found myself lost because it wasn't 100% clear what to do.

That's when I decided I would have to write this post because there seems to be two kinds of PHP programmers out there. Those who don't need very detailed explanations and those that do. I'm in the second category and judging by the messages I see in the forums I am not the only one.

To get Smarty up and running this is what I did:

1. I went to the download page on Smarty's website, located at:

2. I downloaded the most recent stable version which at the time of writing is: Smarty 2.6.20 (.tar.gz) (.zip) August 15th, 2008

3. I created a folder Smarty at /var/www/smarty to temporarily hold my files

4. I extracted the contents of the .tar.gz file into the smarty folder.

5. Inside the Smarty folder is one folder called Smarty-2.6.20, and inside this folder there are four folders and ten files.

The folder were demo, libs, misc, and unit_test.

6. I opened and read QUICK_START and decided to follow these instructions as close as possible.

7. I made my directory structure as follows: /usr/local/lib/php/Smarty/. I started out with only the following: /usr/local/lib/ so I had to create two directories which I did using the terminal, like so:

$> cd /usr/local/lib/
$> mkdir /usr/local/lib/php/
$> mkdir /usr/local/lib/php/Smarty/

Once the directories are set up now we will copy the library files from the download and put the copies in the Smarty folder. I did this using the terminal:

$> sudo cp -r /var/www/smarty/Smarty-2.6.20/libs/* /usr/local/lib/php/Smarty

Now my directory and file structure for Smarty should look like this:


Now going back to the QUICK_START instructions. The next step is to create four directories that Smarty needs to work.

my document root is var/www/website so I decided to put the Smarty files under /var/www/website/smarty. Using the terminal again I create the directories as follows:
$> cd /var/www/
$> mkdir smarty
$> mkdir smarty/templates
$> mkdir smarty/templates_c
$> mkdir smarty/cache
$> mkdir smarty/configs
$> sudo chown user:pass smarty/cache
$> sudo chown user:pass smarty/templates_c
$> chmod 775 smarty/templates_c
$> chmod 775 smarty/cache
The next step now is to create the Smarty PHP script:

$> cd /var/www/
$> mkdir myapp
$> gedit index.php
The gedit text editor opens and you cut and paste the following and save the file at index.php inside the myapp directory.

template_dir = '/web/';
$smarty->compile_dir = '/web/';
$smarty->cache_dir = '/web/';
$smarty->config_dir = '/web/';

$smarty->assign('name', 'Ned');

Change the values for your own specific situation. My webroot is located at /var/www and therefore my values were:

template_dir = '/var/www/smarty/templates';
$smarty->compile_dir = '/var/www/smarty/templates_c';
$smarty->cache_dir = '/var/www/smarty/cache';
$smarty->config_dir = '/var/www/smarty/configs';

$smarty->assign('name', 'Ned');

Save as index.php in the myapp folder.

Now we setup the Smarty Template, here is our template:

(click image to enlarge)

Save this as index.tpl in this folder: /var/www/smarty/template/

Finally, it's time to test if the installation is working. Go into your browser and type http://localhost/myapp/index.php

If you see "Hello, Ned!" displaying on the screen it means you have successfully installed the Smarty Template Engine.

Once you succeed at this it's time to take the Crash Course on the Smarty website.


  1. sexy work bro ....absolutly awsome ...i will promote ur link

  2. nice post! a couple of things though in your index.php file. change template_dir = '/var/www/smarty/templates'; to

    $smarty->template_dir = '/var/www/smarty/templates';

    also place the following lines of code directly above the previous one in the following order:

    require once('/user/local/lib/Smarty/Smarty.class.php');

    $smarty = new Smarty();