Setup Magento2
Published: 2023-08-24 07:19:17

## Step 4: Download Magento

Before download Magento 2, we need Magento authentication keys to authenticate at [Magento marketplace](https://marketplace.magento.com/customer/account/) if don't have one.

Select My Account from the top-right corner. Now click Access Keys and Generate your new SECURE KEYS.

```
Public Key: Is your username
Private Key: Is your password
```

Once you've acquired both the access keys, we can download Magento using one of 2 ways:

### Through Github

You can download sourcecode https://github.com/magento/magento2/releases directly from Magento official repository

After donwloading, you should extract the pack to `C:\xampp\htdocs\magento2` and run this command inside the folder.

#### Creating an authorization file
The Magento 2 GitHub repository requires you to authenticate. The `composer install` commands fails if you do not.


```
composer install
```

After the process finished, run the command

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

and 

```
composer config http-basic.repo.magento.com <public key <private key>
```

### Through Composer

Go to `C:\xampp\htdocs` folder and run this command.

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

After inserting your repo.magento.com username and password, it will ask for store credentials. Type “Yes.”

Then Magento 2 will now start downloading on your server.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/06h48j26o6zts0xahib2.png)

---


Regardless of the downloading method you choose, the final magento2 folder should look like this.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r35taysl2j94bkis1sq1.png)

## Step 5: Install Magento

Now every preparation steps are done and we can start installing Magento 2 on Localhost.

Go to `C:\xampp\htdocs\magento2` folder, insert this command to start installing Magento 2 (make sure Elasticsearch, Apache, MySql all are running before execute this command)

```console
php bin/magento setup:install --base-url="http://127.0.0.1/magento2/pub/" --db-host="127.0.0.1" --db-name="magento2" --db-user="root" --db-password="root" --admin-firstname="admin" --admin-lastname="admin" --admin-email="user@example.com" --admin-user="admin" --admin-password="Admin@123456" --language="en_US" --currency="USD" --timezone="America/Chicago" --use-rewrites="1" --backend-frontname="admin" --search-engine=elasticsearch7 --elasticsearch-host=127.0.0.1 --elasticsearch-port=9200 --elasticsearch-enable-auth=1 --elasticsearch-username=elastic --elasticsearch-password=elastic
```
Important options:

- `--base-url`: your magento 2 installation folder
- `--db-name`: your database name in step 4
- `--db-password`: your database password in step 4

Others are optional, we can change later.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f53fyuejl4o4yllo3doy.png)

If you get the following error

```
In PatchApplier.php line 172:

Unable to apply data patch Magento\Theme\Setup\Patch\Data\RegisterThemes for module Magento_Theme. Original exception message: Wrong file

In Gd2.php line 70: Wrong file  
           
```

Then follow this [solution](https://dev.to/jazurite/part-6-error-handlings-3mnd#error-gd2php-line-70) and run the command again

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p0u5463n1fqwbdmoqy6c.png)

Now open the terminal and run these commands to upgrade the database

```
bin/magento indexer:reindex
bin/magento setup:upgrade
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9h7zs93u3lx4ctuaxce1.png)

and this command to compiles and generates optimized code for Dependency Injection

```
bin/magento setup:di:compile
```

If you get the following error

```error
In ErrorHandler.php line 62:
                                                                                                                                                                                       
Warning: file_put_contents(C:/xampp/htdocs/magento2/generated/metadata/primary|global|plugin-list.php): Failed to open stream: No such file or directory in C:\xampp\htdocs\magento2\lib\internal\Magento\Framework\Interception\PluginListGenerator.php on line 411   
```

Then following this [solution](https://dev.to/jazurite/part-6-error-handlings-3mnd#error-pluginlistgeneratorphp-on-line-411) and run the command again

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tk96z66w9bcvun5grmjn.png)

Now can start to deploying our static content

```
bin/magento setup:static-content:deploy -f
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zqvdto6tvydx89monwi5.png)

Clear Cache
```
php bin/magento cache:flush
```


## Step 6: Install Sample Data

Now we will deploy sample data for our new Magento 2 website on localhost. Sample data will fill your website with some products, categories and images
 
Run these below commands

```
bin/magento sampledata:deploy
bin/magento indexer:reindex
bin/magento setup:upgrade
bin/magento setup:static-content:deploy -f
bin/magento cache:flush
```

Well done! refresh your website and enjoy your new Magento 2.4 installation with sample data

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wvtwuysck0lizncxgvwo.png)