How to solve the “Mcrypt PHP extension required” problem during the Laravel installation

So, you decided to start using/study Laravel? Good!

When it comes to learn or practice with something new the most frustrating part is when this new “toy” won’t work out of the box. And that’s what happened to me the first time I tried to install Laravel on my MacBook Pro. I struggled with this problem for a good hour before finally figuring out what was wrong, and I thought would be great to write it down for future installations or to help other dudes like me that could encounter this problem. So, let’s take a look.

 

First step, before even start

Before even try to install Laravel you have to be sure that your machine has PHP and a Server Apache installed on it. The easiest way is to download MAMP and let him install everything for us, like PHP, Apache and MySql, something that you’ll need for sure if you’re intended to build something with Laravel.

Also, remember to install Composer to manage all the dependency of PHP and help you out a lot with Laravel, but not only.

 

Install Laravel

Open your Terminal and navigate to your favourite folder, the one you decided to use for your Laravel installation
cd /your-directory/your-folder
Always inside the Terminal write this code to activate Composer and tell him to download and install the latest version of Laravel for us
composer create-project laravel/laravel your-project-name --prefer-dist
And here comes the pain! This is the point where I got the current error message
Mcrypt PHP extension required.
Script php artisan clear-compiled handling the post-install-cmd event returned with an error.
[RuntimeException]
Error Output:
create-project [-s|--stability="..."] [--prefer-source] [--prefer-dist] [--repository-url="..."] [--dev] [--no-dev] [--no-plugins] [--no-custom-installers] [--no-scripts] [--no-progress] [--keep-vcs] [--no-install] [package] [directory] [version]

So, why is this happening? I’m not totally sure about the reason, but it looks like that Composer is trying to use the PHP version installed in the OS and not the one installed with MAMP, that it comes with the necessary Mcrypt extension.

 

Solve the problem

To solve this problem we have to edit our .bash_profile script and create an export to our MAMP php version. First of all open another Terminal instance and check your current php version with the command:
php -v
Your result should be something like this
PHP 5.5.10 (cli) (built: Apr 10 2014 17:49:22)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies

Now that we have our PHP version, in my case 5.5.10, we can open our .bash_profile script
nano .bash_profile
and add our MAMP PHP version after the code already present
export MAMP_PHP=/Applications/MAMP/bin/php/php5.5.10/bin
export PATH="$MAMP_PHP:$PATH"

Exit the file and Save it. That’s it!

Now, if you run again the Composer command inside your folder, the installation should successfully complete.

Hope to be helpful to someone, see ya!