Let’s start with Magento 2

Hello and welcome to the very first article on the site. I will show you how to install Magento 2 on Vagrant-based VM. There’s a great guide on Magento official docs, but it’s very time-consuming and it forces you to install a lot of pieces manually. I want to make it quick and easy as much as possible.

Prerequisites

To start off, please check if you have

  1. VirtualBox
  2. Vagrant

installed on your computer.

Linux (only!) users will need to install nfs system packages with the following command:

sudo apt-get install nfs-common nfs-kernel-server

and vagrant-bindfs plugin:

vagrant plugin install vagrant-bindfs

Windows (only!) users, I recommend installing Git Bash so you can follow the tutorial using unix-based commands. Also, make sure to run it as administrator since it’s required for this tutorial.

Download files for your system

I used PuPHPet to prepare a fully configured VM for Magento 2 development:

Setting Up Virtual Machine

Once you’re done with previous step:

  1. Extract the downloaded zip file
  2. Open up a terminal window and go to the extracted folder (the one that contains Vagrantfile)
  3. Run vagrant up and go grab a coffee

The last step will automatically install many things for you:

  • Ubuntu Trusty 14.04 LTS x64
  • Apache web server (2.4.12)
  • PHP (5.6), XDebug and modules (php5-gd, php5-curl, mcrypt)
  • MySQL (5.6) with predefined user root/root and magento2 database
  • git
  • Composer
  • virtual host mage2.dev (document root will be /var/www/html/mage2)

However, you’ll need to be patient since it will take some time. While you wait, you can update hosts file on your system. For Mac/Linux users, it’s located under /etc/hosts, and on Windows, it is C:\Windows\system32\drivers\etc\hosts.

192.168.56.101 mage2.dev www.mage2.dev
puphpet-ready

If you don’t see this in the terminal – “vagrant up” has failed!

Installing Magento and its dependencies

When you see that vagrant up has been finished, you can login to your VM via SSH

vagrant ssh

Make sure that you are in the right folder:

cd /var/www/html/mage2

Clone Magento

git clone https://github.com/magento/magento2.git .

Now we can install all dependencies with the following command:

COMPOSER_PROCESS_TIMEOUT=2000 composer install

Enabling Sample Data

I highly recommend installing sample data, since it will drastically save your time later. To enable sample data, you must update composer.json in the Magento root installation directory to provide the location of the sample data package. So, open up composer.json file in your favourite text editor and  add “minimum-stability”: “beta”, before license. A snippet follows:

"name": "magento/magento2ce",
"description": "Magento 2 (Community Edition)",
"type": "project",
"version": "0.74.0-beta8",
"minimum-stability": "beta",
"license": [
    "OSL-3.0",
    "AFL-3.0"
],

Now enter the following command to reference Magento packages in composer.json:

composer config repositories.magento composer http://packages.magento.com

and enter the following command to require the current version of the sample data package:

composer require magento/sample-data:<version>

where <version> should be replaced by the exact value. At the moment, the right version is 0.74.0-beta9, but you can always check it on this site. This command will take some time, so be patient.

Finally, start the installer from command line

We reached the final point, where we are quite ready to run the installer.

It’s up to you to choose between web-based or command-line installer. For some reason, I couldn’t make web-based installer to work for me, due to the following error: [ERROR] exception ‘Exception’ with message ‘Warning: fopen(): Filename cannot be empty in /var/www/mage2/dev/tools/Magento/Tools/SampleData/Helper/Csv/Reader.php on line 64’ in /var/www/mage2/lib/internal/Magento/Framework/App/ErrorHandler.php:59. I didn’t want to spend too much time debugging this issue, since I am quite new to Magento 2. That’s why I tried command line installer, and it worked like a charm!

Make sure that you are in the root Magento 2 directory.

cd /var/www/html/mage2

Once there, run the installer

php bin/magento setup:install --base-url=http://mage2.dev/ \
--backend-frontname=admin \
--db-host=localhost --db-name=magento2 --db-user=root --db-password=root \
--admin-firstname=YOUR_FIRSTNAME --admin-lastname=YOUR_LASTNAME --admin-email=user@example.com \
--admin-user=ADMIN_USER --admin-password=ADMIN_PASS --language=en_US \
--currency=USD --timezone=America/Chicago --use-sample-data --use-rewrites=1

Make sure to review the snippet and change it according to your local settings. This process is the longest one, and it took around 15-20 mins on my computer.

Once it’s completed, you should be able to go to http://mage2.dev and start playing with Magento 2!

Magento Doesn’t Load Styles

This may happen. We’ve explained how to fix the issue.

Bonus Tip: Accessing MySQL Database

The very last thing that I want to cover is about accessing MySQL. I am attaching screenshots with necessary settings for my favourite DB management tools called Sequel Pro (Mac OS) and MySQL Workbench (Linux and Windows). If you are using a different tool, please make sure it does support connecting to MySQL through SSH tunnel.

connect-to-mysql-via-ssh-tunnel

SequelPro – Connecting to MySQL through SSH tunnel

MySQL Workbench - Connecting through SSH tunnel

MySQL Workbench – Connecting to MySQL through SSH tunnel

Everything should be self explanatory. Private key is coming within PuPHPet, and this is the right location: puphpet/files/dot/ssh/id_rsa.

Milan Stojanov is a certified Magento developer and Tuts+ author on Magento Fundamentals premium course. He likes to read and write about web development.

  • marcelo

    Hello,

    See attached image.

    http://i.imgur.com/FM20CuU.png

    I stopped in this command – COMPOSER_PROCESS_TIMEOUT=2000 composer install

    How to fix?

    Cheers,

    Marcelo Amorim

    • Milan Stojanov

      Hi,

      The error means that Composer was not installed on your virtual machine for some reason. Are you sure that everything went well when you run vagrant up? It should take a couple of minutes, ending with big green READY message in the terminal. I will recheck it on my own tonight and update this comment.

      In the meantime, what you can do to fix the issue is:

      1) vagrant up (from the folder where you downloaded Windows VM files)
      2) vagrant ssh
      3) curl -sS https://getcomposer.org/installer | php
      4) mv composer.phar /usr/local/bin/composer

  • Pingback: How to configure XDebug for Magento 2 development - MageClass()

  • Interesting stuff!

  • Peter

    Thanks for the walk through! Everything went super smooth until the m2 installer that gets halted on the first readiness check with the following error under PHP Settings Check;

    Your PHP Version is 5.6.8-1+deb.sury.org~trusty+4, but always_populate_raw_post_data = 0. $HTTP_RAW_POST_DATA is deprecated from PHP 5.6 onwards and will stop the installer from running.

    I located php.ini under etc/php5/fpm and etc/php5/cli and both had the same section mentioned in the error message but both the actual config lines, always_populate_raw_post_data = -1, were commented. I uncommented both and restarted apache but to no avail.

    Would you have an idea on what’s holding me up? I’d be very gratetful!

    Peter

    • Milan Stojanov

      Hi @disqus_J5wzgx5yiK:disqus

      You need to restart php-fpm as well:

      sudo service php5-fpm stop
      sudo service php5-fpm start

      I noticed that error while playing with web installer, but it went smooth from the command line.

      Let us know if you managed to fix this. 🙂

      • Peter

        You’re an absolute star! Thanks 🙂

      • Peter

        Ok so I got stuck on exactly the same as you and went for the command line install instead so that’s a recommend to anyone else trying!

        • Milan Stojanov

          Yes, exactly! I saw many people in the community are reporting that fopen warning for web installer, but thankfully command line works great.

          Glad that you are up and running with Mage 2, enjoy! 😉

  • Dejan Beljic

    Great article Milan!

  • How to fix «fopen(): Filename cannot be empty» when installing with sample data: https://mage2.pro/t/topic/13

  • Peter Svegrup

    Hi Milan, and thanks again for making it so much more easy to install M2. Could you update your recipe to install the php extension ‘ext-xsl’? It now throws an error that it’s missing when I run composer install. Cheers 🙂

    • Milan Stojanov

      Hi Peter, you are absolutely right – I noticed that with beta16. Because I am on vacation at the moment, I will leave the update for the weekend, I want to test with Merchant Beta. 🙂

      Thanks again for letting us know!

      Milan

      • Peter Svegrup

        I can confirm it works great with the Merchant Beta! By the way please change the cli install command base url to http://mage2.dev cause it’s a little confusing with the 127.0.0.1. Well at least it was for me 🙂

  • Milan Stojanov

    @petersvegrup:disqus and all readers,

    I’ve updated puphpet files to automatically install ext-xsl extension. Also, I’ve modified the snippet for installing sample data, according to the feedback bellow.

    Thanks and cheers. 😉

  • Jens Bourgeois

    Hi Milan,

    Thanks for the detailed tutorial!

    I stumbled upon the following error after running the latest command (php bin/magento setup:install) though:

    [MagentoSetupSampleDataException]
    Error during sample data installation: Class MagentoSampleDataModelSampleData does not exist

    After some research, it seems this may be caused by having the develop branch checked out instead of the master branch. So I had to remove my mage2/ folder and remake it again and check out the master branch instead (git checkout master), then it worked like a charm. 🙂

  • Коля Гірний

    Hi Milan, are you familiar with docker?
    If yes, can you help me to write dockerfile with right way to install magento2.

  • Dan

    Hello! Really it is very detailed tutorial! But I still can’t choose is Magento 2 worth to migrate on it? I have to learn a lot of information about it, and there are many pros and corns. For example here http://sam-ecommerce.com/blog/view/magento2.0-migrate-or-not they say that Magento 2 is 3 times more expensive. Is it true? What do you think?

  • lenn_rt

    Did anyone already installed M2 on Amazon Linux (CentOS) with nginx? I am frustrated, I don’t get it run. I would appreciate any help! =)

  • PIYUSH RAJAWAT

    i have a problem with cron job in magento 2 it is not working on local server or live server . Can anyone help me to solve this problem . Because of this i am not able to install any extension . it always asking to run cron job .

  • Boris Perevyazko

    HI. after vagrant Up I get error :
    ==> default: /tmp/vagrant-shell: line 11: syntax error in conditional expression: unexpected token `;’
    ==> default: /tmp/vagrant-shell: line 11: syntax error near `;’
    ==> default: /tmp/vagrant-shell: line 11: `if [[ -f /.puphpet-stuff/install-ruby-1.9.3-p551]]; then’
    The SSH command responded with a non-zero exit status. Vagrant
    assumes that this means the command failed. The output for this command
    should be in the log above. Please read the output to determine what

    Aslo I attached image. Pls help me.
    Thanks
    https://uploads.disquscdn.com/images/4fa3308f542bc51533bfd1a92c3ea64a19fdead0f55d0a5a276ef1f2b5f139cd.png