Website Vidya
Linux & ServersStep-by-Step Guide

How to Install Magento 2 on Ubuntu 24.04

Step-by-step tutorial on how to install Magento 2 on Ubuntu 24.04 LTS with Apache, Elasticsearch, MariaDB, PHP 8.2, and Composer for eCommerce.

RR
Rajesh RayLead Systems Educator
Published:
6 min read
PDF Cheat Sheet
How to Install Magento 2 on Ubuntu 24.04
AI Executive SummaryWebsiteVidya AI

6 min read

Step-by-step tutorial on how to install Magento 2 on Ubuntu 24.04 LTS with Apache, Elasticsearch, MariaDB, PHP 8.2, and Composer for eCommerce.

Key Highlights
  • Prerequisites and Supported Releases
  • Step 1: Updating System Packages and Installing OpenSearch or Elasticsearch
  • Step 2: Installing Apache and MariaDB
Grounded in full engineering runbookWebsiteVidya AI Engine

Magento 2 (Adobe Commerce Open Source) is the premier enterprise eCommerce platform in the digital commerce industry, powering high-growth global retailers, B2B distributors, and multi-channel marketplaces. With its modular architecture, sophisticated catalog hierarchy, multi-warehouse inventory management, and extensive API framework, Magento provides unmatched scalability for high-transaction stores.

Deploying Magento 2 requires a well-architected software stack: in addition to a standard web server and database, Magento requires an enterprise search engine (Elasticsearch or OpenSearch) for catalog indexing and product searches. Learning how to install magento 2 on ubuntu 24.04 enables systems engineers and merchants to establish a robust, scalable digital commerce platform on Linux.

In this comprehensive step-by-step tutorial, we cover the full installation of Magento 2 on Ubuntu 24.04 LTS (Noble Numbat). We guide you through installing Apache, MariaDB, and PHP 8.2, deploying and configuring Elasticsearch, creating an optimized database, installing Magento using Composer, configuring Virtual Hosts and SSL, and running the command-line setup tool.

Advertisement

Prerequisites and Supported Releases

This guide is thoroughly verified on Ubuntu 24.04 LTS (Noble Numbat). The instructions apply cleanly to cloud instances (AWS EC2, Google Compute Engine, DigitalOcean, Hetzner), bare-metal servers, and local virtual machines.

Before proceeding, ensure you have the following requirements satisfied:

  • A virtual or dedicated server running Ubuntu 24.04 LTS.
  • A non-root user with administrative sudo privileges.
  • A registered domain name (e.g., commerce.example.com) pointed to your server IP address.
  • At least 4 GB of RAM (8 GB strongly recommended; Magento and Elasticsearch are resource-intensive).

Step 1: Updating System Packages and Installing OpenSearch or Elasticsearch

Magento 2 strictly requires Elasticsearch or OpenSearch as its catalog search engine. The Magento installation will refuse to complete if an active search instance is unreachable.

Update your package repository lists and install OpenSearch (or Elasticsearch 7.x/8.x) from its official repository.

Advertisement

Verify that the search service is running and responding on HTTP port 9200.

# Update repository index
sudo apt update && sudo apt upgrade -y

# Install prerequisite tools
sudo apt install -y curl gnupg apt-transport-https

# Test Elasticsearch / OpenSearch connectivity on port 9200
curl -X GET 'http://localhost:9200/'

Step 2: Installing Apache and MariaDB

Magento 2 relies on Apache (with mod_rewrite and mod_ssl) and a high-performance relational database.

Install apache2 and mariadb-server using APT.

Tune MariaDB settings in /etc/mysql/mariadb.conf.d/50-server.cnf for Magento's heavy indexing workload by setting max_allowed_packet = 128M and innodb_buffer_pool_size = 2G (or 50% of system RAM).

Advertisement

Restart MariaDB to apply the memory optimizations.

# Install Apache and MariaDB
sudo apt install -y apache2 mariadb-server mariadb-client

# Enable and verify services
sudo systemctl enable --now apache2 mariadb

Step 3: Installing PHP and Required Magento Extensions

Magento 2.4.x requires PHP 8.1 or 8.2 along with a comprehensive suite of extensions for XML handling, cryptographic hashing, image processing, and internationalization.

Install PHP and required modules: libapache2-mod-php, php-mysql, php-curl, php-gd, php-intl, php-mbstring, php-xml, php-zip, php-soap, php-bcmath, and php-opcache.

Tune php.ini parameters: set memory_limit = 2G, max_execution_time = 1800, and zlib.output_compression = On.

Restart Apache to load the new PHP configuration.

# Install PHP and Magento required extensions
sudo apt install -y php libapache2-mod-php php-mysql php-curl php-gd php-intl php-mbstring php-xml php-zip php-soap php-bcmath php-opcache

# Restart Apache
sudo systemctl restart apache2

Step 4: Installing Composer Globally

Magento 2 is distributed and managed exclusively via Composer. Composer resolves dependencies, installs third-party modules, executes core upgrades, and generates class maps for rapid dependency injection autoloading.

Download and install Composer 2 globally into /usr/local/bin/composer. Ensure you are running Composer version 2.2 or later, as Magento 2 requires Composer 2 plugin API compatibility.

Verify the installed Composer version and ensure that memory limits are appropriately relaxed when running Composer commands by exporting COMPOSER_MEMORY_LIMIT=-1 if necessary.

# Download Composer installer
curl -sS https://getcomposer.org/installer | php

# Move to system path
sudo mv composer.phar /usr/local/bin/composer

# Verify Composer version
composer --version

Step 5: Creating the Magento MariaDB Database and User

Set up a dedicated database and database user inside MariaDB for Magento.

Log into the MariaDB shell as root.

Create the database magento_db with utf8mb4 encoding, create user magento_user, grant full privileges, and flush privileges.

Replace StrongMagentoPass123! with a strong, randomized password.

-- Access MariaDB root shell
sudo mariadb

-- Create Magento database
CREATE DATABASE magento_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- Create dedicated database user
CREATE USER 'magento_user'@'localhost' IDENTIFIED BY 'StrongMagentoPass123!';

-- Grant database privileges
GRANT ALL PRIVILEGES ON magento_db.* TO 'magento_user'@'localhost';

-- Apply changes and exit
FLUSH PRIVILEGES;
EXIT;

Step 6: Deploying Magento 2 via Composer

To download Magento Open Source via Composer, you need an Adobe Commerce authentication key pair (Public Key as username, Private Key as password), which is free to generate in your Adobe account portal under Commerce Marketplace.

Create your project root directory at /var/www/magento.

Run the Composer create-project command to fetch the core Magento Open Source packages. When prompted for credentials, paste your Adobe Public Key as the username and Private Key as the secret password.

Set file ownership to www-data and assign appropriate executable permissions to the bin/magento CLI tool so that CLI maintenance and compilation tasks can proceed without privilege errors.

# Navigate to web root and create Magento project
cd /var/www
sudo composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition magento

# Set directory permissions
sudo chown -R www-data:www-data /var/www/magento
sudo chmod -R 775 /var/www/magento
sudo chmod +x /var/www/magento/bin/magento

Step 7: Configuring Apache Virtual Host for Magento 2

For maximum security, Magento 2 uses the pub/ subdirectory as its document root. Directing traffic to /var/www/magento/pub prevents unauthorized visitors from reading core code, vendor modules, or configuration files.

Create /etc/apache2/sites-available/magento.conf and define the document root to /var/www/magento/pub.

Enable mod_rewrite, activate the virtual host, and restart Apache.

# Create Apache Virtual Host configuration
sudo bash -c 'cat <<EOF > /etc/apache2/sites-available/magento.conf
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/magento/pub
    ServerName commerce.example.com

    <Directory /var/www/magento/pub>
        Options -Indexes +FollowSymlinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/magento_error.log
    CustomLog ${APACHE_LOG_DIR}/magento_access.log combined
</VirtualHost>
EOF'

# Enable site and rewrite module
sudo a2enmod rewrite headers
sudo a2ensite magento.conf
sudo a2dissite 000-default.conf
sudo systemctl restart apache2

Step 8: Executing Magento CLI Setup and Configuring Cron

Execute Magento's command-line setup script to populate database tables, configure the Elasticsearch endpoint, and create the initial administrator account.

Run bin/magento setup:install with your database and domain parameters.

After installation finishes, secure the domain with Let's Encrypt SSL via Certbot.

Finally, configure Magento's background cron jobs by executing sudo -u www-data bin/magento cron:install.

# Run Magento CLI setup
cd /var/www/magento
sudo -u www-data bin/magento setup:install \
    --base-url="https://commerce.example.com/" \
    --db-host="localhost" \
    --db-name="magento_db" \
    --db-user="magento_user" \
    --db-password="StrongMagentoPass123!" \
    --admin-firstname="Admin" \
    --admin-lastname="User" \
    --admin-email="[email protected]" \
    --admin-user="magento_admin" \
    --admin-password="AdminPassword123!" \
    --language="en_US" \
    --currency="USD" \
    --timezone="UTC" \
    --use-rewrites="1" \
    --search-engine="elasticsearch7" \
    --elasticsearch-host="localhost" \
    --elasticsearch-port="9200"

# Install Magento cron jobs
sudo -u www-data bin/magento cron:install

Troubleshooting and Common Errors

When configuring Ubuntu servers, unexpected permission errors, network timeouts, or lock contention can occur. Review these common issues and verified resolutions:

Installation halts with 'Could not validate a connection to Elasticsearch'

Resolution: Ensure Elasticsearch or OpenSearch is running: sudo systemctl status elasticsearch. Verify that port 9200 responds with curl http://localhost:9200 before running the setup command.

CSS and JavaScript files return 404 in Magento admin

Resolution: Deploy static view files and reindex the store by executing: sudo -u www-data bin/magento setup:static-content:deploy -f && sudo -u www-data bin/magento indexer:reindex.

Permission denied errors during cache flushing

Resolution: Reset file permissions for the www-data user: sudo chown -R www-data:www-data /var/www/magento/var /var/www/magento/pub/static /var/www/magento/generated.

Security Best Practices and Hardening

Always set the Apache DocumentRoot strictly to /var/www/magento/pub. Exposing the project root directory allows attackers to download app/etc/env.php and read encryption keys.

Never run Magento CLI commands as root. Always execute bin/magento commands as the www-data web server user.

Enable Two-Factor Authentication (2FA) for all administrative accounts immediately after installation.

Change the default admin URI from /admin to a randomized path in app/etc/env.php to prevent automated brute-force attacks.

Deepen your Linux system administration skills with our related production-tested guides:

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

3 FAQs
Magento 2 completely delegates catalog search, layered category navigation, and filter queries to Elasticsearch. Without an active search engine, catalog indexing and frontend product navigation will not function.
Run sudo -u www-data bin/magento deploy:mode:set production. Production mode compiles dependency injection, deploys minified static assets, and disables automatic code regeneration for maximum performance.
Magento 2 requires a minimum of 4 GB of RAM (8 GB strongly recommended), a multi-core CPU, and fast SSD storage due to intensive PHP compilation and Elasticsearch indexing operations.
RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout