Magento 2 – Development – Part 1: Install with composer
Watch the video below.
This blog and video series is going to take you through the fundamental basics of developing with Magento 2 based around the 2.3.* version, though the practices learned should all be compatible with earlier versions also.
Though this series is for beginners with the Magento 2 framework, it does consider you have already gained some knowledge about using command line tools, and PHP.
So to get all the benefits of Magento 2 we are going to discuss how to install it, but how to do it the right way, to comply with magento’s best practices.
Requirements of Magento?
This tutorial will require you to have a Linux (RedHat, CentOS, Ubuntu etc) or Mac system, or primarily an Unix based system already setup, with a working lamp stack.
Or at minimum PHP and a database. Though you will find it harder to get this running on any windows based systems (likley not at all).
Though one constraint is the Operating system, the other is the memory, Magento 2 requires PHP to handle allot. So a 256MB of memory just wont cut it you will need a system capable of 2GB or more RAM.
For further information regarding system requirements visit the Magento 2 hardware documentation.
Magento 2.3.3 requires your PHP version to be 7.3 or higher in order to function, this is constrained by the individual packages.
Whilst Magento 2.3 requires you to have PHP 7.2 or higher.
With PHP you will need to have the following PHP extensions installed also along with all there dependencies:
- gd, ImageMagick;
- json (PHP 7 only);
- iconv (PHP 7 only)
- opcache – Not required but recommended for code caching.
Though composer is not required for Magento 2, as you can do the same tasks on the file system or with git repositories. It’s highly recommended to have it installed.
What is composer ?
Composer is a package management PHP command line tool, that allows for easy installation of PHP code and auto loader generation.
But composer is a bit more than a simple package management tool, it is a tool allowing you to install packages of code, or multiple packages of code, whilst installing there decencies and there parents decencies until the chain is completed with a working application.
To install composer is relatively easy on any Unix system, get yourself across to https://getcomposer.org/ scroll to the bottom of the page and click on the version number to download.
Once downloaded open a terminal window, to do this on a Mac press CMD + Space to open spotlight and search terminal or on a Linux machine press CTL+ALT+T
Then you need to copy your phar to an global executable folder on unix such as /usr/local/bin, this makes the phar executable from any terminal without specifying location:
# sudo - command this causes you to complete the action as root # cp - Copy command followed by old parh and target path sudo cp ~/Downloads/composer.phar /usr/local/bin/composer
With the phar moved it needs to be made executable to do this you will need to run the below command:
#chmod - Changes the permission followed by a read, write and # execute number 7 being allow all OR by a +x sudo chmod +x /usr/local/bin/composer
For more information about composer read our article on how to use composer properly.
Installing Magento 2
Where to start ?
The first thing you want to do now your all set up is to make sure you have started your database, and created a new database. As the Magento framework need to install over 300 tables. One way can do this is to go back into your terminal and run the below command.
mysql -u DBUSER -p -e 'create database magento;'
On a Mac with the Msql Command line installed:
mysqlsh --sql -u DBUSER -p -e 'create database magento;'
What did we just do ? Well we used the MySql command to connect to the database using -u to specify the user. The -p flag to specify we have a password and finally the -e flag this tells mysql to run an inline query to create the database.
Ways to install?
Magento has a number of ways of installing the Magento 2.3 framework both are acceptable. But composer is easier to manage than the git method.
- Git – This involves cloning the Mageto 2 repository down into a directory and causing extra work. As you still partially have to use composer to get it to fully install. Also using the git method means all the modules are install in app/code. Rather than packages and version maintainable without manually doing so.
- Composer – This involves using the package management to download all the module parts of Magento with a single terminal command, making upgrading and maintainability easier to accomplish.
Handy Tip: One of the actual magento 2 exam questions revolves around physically upgrading magento, and how would you do it. The answer to this is simply change the version in composer.json. Or by using composer require command and running the, composer update command
We are only going to focus on installing with composer.
Magento 2 Installation Process
Now this is the easy bit, log back into your terminal window, and create a new directory where you would like to develop.
# mkdir – This command can be used at terminal level to make new directory such as mkdir /var/www/magento2 or mkdir ~/www/
With the database created, our directory created you can get magento installed. Again in the terminal run the below command:
Magento Open Source
composer create-project --repository=https://repo.magento.com/ magento/project-community-edition ./www
composer create-project --repository=https://repo.magento.com/ magento/project-enterprise-edition ./www
So what did we do here then? Using the composer comand you told it to create you a project with the repository repo.magento.com. Then you told it the project file to look for was named magento/project-<type>. By finally telling it the directory to install.
You will now see composer asking you for a username and password, to get these you will need to log yourself into https://marketplace.magento.com.
Once in click in the top right dropdown and go to “Your Profile”, you will see a screen like below.
Password and username you will find under “Access Keys”, the Public Key you will find is your username. Then the Private Key is your password.
Enter these into the terminal when requested from the composer command.
Composer will then run through installing each of the module components of magento 2.
With that, we finally need to tell magento to run its initial setup process. If your on a lamp stack you can now visit the url in a browser to go through this process.
Alternatively you can continue in the terminal. You can do this by changing directory to your web root. Then running the magento cli command in the magento bin directory.
You will need to change the parameters as required.
$ cd www/ $ php bin/magento setup:install \ --admin-firstname=John \ --admin-lastname=Smith \ --email@example.com \ --admin-user=admin \ --admin-password=password1 \ --base-url=http:/127.0.0.1:8282/ \ --db-host=localhost \ --db-name=magento \ --db-user=magento \ --db-password=magento \ --currency=USD \ --timezone=America/Chicago \ --language=en_US \ --use-rewrites=1
As this runs you will see each of the individual modules running there install scripts.
Now for those who don’t have a lamp stack you will see i set the base-url to 127.0.0.1:8282. That’s because when developing i like to run a single PHP instance from a terminal rather than apache/nginx.
Simply run the command below from the magento 2 root directory:
php -S 127.0.0.1:8082 -t ./pub/ ./phpserver/router.php
Visit 127.0.0.1:8082 in your browser and you should see a blank magento 2 instance.
You can now move on to part 2 of this series and start creating your first module. Magento 2 – Development – Part 2: Creating a new module