Magento 2 has very specific server requirements that require a lot of server configuration and tweaking, along with setting up Composer (not a default option on many platforms). In this tutorial, I will show you how to install and set up Magento 2 on a localhost using XAMPP.
XAMPP is a free and open-source cross-platform web server solution that allows you to run Magento 2 on your local machine for development and testing purposes. By the end of this tutorial, you will have a fully functional Magento 2 installation running on your localhost.
- Benefits of Magento 2 on Localhost
- Magento 2 Prerequisites for Localhost
- Install and Set up XAMPP
- Create a Database in phpMyAdmin
- Install Composer for Magento
- Download Magento Using Composer
- Install Elasticsearch for Magento 2
- Install Magento 2 Using Command Line
- Common Issues With Magento 2 Versions
Benefits of Magento 2 on Localhost
These are the following benefits of installing Magento 2 on a localhost:
- Running Magento 2 on localhost eliminates the need for a dedicated hosting plan, which can be costly.
- A local installation of Magento 2 allows you to work on your website offline without an internet connection.
- A localhost installation allows developers to test new features and extensions before deploying them to a live site.
- A localhost installation allows you to customize your website without the risk of breaking the live site.
- Localhost environments typically perform faster than remote servers because there is no latency from network communication.
- By running Magento 2 on a localhost, you can protect your store from unauthorized access and hacking attempts.
- A localhost installation allows you to easily back up and restore your website in case of a problem.
Experience the Cloudways Magento 2 Demo Store – No tech skills needed!
Experience a fully functional Magento 2 store built on top of renowned Cloudways hosting to deliver the fastest speeds.
Magento 2 Prerequisites for Localhost
Before installing Magento using Composer, you should check the prerequisites and system requirements that need to be installed.
- Apache/NGINX Version 2.2 or 2.4
- PHP Version 7.4.x or 8.1.x
- MySQL/MariaDB Version 5.6.x, 5.7.x or Maria DB
- Elasticsearch 7.6.x or higher
Take your Magento ecommerce store to the next level with Cloudways.
Get started today and experience lightning-fast loading speeds, seamless scalability, and 24/7 expert support.
Install and Set up XAMPP
I will cover the installation of Magento 2 on localhost so that you will not face any issues while following the process.
Step: 1 Download XAMPP Server
Go to Apache Friends and Download XAMPP Server.
Magento strongly recommends installing XAMPP with PHP version 8.1.x for M2.4.5. Also, you can install Magento 2.4 with PHP 7.3, but it is not tested or recommended.
Step: 2 Install the XAMPP Server
After downloading XAMPP, please double-click on the file to install it on your computer.
Click the Next button.
Leave the default selection as in the image. Click the Next button.
Choose your installation folder. The default location is C:\xampp, but selecting a directory other than C:\ drive like D:\ or E:\ as per your convenience is recommended.
If you need to change, click the Browse icon to change your destination for the XAMPP program, and then click the Next button to go to the next step.
Click the Next button.
Click the Next button.
Please wait until the setup wizard installs XAMPP on your computer.
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.
Step: 3 Configure PHP
In XAMPP Control Panel, click the Config button on the Apache row, and click PHP (php.ini).
In the php.ini file, find these rows and remove semicolons (😉 before each row:
;extension=intl
;extension=soap
;extension=sockets
;extension=sodium
;extension=xsl
;extension=gd
Extend the memory_limit to 4G:
memory_limit=4G
After you’ve done, save and close the file. Then, click the Start button on 2 rows of Apache and MySQL to start them. Don’t quit XAMPP after this step, just let it run.
Step: 4 Configure Hosts File
Access to the directory C:\Windows\System32\drivers\etc\hosts.
Add the following line to the last row:
127.0.0.1 www.localhost.com
Open the XAMPP Control Panel and start Apache and MySQL applications.
Create a Database in phpMyAdmin
Access the URL http://localhost/phpmyadmin, to create a database. Put in a database name (for example, Magento2) and click Create.
Click the Next button to create the database.
Database created successfully.
Note: Remember the name of the database you would create as you’ll want to install Magento on it later on.
Install Composer for Magento
The latest version of Magento, open source 2.4.0 onwards, is compatible with Composer 2. So you need to install Composer 2 to download Magento 2 with the latest version.
The best way to do this is to download and run Composer-Setup.exe. This will install the latest version and set up your path to “call” Composer from any directory.
Click the Next button and browse the PHP path in the XAMPP folder.
It’s optional to enter your proxy URL.
Now click the Install button to initiate the final step.
Click the Next button.
Click the Finish button.
Launch the Command Prompt and run the Composer command to verify that it has been installed without any issues.
Download Magento Using Composer
Next, cd to C:\xampp\htdocs and run the following command using Composer to Download a specific Magento 2 version.
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.5 <install-directory-name>
Magento Open Source
composer create-project --repository-url=https://repo.magento.com/magento/project-community-edition:2.4.5
Adobe Commerce (Enterprise)
composer create-project --repository-url=https://repo.magento.com/magento/project-enterprise-edition:2.4.5
By the above command, you can specify the specific version of Magento (2.0, 2.1, 2.2.1, 2.2.2, 2.2.3, 2.2.4, 2.2.5, 2.2.6, 2.3, 2.4). If not, skip the version and just run the command below:
Next, enter your Magento authentication keys. Public and Private Keys are created and configured in your Magento marketplace account.
Navigate to the Account name in the top right of the page and select My Profile.
Next, click on the Access keys from the Marketplace tab.
Click Create a New Access Key. Enter a specific name for the keys (for example, the name of the developer receiving the keys) and click OK.
Copy the Public Key in the username and the Private Key in the password to install Magento.
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:
Install Elasticsearch for Magento 2
Elasticsearch is a highly recommended extension for Magento 2.4.X versions.
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
You can also check the availability of Elasticsearch on port:9200. Type local:9200 in the browser, and you will get the details of the installed Elasticsearch extension.
{ "name" : "DESKTOP-Jyoti", "cluster_name" : "elasticsearch", "cluster_uuid" : "ONNrUlTWQCSoigYBy6WF9g", "version" : { "number" : "7.9.3", "build_flavor" : "default", "build_type" : "zip", "build_hash" : "c41E4fgiueQSoiytrBW", "build_date" : "2023-02-02T09:51:41.449Z", "build_snapshot" : false, "lucene_version" : "8.7.0", "minimum_wire_compatibility_version" : "6.8.3", "minimum_index_compatibility_version" : "6.8.3" }, "tagline" : "You Know, for Search" }
Install Magento 2 Using Command Line
Open the command prompt window. Next, cd to your Magento directory and execute the command:
php bin/magento setup:install --base-url="http://localhost/magento2/" --db-host="localhost" --db-name="dbmagento241" --db-user="root" --admin-firstname="admin" --admin-lastname="admin" --admin-email="[email protected]" --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
Please note: Assign the base URL per your path, note down the admin username and password, and do not use the Elasticsearch port.
Replace these values:
- –base-url: your magento 2 installation folder
- –db-name: your database name while creating the database
- –db-password: your database password
- –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: Admin first name
- –admin-lastname: Admin lastname
- –admin-email: Admin 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
Other values are optional, we can change them later.
Now, if everything is done properly, Composer 2 will start to install Magento 2. The process will last for around 10 minutes.
Be patient, as it could take some time to install all instances and required modules to set up Magento 2.4.5.
After the installation, you will end up with these results.
Run the Commands
Now, you need to run the following commands.
php bin/magento indexer:reindex php bin/magento setup:upgrade php bin/magento setup:static-content:deploy -f php bin/magento cache:flush
Now hit the URL (usually localhost/magento2/pub/) or the base URL you set earlier for the in base-url during the installation.
For Admin: Access the url with http://localhost/magento2/pub/admin.
Congratulations!
You have now successfully installed Magento 2.4.5-p1 on your localhost. You can start making your preferred customizations and launch a fully functional ecommerce store on the local machine.
Common Issues With Magento 2 Versions
There is a chance that you might encounter an issue during the Magento 2.4.1 or below versions of the Magento 2.4.5 installation process. Here are some of the most common ones, along with a solution.
1. Blank Grey Page – Magento Admin Panel
All you have to do is go to the directory path …\vendor\magento\framework\View\Element\Template\File\Validator.php
In the function isPathInDirectories, add a line.
$realPath = str_replace('\\', '/', $this->fileDriver->getRealPath($path));
Refer to the following image for the point where you need to insert the line:
2. If You Get an Authentication Error
If you couldn’t log in to the Magento Admin Panel and encounter an authentication error, simply run the following command in the Composer:
php bin/magento module:disable Magento_TwoFactorAuth
Summary
So that’s how you can install Magento 2.4.5-p1 on the localhost using the Composer. Once you are ready to take your store live, you don’t need to worry about performance because Cloudways Managed Magento Hosting is here to increase your sales and remove server-side management hassles. If you face any problem like a 500 Internal Server Error after implementing the above steps, please check here to fix the error.
Just focus on building an awesome Magento 2 store, and Cloudways will take care of the rest. And if you have any issues while installing Magento 2, you can go ahead and ask me via the comments section.
Jyotishna Kumari
Jyotishina is the Magento Community Expert at Cloudways and has 4 years of experience in web development. She has worked on e-commerce sites since the turn of the millennium and was working with Magento before version 1 was released. She loves to travel and explore new ideas whenever she finds time. Get in touch with her at [email protected].