Essential Steps for Setting Up a Magento 2 Local Development Environment

Essential Steps for Setting Up a Magento 2 Local Development Environment

We have noticed in the past that many developers reached out to us with the questions like how to setup Magento 2 in localhost or how to install Magento 2 in localhost as it allows software developers to efficiently build, test, and debug their applications on their own machines before deploying them to a production environment so that they can work on this replica in a controlled and familiar setting.

Magento development environment includes all the necessary components and tools required to run an application, such as a web server, a database server, and other dependencies, that’s why it is one of the renowned, feature-rich, high-performing open-source e-commerce platform trusted by brands like Jaguar UK, Panasonic, and Vitamix.

It offers robust security and versatility for developing online stores.

While hosting on cloud servers is optimal, Magento 2 can also be installed locally on an XAMPP server for development and testing.

This guide will help on how to install Magento 2 on XAMPP, a free cross-platform web server solution. Ideal for beginners and experts, it simplifies local development and testing, allowing exploration of Magento 2’s powerful features.

Why Set Up Magento 2 on Localhost?

Installing Magento 2 on localhost provides a safe, versatile environment for developers and designers to build, test, and modify without impacting live websites.

Here are the detailed benefits:

  • Testing New Updates: Whenever a new Magento version, feature, or security patch is released, you can create a local test environment to verify its stability and compatibility before applying it to your live site. This minimizes risks and ensures smooth integration.
  • Safe Modifications for Client Requirements: Making changes directly on a production website can lead to unexpected issues.

With a localhost staging environment, you can safely implement modifications, test them thoroughly, and resolve any errors. Once everything works perfectly, you can confidently deploy the updates to the live server.

  • Version Experimentation: A local setup allows developers to work on multiple Magento versions simultaneously, testing functionalities, extensions, and themes without affecting live operations.
  • Enhanced Control and Customization: A local setup allows developers to fully customize their workspace.

This includes installing necessary tools, extensions, and configuring settings tailored to their specific project requirements.

  • Streamlined Debugging: When an issue arises, through the Magento development environment, developers can directly access components and thoroughly investigate the problem for efficient troubleshooting.
  • Offline Functionality: Developers can write, compile, and run code locally without requiring an internet connection.

However, pushing changes to a version control system will require connectivity.

By setting up Magento 2 on localhost, you enhance productivity, ensure a secure development workflow, and reduce the risks associated with live site changes.

Magento 2 Installation Requirements

Before installing, ensure your system meets these specifications:

  • Web Servers: Apache/Nginx 2.2 or 2.4
  • PHP Versions: 7.4.x or 8.1.x
  • Database: MySQL/MariaDB 5.6.x or higher
  • Search Engine: Elasticsearch 7.6.x or higher

Installing XAMPP Server

Follow all the below steps to know how to install Magento 2 on XAMPP.

  1. Download XAMPP from the Apache Friends site, ensuring compatibility with your OS.
  2. Install XAMPP, selecting your desired components and installation folder.
  3. Launch the XAMPP control panel to manage your server environment.

This comprehensive guide ensures you can confidently install Magento 2 locally, enabling a secure and efficient development process.

Configuring XAMPP for Magento 2 Installation

To prepare XAMPP for installing Magento 2, follow these steps for a seamless setup process.

1. Starting XAMPP Services

  • Launch the XAMPP control panel.
  • Start the Apache and MySQL services by clicking the “Start” button next to each.
  • Ensure the status indicators turn green, showing both services are running smoothly.

2. PHP Configuration

  • In the XAMPP control panel, click on the “Config” tab under Apache.
  • Open the php.ini file in a text editor.
  • Uncomment extensions like gd, intl, soap, sockets, sodium, and xsl by removing the semicolon (;).

Increase the memory limit to 4G: memory_limit = 4G

  • Save the changes, then restart Apache and MySQL services.

3. Creating a Database

  • Open http://localhost/phpmyadmin in your browser.
  • Log in and navigate to the “Database” tab.
  • Enter a name for your database and click “Create.”
  • Optionally, define tables and columns as needed.

4. Downloading and Installing Composer

  • Visit getcomposer.org and download the Composer setup file.
  • Run the installer and select “Install for all users.”
  • Specify the PHP path within your XAMPP directory and check the option to add PHP to your system path.
  • Skip the proxy settings and proceed with the installation.
  • Complete the setup and verify installation by running composer in Command Prompt.

With these steps, XAMPP and Composer will be configured for Magento 2, enabling efficient local development.

Download and Install Elasticsearch for Magento 2

To install Elasticsearch, a Magento 2 prerequisite, follow these steps:

1. Download Elasticsearch

2. Set Up Elasticsearch

Open the Command Prompt and navigate to the extracted folder using the cd command: cd directory-path

Install Elasticsearch by running: .bin\elasticsearch.bat

3. Verify Installation

  • Open your browser and enter localhost:9200 to check Elasticsearch availability.
  • If it loads successfully, Elasticsearch is installed and running.

Download Magento 2 Using Composer

1. Navigate to Desired Directory

Open the Command Prompt and use the cd command to access the directory for your Magento application: cd directory-path

Example: cd C:\xampp\htdocs\magentoprojects

2. Install Magento with Composer

Run the following command to install your desired Magento version:
composer create-project –repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.5

3. Authenticate Magento Repository

  • When prompted, log in to your Magento Marketplace.
  • Navigate to the Access Keys page and create new keys.
  • Use the Public Key as the username and Private Key as the password in the terminal.

4. Complete Installation

  • Save the credentials if prompted by entering Y.
  • The command will start downloading Magento.
  • Once completed, Magento is successfully downloaded using Composer.

These steps set up Elasticsearch and Magento for local development effectively.

Install Magento 2 on Localhost Using Command Line

For how to install Magento 2 on localhost using the command line, follow these detailed steps:

1. Modify Gd2.php File

Before executing the installation command:

  • Navigate to vendor/magento/framework/Image/Adapter/Gd2.php in your project directory.

Replace:
if ($url && isset($url[‘scheme’]) && !in_array($url[‘scheme’], $allowed_schemes))

With:
if ($url && isset($url[‘scheme’]) && !in_array($url[‘scheme’], $allowed_schemes) && !file_exists($filename))

  • This fixes potential errors related to data patches during installation.

2. Run Magento Installation Command

Open the command prompt and execute the following:

php bin/magento setup:install –base-url=”http://localhost/project-community-edition/” –db-host=”localhost” –db-name=”magento2app” –db-user=”root” –admin-firstname=”admin” –admin-lastname=”panel” –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” –search-engine=elasticsearch7 –elasticsearch-host=”localhost” –elasticsearch-port=9200

Key Parameters to Customize:
  • –base-url: Path to your Magento 2 installation folder.
  • –db-name: Desired database name.
  • –db-user/db-password: Database user and password.
  • –admin-user/admin-password: Credentials for Admin Panel.
  • –language: Specify the storefront language (e.g., en_US).
  • –timezone: Set your local timezone.
  • –search-engine: Use Elasticsearch version (default is Elasticsearch 7).

3. Finalize Installation

When the installation completes successfully, execute the following commands to finalize the setup:

php bin/magento indexer:reindex

php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy -f

php bin/magento cache:flush

4. Access Magento

  • Storefront: Navigate to http://localhost/projectdirectoryname/pub/.
  • Admin Panel: Use http://localhost/projectdirectoryname/pub/admin.

Conclusion

Through this blog, Rock technolabs content development team tried our best to explain how to setup Magento 2 in localhost and why it is important to work in a Magento development environment.

Besides this, we just wanted to let you know that local development environments can vary depending on the specific needs and preferences of developers.

So in conclusion, setting up a Magento 2 local development environment provides developers with a safe and flexible space to test, experiment, and customize e-commerce solutions.

This guide has detailed every step—from installing XAMPP, configuring PHP, and setting up Elasticsearch to downloading and installing Magento 2 using Composer and the command line.

By following these instructions, you ensure a seamless setup process, allowing you to explore Magento’s capabilities effectively. Whether for learning or development, a local Magento 2 environment is an essential tool for any developer.

author-img

Dhiren is Digital Marketing Manager and also Magento Support and Solutions Provider at Rock Technolabs. He is passionate about anything related to Digital Marketing. He is passionate about using the power of search to help local businesses succeed.

Leave a Reply

Your email address will not be published. Required fields are marked *