How to install Magento 2 using composer
The first step would be to create a directory that should be empty.
mkdir magento;
cd magento;
To download Magento with latest version run
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition .
To download any specific Magento version
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.2.7 .
This will prompt you for Username and Password, which you can get by creating your account https://account.magento.com/applications/customer/create/
Once you create your account go here https://marketplace.magento.com/customer/accessKeys/ and click on “Create a new access key“.
Then you will get new keys using the public key as the username and private key as the password.
Once you are done with Magento download, install sample data, this step is optional.
php bin/magento sampledata:deploy
Once all the above steps are done you can install Magento like this from terminal
php bin/magento setup:install --db-host=localhost --db-name=your_database_name --db-user=database_username --db-password=database_password --base-url=https://your_domain.com/ --backend-frontname=admin --admin-firstname=Ratnesh --admin-lastname=Kumar --admin-email=ratnesh@domain.com --admin-user=admin --admin-password=admin123 --language="en_US" --currency="USD" --timezone="America/Chicago"