As we know that the to setup development environment for PHP, you need a PHP parser, a Web Server and a database like MySQL or Oracle if you want to work on a dynamic website. To do so, either you can install everything manually or you can install ready-made software packages, which comes packed with all the requirements for PHP development environment.
The most popular, cross-operating system software package is XAMPP, which is widely used for PHP development locally for Windows OS, but personally, it is not that good when it comes to the macOS. I would suggest installing MAMP for macOS as it is more compatible with it and has a better user interface, which is a must for beginners.
Installing MAMP
Installing MAMP on macOS is very simple. All you have to do is download the latest setup from MAMP's Official Website. Once the setup file is downloaded, click on it to start the installation. The installation process is pretty straightforward, with just one catch, MAMP Pro is also installed along with the MAMP free version, which you can, of course, uninstall later.
Now go to your applications and double click on MAMP icon to start it. Click on Start Server to start the Apache Web server and MySQL database server.
What is Composer?
Composer is a Dependency Manager for your project. It downloads all the dependency that any project has on external libraries. These days almost all the libraries are using composer.
Install Composer globally using MAMP's PHP installation
Now as PHP is installed within MAMP, we would want that Composer should use PHP installation that was done along with MAMP. For this we first need to create an alias for MAMP's PHP installation, for this, we will add the code for making a new alias in our bash profile.
Open your terminal, and run the below command to open profile. We are using nano, you can use vim if you want:
For new versions of macOSX, bash profile is now changed to zsh, so you will have to update the .zprofile file and add an alias for the PHP installed by MAMP.
Run the command,
nano ~/.zprofile
And add the following line to this file,
# alias for MAMP PHP installation for composer
alias phpmamp='/Applications/MAMP/bin/php/php7.4.33/bin/php'
Update the above command as per your PHP version. As the installed version of php in MAMP on my laptop is 7.4.33, hence the directory name is php7.4.33, but you can check your PHP version.
Once you have done this, please open a new shell window for the changes to be picked up by the terminal.
With that set, now it's time to install composer.
First, run the following command in your terminal. This will install composer using MAMP's PHP
curl -sS https://getcomposer.org/installer | phpmamp
As you can see, that instead of the standard php at the end, we have used phpmamp, which is an alias for the PHP installation of MAMP.
Running the above command will download the composer.phar in the current directory.
Now to make composer available globally, i.e. everywhere in your computer/laptop, we have to move the generated composer.phar (PHP archive) file to location /usr/local/bin/composer,
sudo mv composer.phar /usr/local/bin/composer
For this, the terminal might ask for your password. Enter your password and hit "return" and we are done.
If you have a new laptop, your machine may not have the /usr/local/bin folder, in that case you will have to create the directory yourself. Run the command sudo mkdir /usr/local/bin
and hit return. If asked for password, provide your system password.
To verify the successful installation of Composer, open your terminal(if you closed it), and type the following command:
composer
It will show you the version installed and the available commands that you can use.
If you see the following error instead,
env: php: No such file or directory
or something similar, then you will have to perform, one small additional step.
You have to add the /usr/local/bin to your PATH, and also add the MAMP PHP to the Path. It's easy to do,
Open the .zprofile file,
nano ~/.zprofile
and add the following lines in it,
export PATH=/usr/local/bin:$PATH
# change the PHP version as per your version
export PATH=/Applications/MAMP/bin/php/php7.4.33/bin:$PATH
That's it. Now open the new shell window, and composer should be working.
You may also like: