Magento Tutorials

How to Install Magento 2 on Localhost

Updated April 13th, 2021: Users may run into a 404 Not Found error when trying to install Magento 2.4.2. Follow the suggested solution in the article for the best result.

It has been quite a long time since Magento introduced their 2.0 version. With more friendly user interface and improved performance, it is likely that many existing Magento 1 users will move to Magento 2 sooner or later. Flowing with the trend, SimiCart also released Magento 2 mobile app in December 2016.

Today, we’re gonna show you how to install Magento 2.4 locally on Windows using XAMPP server. This tutorial is targeted on current Magento 2 users and also users who are completely new to Magento. Now, let’s get started!

Part 1: Install and configure XAMPP

Step 1: Download XAMPP

Download link: https://www.apachefriends.org/download.html

We recommend installing XAMPP with PHP version 7.4.x, which is recommended by Magento for M2.4. Also according to Magento, you can install Magento 2.4 with PHP 7.3, but it is not tested or recommended.

Step 2: Install XAMPP

After downloading XAMPP, double-click on the file to install it on your computer.

Click Next.

xampp setup step 1

Leave the default selection as in the image. Click Next.

xampp setup step 2

Choose your installation folder. The default location is C:\xampp. After that, click Next.

xampp setup step 3

Click Next.

xampp setup step 4

Click Next.

xampp setup step 5

Setup will now install xampp on your computer. Please wait for a while.

xampp setup installing

After setup is finished, you will see the option “Do you want to start the Control Panel now?”. Keep it selected. Click Finish to exit setup and enter XAMPP Control Panel.

xampp setup finish

Step 3: Configure XAMPP

In XAMPP Control Panel, click Config button on “Apache” row, and click “PHP (php.ini)”.

configure php.ini


In the php.ini file, find these rows and remove “;” before each row:

;extension=intl
;extension=soap
;extension=sockets
;extension=sodium
;extension=xsl

Expand memory_limit to 4G:

memory_limit=4G

After you’ve done, save and close the file. Then, click the Start button on 2 rows Apache and MySQL to start them. Don’t quit XAMPP after this step, just let it run.

Step 4: Configure hosts file

Open file “C:\Windows\System32\drivers\etc\hosts“. Add the following line to the last row of the file:

127.0.0.1 www.localhost.com

Step 5: Create a database

Browse the URL http://localhost/phpmyadmin/ to access phpMyAdmin page. Put in a database name (“Magento2”, for example) and click Create.

phpmyadmin

Take note of your database name, as you’ll want to install Magento on it later on (using the root user).

Part 2: Install Elasticsearch

As of version 2.4, Magento requires Elasticsearch to be the catalog search engine.

Download Elasticsearch 7.6.0: https://www.elastic.co/downloads/past-releases/elasticsearch-7-6-0 (Magento 2.4.x is tested with Elasticsearch 7.6.x only. You can use other 7.x versions at your discretion, but we recommend using the tested version of Elasticsearch.)

Extract the .zip file you’ve just downloaded. In a terminal window, cd to the extracted directory, and run this command:

.\bin\elasticsearch.bat

When the installation is complete, you can check if Elasticsearch is properly installed at http://localhost:9200, which should give you something like this:

{
   "name" : "Cp8oag6",
   "cluster_name" : "elasticsearch",
   "cluster_uuid" : "AT69_T_DTp-1qgIJlatQqA",
   "version" : {
     "number" : "7.6.0",
     "build_flavor" : "default",
     "build_type" : "tar",
     "build_hash" : "f27399d",
     "build_date" : "2016-03-30T09:51:41.449Z",
     "build_snapshot" : false,
     "lucene_version" : "8.7.0",
     "minimum_wire_compatibility_version" : "1.2.3",
     "minimum_index_compatibility_version" : "1.2.3"
   },
   "tagline" : "You Know, for Search"
 }

Part 3: Download Magento using Composer

As some of the plugins from Magento 2 do not work with composer 2.x, you’ll want to use a stable version of composer 1.x to download Magento. The best way to do this would be by installing the Composer-Setup.exe.

This setup will install the latest composer version and set up PATH environment variables so that you can conveniently call composer from any directory. After you’re finished installing composer, it’s required that you should downgrade to an earlier 1.x version using composer self-update --1 in order to ensure the best compatibility.

Next, cd to C:\xampp\htdocs and run the following command:

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.2 <install-directory-name>

It should take a while for the composer to download and install all the necessary modules. When everything is finished, this is what you should see:

composer download Magento

Part 4: Install Magento using command line

Starting from Magento 2.4, command line interface is the default (and only) installation method for Magento.

During Magento 2.4 installation, you may encounter some errors. To prevent such errors, you first need to make some tweaks to Magento files.

To prevent this error:

gd2 error

Find validateURLScheme function in this file \vendor\magento\framework\Image\Adapter\Gd2.php and replace it with:

private function validateURLScheme(string $filename) : bool
   {
       $allowed_schemes = ['ftp', 'ftps', 'http', 'https'];
       $url = parse_url($filename);
       if ($url && isset($url['scheme']) && !in_array($url['scheme'], $allowed_schemes) && !file_exists($filename)) {
           return false;
       }
       return true;
 }

Next, cd to your Magento directory and run this command:

php bin/magento setup:install --base-url=http://localhost/magento24/ --db-host=localhost --db-name=yourdbname --db-user=yourdbuser --db-password=yourdbpassword --admin-firstname=admin --admin-lastname=admin --admin-email=admin@admin.com --admin-user=admin --admin-password=admin123 --language=en_US --currency=USD --timezone=America/Chicago --use-rewrites=1 --backend-frontname=admin --search-engine=elasticsearch7 --elasticsearch-host=localhost --elasticsearch-port=9200

Descriptions:

  • 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.

Upon successful installation, you will 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.

After this, we’ll have to make some additional tweaks in order to make Magento work with Windows, like so:

In app\etc\di.xml, replace Symlink with Copy

<virtualType name="developerMaterialization" type="Magento\Framework\App\View\Asset\MaterializationStrategy\Factory">
    <arguments>
        <argument name="strategiesList" xsi:type="array">
            <item name="view_preprocessed" xsi:type="object">Magento\Framework\App\View\Asset\MaterializationStrategy\Symlink</item>
            <item name="default" xsi:type="object">Magento\Framework\App\View\Asset\MaterializationStrategy\Copy</item>
        </argument>
    </arguments>
</virtualType>

In vendor\magento\framework\View\Element\Template\File\Validator.php, replace line 138 with:

$realPath = str_replace('\\', '/',$this->fileDriver->getRealPath($path));

Finally, run these commands:

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

Additional tweaks for Magento 2.4.2 and above

Starting from Magento 2.4.2, it’s now mandatory to serve content from from the /pub/ in order to ensure better security. This is why additional tweaks are required in order to make your Magento 2.4.2 version work properly.

Step 1: Copy the index.php and .htaccess files from /pub/ to your root folder.

Step 2: Find the below line in the index.php (in the root folder):

require __DIR__ . '/../app/bootstrap.php';

and replace it with

require __DIR__ . '/app/bootstrap.php';

Step 3: Go to your Magento 2 database you recently created (http://localhost/phpmyadmin) and find the table core_config_data.

In this table, you’ll want to insert these rows in:

PathValue
web/secure/base_static_urlhttp://localhost/magento24/pub/static/
web/unsecure/base_static_urlhttp://localhost/magento24/pub/static/
web/secure/base_media_urlhttp://localhost/magento24/pub/media/
web/unsecure/base_media_urlhttp://localhost/magento24/pub/media/
Replace magento24 with your Magento install directory
Insert new table database
Localhost database new row path and value

Your newly added rows should look something like this:

Now clear cache using:

php bin/magento cache:flush

And refresh your frontend. You should now be able to see a blank page like this when access your frontend via configured base-url:

Now you can access your Magento frontend with your configured base-url. For example, with our Magento 2.4.2 installation, we access our new Magento site at http://localhost/magento24/, and our backend at http://localhost/magento24/admin.

*Note: if you face this error when trying to log into your Magento Admin account: “You need to configure Two Factor Authorization…”

Magento backend two factor authentication error

Run this command:

php bin/magento module:disable Magento_TwoFactorAuth

Part 5: Import sample data (optional)

Run this command to import sample data:

php bin/magento sampledata:deploy

When prompted, enter your Magento authentication keys. Public and private keys are created and configured in your Magento Marketplace account.

Magento access keys

After importing data is complete, run:

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:clean

Now, refresh the frontend and you should be able to see the default Magento store with Luma theme and sample data.

Magento 2 Luma theme

That’s it! You have finished installing Magento 2 on your localhost. Now you can access your Magento 2 frontend/backend and start exploring Magento 2 features. We hope you find this post helpful!


Related posts:
Magento 2 System Requirements: The full checklist
How to Install Magento 2 Using Composer
How to Install Magento 2 on Ubuntu 16 Using Nginx
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
31 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Muhammad Amirul Iman
Muhammad Amirul Iman
21 days ago

This is one of the best tutorial to install Magento on a Windows Machine, It also included all the walls that i have been facing trying to run on Windows. Great Article Overall

James Auble
15 days ago

I have to agree but that isn’t saying much lol.

When I run

php bin/magento setup:upgrade

I get

Can’t run this operation: deployment configuration is absent. Run ‘magento setup:config:set –help’ for options.

Any ideas?

Nurhazwani Mat Salleh
Nurhazwani Mat Salleh
21 days ago

This content is very helpful. Thank you very much!

Monali Dhamale
Monali Dhamale
3 years ago

I have downloaded everything. I stuck this step
Step 3: Create a new empty database for Magento 2
On my laptop i don’t get it phpmyadmin page on browser.

Kislay Komal
2 years ago

Hi Dom, Indeed a great article. I followed the setps mentioned here to install Magento 2.3 on localhost. 1. Frontend: Able to see a page with body text “Home Page” when i open http://localhost/magento/ it looks fine, i am sure when there will be any template set to it, it will be full fledged page. 2. Admin backend panel: http://localhost/magento/admin_pgrz6u/ I can login and see some contents, but all the admin panel menus dont go anywhere, when i click them. neither of the hyperlinks, buttons works on that page so cant actually perform any setup, configuration and unable to move… Read more »

Megh
Megh
1 year ago
Reply to  Dom

Hi Dom,

The same problem happened with me also. I can login to admin but there is a spinner problem occured and I can not click on any link in admin

B
B
2 years ago

Been throwing my brain against this for hours today. Can get everything correct, but when trying step 4 to install magneto, i get this:
Autoload error
Vendor autoload is not found. Please run ‘composer install’ under application root directory.

However, composer is installed. I have tried every article “fix” found on the net, with no luck. Any adivce where to go next?

Kien
2 years ago

Hi Sir, I’ve followed all the steps shown above, but i get all these errors and page couldn’t load css/js files properly. (LOCALHOST) –Errors– 127.0.0.1/:1 Refused to apply style from ‘http://127.0.0.1/mage2/pub/static/version1549855875/frontend/Magento/luma/en_US/mage/calendar.css’ because its MIME type (‘text/html’) is not a supported stylesheet MIME type, and strict MIME checking is enabled. mixins.js:1 Failed to load resource: the server responded with a status of 404 (Not Found) require.js:1 Failed to load resource: the server responded with a status of 404 (Not Found) polyfill.js:1 Failed to load resource: the server responded with a status of 404 (Not Found) mixins.js:1 Failed to load resource: the… Read more »

Andrew
Andrew
1 year ago

Hi. I have a trouble with step 4 – when i try to request the url to download magento my browser says that object not found

komal arya
komal arya
1 year ago

Hello i installed the magento and everything was perfect but when click the admin link and it’s blank like black screen?

abdul muizz
abdul muizz
1 year ago
Reply to  komal arya

also got this problem

PAK BOY
PAK BOY
1 year ago
Reply to  abdul muizz

I was also getting the same Problem. But i fix it yesterday ( 2 NOV 2019 ). All you have to do is go to

#/vendor/magento/framework/View/Element/Template/File/Validator.php:140

Than open the validator file and change the line 140

if (0 === strpos($realPath, $directory)) {
return true;
}

To

$realDirectory = $this->fileDriver->getRealPath($directory);
if (0 === strpos($realPath, $realDirectory)) {
return true;
}

Than All you have to do is go to magento/pub/static folder and delete all the files except .htaccess. Because after login you’ll faced the css bug.

Peace From Pakistan

Mela
Mela
2 months ago
Reply to  PAK BOY

dude, you are my savior 🙂 I’ve stuck with this thing for 6 Hours ++ and almost give up to install this app. BIG THANKS TO YOU!

Xander marc Sususco
Xander marc Sususco
1 year ago

Can I ask the laptop or system requirements of magento2? I’am a college student and Ia want to use it in my thesis about ecommerce

Muhammad Arqam Awais
Muhammad Arqam Awais
8 months ago

I’m installing Magento 2.3.5, my installation process stuck at 51% and show Error message. Please help me how can I overcome this problem

Tommaso
Tommaso
2 months ago

Hi Dom, I’m thankful for your guide but I found some mistakes:
ONE: in point 4 you missed the php before bin/magento
SECOND: still in point 4 the correct line to replace in line 138 has to have 2 slashes:
str_replace(‘\\’,’/’ …

Last edited 2 months ago by Tommaso
Benoit
Benoit
1 month ago

Thanks for all those tips. They worked perfectly with Magento 2.4.1 and XAMPP but Magento 2.4.2 can’t work with XAMPP. Everything worked perfectly if we follow your tips and modify files on Magento 2.4.1 but they don’t work at all with Magento 2.4.2. As suggested by severaI people I also tried to enable libsodium.dll but it didn’t work. Everytime I get Error 404 Object not found!

Luke Vu
Editor
1 month ago
Reply to  Benoit

Hi Benoit,

We did run into the same issue and we’re working on a solution to fix this asap. Stay tuned!

JorgeCroce
JorgeCroce
28 days ago
Reply to  Benoit

The same for me. 2.4.2 is not working. Page not found result.

Benoit
Benoit
24 days ago
Reply to  JorgeCroce

Hi, I found the solution: first you need to understand Magento/Adobe made few important changes in this release: – There is no longer an index.php file in the root folder – The directory root is /pub folder (this is very important), good for security also Now you use phpMyAdmin and look for the table named: core_config_data and look for: web/unsecure/base_url and change http://localhost/nameofyourmagento/ to http://localhost/nameofyourmagento/pub/ and: web/secure/base_url and change https://localhost/nameofyourmagento/ to https://localhost/nameofyourmagento/pub/ Open you browser and to go to admin: http://localhost/nameofyourmagento/pub/name-of-your-secret-admin-folder/ To visit your website: http://localhost/nameofyourmagento/pub/ PS: In my example above I typed “nameofyourmagento” of course you understand you need… Read more »

Last edited 24 days ago by Benoit
Luke Vu
Editor
22 days ago
Reply to  Benoit

Hi Benoit, 

We tried your solution and it worked like a charm. We’ve updated our article with your solution so that future readers can have a way to get around this bug.

moby dick
3 days ago
Reply to  Benoit

dude, that’s not a solution!
who the hell want’s to have the store’s URL of the site ending with /pub?!

moby dick
3 days ago
Reply to  moby dick

and yes, I have managed to make it work, but due to the infra setup that I have, I’m having issues with using custom path for admin panel

Prajeesh
Prajeesh
21 days ago

Hi ,

I am getting 404 found issues on both the front and backend. I did all the steps except part 1 – step-4 because I am not confident to make the changes.

The current, host screenshot attached

Thanks
Prajeesh

hosts.PNG
Luke Vu
Editor
21 days ago
Reply to  Prajeesh

Hi Prajeesh,

This 404 not found issue is common on Magento 2.4.2 installations. The solution for this, as outlined in Benoit’s comment and in our tutorial, involves changing the base-url of your Magento installation to http[s]://<host or ip>/<your Magento install dir>/pub.
The /pub path is necessary as starting from Magento 2.4.2, due to security reasons, there’s no longer an index.php in the root directory.

Prajeesh
Prajeesh
21 days ago
Reply to  Luke Vu

Thanks, Luke Vu, I tried to open “http://localhost/magento24/pub/” – working, but without pub, its not working, Can you send me the .htaccess file for rererence.