Magento Tutorials

How to Install Magento 2 Using Composer

Starting from Magento version 2.4.2 onwards, command line is the default (and only) method for installing Magento. And while you could technically download an archive of the Magento version that you want to install, this method is not really recommended and you should use Composer to download Magento instead, as it provides various more benefits including:

  • Reuse third-party libraries without bundling them with source code
  • Reduce extension conflicts and compatibility issues by using a component-based architecture with robust dependency management
  • Adhere to PHP-Framework Interoperability Group (FIG) standards
  • Repackage Magento Open Source with other components
  • Use the Magento software in a production environment

Note: You may want to check Magento 2 System Requirements first before installing Magento using Composer.

Step 1: Install Composer

  1. Run the following command to check if Composer has already been installed:
composer --help
composer list --help
  1. If nothing displays or you face any errors, please install Composer:

Create an empty directory on your Magento server and run the following commands:

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
  1. Run the following command to make your Composer globally available
mv composer.phar /usr/local/bin/composer

Step 2: Download Magento 2

Run the following command in the root directory.

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

For example, let’s say we want to download Magento 2.4.2 in our root directory.

composer create-project --repository-url=https://repo.magento.com/ magento/project-enterprise-edition=2.4.2 .

Step 3: Set Up Permissions

You should set correct permissions for the whole Magento 2 installation directory by running the below command:

find . -type f -exec chmod 644 {} \;            
find . -type d -exec chmod 755 {} \;        
chmod -Rf 777 var
chmod -Rf 777 pub/static
chmod -Rf 777 pub/media
chmod 777 ./app/etc
chmod 644 ./app/etc/*.xml
chmod -Rf 775 bin

Step 4: Create The Database

Create an empty database to work with MySQL in case you have already have correct permissions:

echo "CREATE DATABASE magento2" | mysql -u[mysqluser] -p

Step 5: Install Magento 2

Run the following lines in Command Line to install your Magento 2:

php bin/magento setup:install 
--base-url="http://yoururl.com/" 
--db-host="localhost" 
--db-name="dbname" 
--db-user="dbuser" 
--db-password="dbpass" 
--admin-firstname="admin" 
--admin-lastname="admin" 
--admin-email="user@example.com" 
--admin-user="admin" 
--admin-password="admin123" 
--language="en_US" 
--currency="USD"
--timezone="America/Chicago" 
--use-rewrites="1" 
--backend-frontname="admin"<b>
</b>
  • base-url: the path that your Magento directory is in, which follows the following format: http[s]://<host or ip>/<your Magento install dir>/
  • db-host: the hostname or IP address of your host 
  • db-name: change it to the name of the Magento database you just created
  • db-user: a database user with full permission. We’ll be using the default root user.
  • db-password: the password of your database user. Leave it blank if you’re using ‘root’ database user
  • admin-firstname: your first name
  • admin-lastname: your lastname 
  • admin-email: your email address
  • admin-user: the username which you’ll be using to log into Admin Panel
  • admin-password: the password which you’ll be using to log into Admin Panel
  • language: the language which you’ll be using in your Admin Panel and your storefront. Use language code like en_US.
  • admin-email: change it to your email
  • currency: set the default currency to use in your storefront.Enter php bin/magento info:currency:list for a list of supported currencies along with their codes
  • timezone: change to the timezone that you’re in. Refer to the list of supported timezones for a better idea on what to fill.
  • url-rewrites: set to 1 to enable Web Server Rewrites. This will help with your site ranking.
  • backend-frontname: set your Admin URL. Omitting this parameter will result in a randomly generated URL for your Magento Admin path (e.g., admin_jkhgdfq)
  • search-engine: set the version of Elasticsearch that you want to use for this Magento installation. The default is elasticsearch7
  • elasticsearch-host: the hostname or IP address where Elasticsearch is running. The default is localhost
  • elasticsearch-port: the port number that Elasticsearch is listening to. The default is 9200

For more configurable options, please refer to the official guide by Magento.

Wait for a while and if you see this message:

Post installation file permissions check…
  For security, remove write permissions from these directories: 'C:/xampp/htdocs/magento24/app/etc'
  [Progress: 1270 / 1270]
  [SUCCESS]: Magento installation complete.
  [SUCCESS]: Admin Panel URI: /admin
 Nothing to import.

You have successfully installed Magento using Composer!

The above values are just examples, you definitely should change them according to your own info.

Hope this helps!


DON’T MISS:

How to Setup Magento 2.3 PWA Studio With Custom Theme
How to Install Magento 2 on Ubuntu 16 Using Nginx
How to Install Magento 2 on Localhost
How to Install Magento 2 on WAMP Server Localhost

Is it helpful?

Dom

A knowledge craver who always strive to be wiser everyday.

Subscribe
Notify of
guest
9 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
vrms
vrms
3 years ago

>> Run the following command in the root directory

that is not the root directory ‘/’ of your linux server, right? If that assupmtion is correct wouldn’t …

“Run the following command in the directorz of your linux server that you want to install Magento in. A common method would be to create a system user for a Magento store and use the /home directory of that user to install Magento”

… be a little bit more precise instrucion?

Parminder singh
Parminder singh
2 years ago

Is this possible to install Magento direct to the hosting via composer? not localhost, I want to install to the dreamhost vps

cruiserbenz
cruiserbenz
2 years ago

Thanks for the tutorial, I was at step 5, then there came errors that my php was below the requirements. After reading the docs and installing all the php components i have issues regarding the installation. Currently facing this error using composer at step 2: Your requirements could not be resolved to an installable set of packages. Problem 1 – magento/inventory-composer-installer 1.1.0 requires composer-plugin-api ^1.1 -> no matching package found. – magento/product-community-edition 2.3.2 requires magento/inventory-composer-metapackage 1.1.2 -> satisfiable by magento/inventory-composer-metapackage[1.1.2]. – magento/inventory-composer-metapackage 1.1.2 requires magento/inventory-composer-installer 1.1.0 -> satisfiable by magento/inventory-composer-installer[1.1.0]. – Installation request for magento/product-community-edition 2.3.2 -> satisfiable by magento/product-community-edition[2.3.2].… Read more »

trackback

[…] trying to install magento 2.3.2 via composer and have come acrosss this article about how to do […]

Kuber
Kuber
5 months ago

Hi, In official documentacion says

find . -type f -exec chmod 664 {} \;
find . -type d -exec chmod 775 {} \;

Why do you recommend 600 and 700?

Luke Vu
5 months ago
Reply to  Kuber

Hi Kuber,

Thanks for pointing that out! That was outdated information from our part and we have updated the article with more up-to-date instructions.

In newer Magento versions, you should set permission for your files to 664 and for your folders to 775, as this way gives full control to your file system owner and users in your web server group, and no permissions to anyone else.