Magento 2: Install via Composer and Command Line (CLI)

This article/tutorial show how to download and install Magento 2 through command line interface (CLI). We will be using Composer to download Magento 2 from Magento’s repository.

Here is the step-by-step guide on installing Magento 2 via command line:

1) Check & Verify System Requirements

Before, downloading and installing Magento 2, we need to make sure that our system/computer meets all the system requirements in order to run Magento 2. System requirements means proper PHP version and required PHP modules, proper database, web server, etc. Check Magento 2 system requirements: http://devdocs.magento.com/guides/v2.0/install-gde/system-requirements.html

2) Install Composer

Make sure you have composer installed in your system. You can get composer from: http://getcomposer.org

Alternatively, you can install composer in Ubuntu Linux using the following command:


sudo apt-get install composer

3) Download Magento via Composer

In Ubuntu 16.04 Linux, the webserver root is at path /var/www/html. I will create a folder named magento2 inside it and download Magento 2 over there. So, the full path for my magento 2 folder will be /var/www/html/magento2.

Using Composer, get/download Magento CE edition to your computer:


composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition /var/www/html/magento2

After running this command, it will ask for username and password for repo.magento.com.


Authentication required (repo.magento.com): 
Username:
Password:

To get the username and password for repo.magento.com, you have to generate access keys from https://marketplace.magento.com/customer/account/

– Go to https://marketplace.magento.com/customer/account/
– Login
– After you are logged in, click My Access Keys link
– On My Access Keys page, click on Generate Keys button
– Then, you will see two keys (Public and Private)
– Public Key will be your username
– Private Key will be your password

4) Set proper file permissions

You have to set write permissions to vendor, app/etc, pub/static, pub/media, and var directories. You can set the permission to all of these folders with the following command:


cd /var/www/html/magento2 && find var vendor pub/static pub/media app/etc -type f -exec chmod g+w {} \; && find var vendor pub/static pub/media app/etc -type d -exec chmod g+w {} \; && chmod u+x bin/magento

5) Switch User

Switch to user which has write permission to your magento2 directory.


su your_system_user

In Ubuntu, my default user has sudo priviledge and is the owner of the /var/www/html/magento2 directory. So, I didn’t have to switch user.

6) Install Magento 2

Go to your magento2 web root directory:

cd /var/www/html/magento2

As you can see below, I have specified the base url, database host, database user, database password, admin name, admin email, admin password, default currency, default timezone, etc. You can update them with your data.


sudo php bin/magento setup:install --base-url=http://127.0.0.1/magento2/ \
--db-host=localhost --db-name=magento2 --db-user=root --db-password=root \
--admin-firstname=Magento --admin-lastname=User --admin-email=user@example.com \
--admin-user=admin --admin-password=admin123 --language=en_US \
--currency=USD --timezone=America/Chicago --use-rewrites=1

After installation completion, you will get the message something like this:


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

So, in this case, our Magento 2 admin URL is http://127.0.0.1/magento2/admin_1vdc5o

Note: You need to give write permission to pub and var directory if you get cache write permission error:

sudo chmod -R 777 pub var

7) Install Sample Data (Optional)

This step is optional. You have a clean Magento 2 installed. Now, if you also need to install the sample products, categories, orders, customers, etc. then you need to install sample data.

Go to your magento2 web root directory:

cd /var/www/html/magento2

Run the following command:

sudo php bin/magento sampledata:deploy

At this point, you might get error stating something like “The requested package magento/module-name could not be found in any version, there may be a typo in the package name. sample data deploy magento 2“.

If you get such error then, run the following command on your magento2 root directory:

composer config repositories.magento composer https://repo.magento.com

After this, you can run the sample data deploy command again and then follow the below steps:

sudo php bin/magento sampledata:deploy

You might again be asked the username and password for repo.magento.com


Authentication required (repo.magento.com): 
Username:
Password:

To get the username and password, you need to do the following:

– Login to https://marketplace.magento.com/customer/account/
– Go to My Access Keys page
– Generate Keys
– Then, you will see two keys (Public and Private)
– Public Key will be your username
– Private Key will be your password

After sample data has been installed, you have to clean cache and run setup upgrade:


sudo php bin/magento cache:clean
sudo php bin/magento setup:upgrade

You will get the following message after successful setup upgrade:

Please re-run Magento compile command

You may ignore this command for development mode because Magento automatically compiles the files on first page load. If you want to go into production mode, then you need to run the following command to compile files:

sudo php bin/magento setup:di:compile

Note: Now, when you browse your Magento site, if you get error regarding permission issue while storing cache then you have to give write permission to pub and var directories:

sudp chmod -R 777 pub var

That’s all. Now, you should be able to see the categories and products in frontend when you browse http://127.0.0.1/magento2.

Hope this helps. Thanks.