Installing Magento 2

The new and improved Magento 2 platform has been released but installing it can be tricky the first time round. The installation process has changed since the last versions and it takes a bit of time to get it up and running. I’ll be showing you how to install it locally while detailing any errors you might hit along the way.

There are two different methods of installing Magento 2. The easiest solution is by using the setup wizard which you’ll be familiar with already if you’ve installed previous versions of Magento. Alternatively there is the advanced installation using the command line interface (CLI) which gives us more control and allows us to install optional sample data.

If your unsure which path to take have a look at choosing how to install the Magento software page which explains the different options in more detail.

In this article I’ll only be covering the advanced installation using Composer and the CLI.

Before we begin

This articles assumes you have your server configuration already setup and have some knowledge of using the command prompt. Please refer to the Magento system requirements before proceeding.

I am using the built in Apache 2 server that comes with Mac OS X 10.11 which I have previously talked about in another article.

Step 1: Install Composer

Magento now uses Composer for dependency management. Composer enables us to manage the Magento components and their dependencies.

Before you install Composer lets check to see if you already have it installed. In the command prompt enter the following command:

composer --help

If the command help displays, Composer is already installed in which case you can skip this step and move onto cloning the Magento repository. If an error displays, to install Composer enter the following commands:

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

For further installation options please refer to the Composer documentation.

Step 2: Download Magento CE metapackage

If you haven’t done so already get your authentication keys setup as you’ll be prompted for these when downloading the metapackage.

Next change to your web server root directory, or to a directory you’ve configured as a virtual host, for example:

cd /Users/your_username/Sites

Now enter the following command to download the metapackage:

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition <installation directory name>

When prompted enter your authentication keys, your public key is your username and your private key is your password.

This command creates the project and downloads dependencies for it. The project is in a directory named <installation directory name> if you provided the parameter or it will be named project-community-edition if you did not.

Step 3: Set permissions

Once the download has completed you can now setup the correct permissions. This is an important step in the process, if skipped over you will face errors further on.

Change to your Magento root directory, for example:

cd /Users/your_username/Sites/magento2

Now enter the following command:

find . -type d -exec chmod 770 {} \; && find . -type f -exec chmod 660 {} \; && chmod u+x bin/magento

Step 4: Install sample data

This is an optional step and isn’t necessary, if you don’t require sample data please feel free to skip to the next step.

Installing sample data has got a whole lot easier in Magento 2.0 which can now be achieved through the CLI. You can use this method either before or after you install Magento; however, there might be additional tasks for installing sample data after you install Magento. To save yourself the headache its best to install the sample data before the installation.

Make sure you have your authentication keys on hand as you’ll be prompted for these when installing the sample data.

First change to your Magento bin directory, for example:

cd /Users/your_username/Sites/magento2/bin

Then to install the sample data enter the following command:

./magento sampledata:deploy

Step 5: Install Magento

Now everything it setup correctly you just need to run the install. This can be done through the command prompt which accepts a range of different parameters, for a full list of accepted parameters you can refer to the command install summary.

Change to your Magento bin directory, for example:

cd /Users/your_username/Sites/magento2/bin

From here you can use the Magento CLI by simply typing in:

./magento

This will display a list of all the available options for the CLI. At this stage we are only interested in the setup:install command for the installation.

Below is a basic example with minimal parameters passed to the command:

./magento setup:install \
--db-host=localhost \
--db-name=magento \
--db-user=root \
--db-password=root \
--backend-frontname=admin \
--base-url=http://127.0.0.1/magento2/ \
--language=en_GB \
--timezone=Europe/London \
--currency=GBP \
--admin-firstname=Magento \
--admin-lastname=User \
--admin-email=user@example.com \
--admin-user=admin \
--admin-password=password1

If there are any errors or missing parameters the CLI will notify you.

The great thing about the CLI is that its built to be used multiple times. If you’ve already installed Magento and say you want to re-install with a clean database, perhaps you forgot to pass a parameter and want to start again, simply supply the --cleanup-database parameter and it will drop the database tables before installing Magento.

If the installation has been successful within the command prompt you should see the following messages:

[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: /admin

Step 6: View your installation

Now you have everything setup the final step is to navigate to the url in your browser to ensure everything is working as expected.

The frontend url is what you passed through for the --base-url parameter during the installation, for example http://127.0.0.1/magento2/.

The backend url is provided for you within the command prompt as shown above. If you omitted the backend parameter Magento will have generated a random URI for you.

Note: Don’t forget depending on how you set up your web server and virtual hosts, the path might be different, please ensure these have been setup correctly as mentioned in the Before we begin section above.

Tips

During this guide you have been running the Magento commands within the
<installation directory name>/bin directory which can be frustrating if you have to change directory to run the commands.

However there is a better way of doing this which enables you to run Magento commands from any directory by adding <installation directory name>/bin to your system PATH.

Open your .bash_profile file within the command prompt and add the following line:

# Magento 2 CLI
export PATH=$PATH:/Users/your_username/Sites/magento2/bin

Now when you want to run a Magento command you can from any directory simply by entering magento into the command prompt.

Wrap up

If you’ve got this far hopefully you now have a fully working installation of Magento 2! I think the new CLI that comes with Magento 2 is really useful and it should speed up your workflow once you get used to the syntax, it’s a big improvement from previous versions.