Installing Magento 2 on Ubuntu remains one of the most reliable ways to build a scalable, enterprise-grade e-commerce store. Whether you’re a developer setting up a local environment or a systems administrator deploying a production server, getting the LAMP stack right and the file permissions correct is what separates a clean install from hours of troubleshooting. This guide covers the complete process — from Apache and PHP setup through MySQL configuration, Composer, and the final Magento 2 installation with sample data — using the commands that actually work in 2026.
Magento 2 is an Adobe-backed open-source platform trusted by thousands of businesses worldwide for its flexibility and deep customization. Unlike simpler platforms, it demands a specific server environment before a single line of its installer runs. Meeting those requirements up front is the only way to avoid cryptic errors mid-installation.
System Requirements Before You Begin
Before running any commands, confirm your Ubuntu server meets Magento 2’s minimum requirements. You need at least 2GB of RAM (4GB is strongly recommended for production), a 64-bit Linux environment, PHP 8.2 with a specific set of extensions, Apache 2.4 or Nginx 1.18+, and either MySQL 5.7+ or MariaDB 10.4+. Elasticsearch or OpenSearch is also required from Magento 2.4.x onward for catalog search functionality — skipping this step is one of the most common reasons installations fail silently.
Disk space matters too. A base Magento 2 installation without sample data consumes around 2GB, and adding sample data pushes that closer to 4–5GB once assets are deployed. Plan your partition sizes accordingly before you begin.
Step 1: Update Your System and Install Apache
Start by refreshing your package index and installing the Apache web server. Run the following in your terminal:
apt-get update
apt-get install apache2
Once Apache is installed, you need to enable the rewrite module, which Magento uses for its URL routing. Without it, every storefront and admin URL will return a 404. Open your terminal and run:
sudo a2enmod rewrite
sudo service apache2 restart
Next, open the Apache main configuration file and find the AllowOverride None directive inside the <Directory /var/www/> block. Replace it with AllowOverride All to allow Magento’s .htaccess rules to take effect.
sudo nano /etc/apache2/apache2.conf
Step 2: Install PHP 8.2 and Required Extensions
Magento 2.4.x in 2026 runs best on PHP 8.2. The Ondřej Surý PPA remains the most reliable source for current PHP packages on Ubuntu. Add it and install PHP along with all required extensions in a single pass:
add-apt-repository ppa:ondrej/php
apt-get update
apt-get install php8.2 libapache2-mod-php8.2 php8.2-common php8.2-gd php8.2-mysql php8.2-curl php8.2-intl php8.2-xsl php8.2-mbstring php8.2-zip php8.2-bcmath php8.2-soap php8.2-xml php-imagick
php -v
After installation, open the PHP configuration file for Apache and tune the following directives to match Magento’s requirements. These values directly affect how large catalog imports behave and how long complex operations are allowed to run:
sudo nano /etc/php/8.2/apache2/php.ini
Set these values:
memory_limit = 2G
max_execution_time = 3600
max_input_time = 1800
upload_max_filesize = 128M
zlib.output_compression = On
Restart Apache after saving so the new PHP settings take effect immediately.
Step 3: Install and Configure MySQL
Magento 2 works with both MySQL 5.7+ and MariaDB 10.4+. For Ubuntu in 2026, MariaDB is the preferred choice due to its performance improvements and better Magento compatibility. To install it:
sudo apt install -y mariadb-server mariadb-client
Start the service and run the secure installation script:
sudo service mysql start
sudo mysql_secure_installation
Follow the prompts to set a root password, remove anonymous users, disallow remote root login, and drop the test database. Once complete, log in and create a dedicated Magento database user — never use the root account for Magento directly:
sudo mysql -u root -p
CREATE DATABASE magento2;
CREATE USER 'magento'@'localhost' IDENTIFIED BY 'YourSecurePassword';
GRANT ALL PRIVILEGES ON magento2.* TO 'magento'@'localhost';
FLUSH PRIVILEGES;
EXIT;
If you encounter slow query issues later, locate the max_allowed_packet setting in /etc/mysql/mysql.cnf and increase it — this is especially important when importing large SQL dumps or sample data.
Step 4: Install Composer
Magento 2 uses Composer for all dependency management. The fastest way to get Composer 2 on Ubuntu is:
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
Verify the installation succeeded:
composer --version
You should see Composer 2.x in the output. Magento 2.4.x requires Composer 2.2 or later — if you see a version below that, update before proceeding.
Step 5: Set Up User and File Permissions
Running Magento as the root user is a security risk and will cause permission conflicts during setup. Create a dedicated user and add user to the www-data group so the web server and the Magento process share the same file ownership:
sudo usermod -a -G www-data your-username
Check that the group assignment worked correctly:
id your-username
groups your-username
Transfer ownership of the web root to your user under the www-data group:
sudo chown your-username:www-data -R /var/www/html
Correct file permissions are critical. Directories need 755 and files need 644 as the base, with specific Magento directories requiring 775 for the web server to write cache and generated files during operation.
Step 6: Download Magento 2 Using Composer
Navigate to your web root and use Composer to pull the latest Magento 2 release directly from the official Adobe repository:
cd /var/www/html
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition magento2
When prompted, enter your Magento Marketplace public and private keys as the username and password. If you don’t have these yet, create a free Adobe/Magento Marketplace account, navigate to My Profile, and generate a new set of access keys under the Access Keys tab.
Once the download completes, set the correct ownership and permissions on the new directory:
sudo chown -R www-data:www-data /var/www/html/magento2
sudo find /var/www/html/magento2 -type d -exec chmod 755 {} \;
sudo find /var/www/html/magento2 -type f -exec chmod 644 {} \;
Step 7: Install phpMyAdmin (Optional)
If you prefer a graphical interface for managing your Magento database, download phpMyAdmin from the official site and extract it into /var/www/html/phpmyadmin. Make sure to restrict access to it by IP in your Apache configuration — leaving a public phpMyAdmin installation exposed is a significant security vulnerability that attackers actively scan for.
Step 8: Run the Magento 2 Installer
With the stack in place, run the Magento setup command from within the magento2 directory. Replace the placeholder values with your actual domain, database credentials, and admin details:
cd /var/www/html/magento2
sudo -u www-data php bin/magento setup:install \
--base-url=http://your-domain.com/ \
--db-host=127.0.0.1 \
--db-name=magento2 \
--db-user=magento \
--db-password=YourSecurePassword \
--admin-firstname=Admin \
--admin-lastname=User \
--admin-email=admin@your-domain.com \
--admin-user=admin \
--admin-password=Admin123! \
--language=en_US \
--currency=USD \
--timezone=America/Chicago \
--use-rewrites=1 \
--search-engine=elasticsearch7 \
--elasticsearch-host=localhost \
--elasticsearch-port=9200
The installer will run for several minutes. When it completes, it will output your admin panel URL — save this immediately. After the setup finishes, deploy static content so your storefront renders correctly:
php bin/magento setup:static-content:deploy -f
Step 9: Install Magento 2 Sample Data
Sample data gives you a fully populated store with products, categories, CMS pages, and customer accounts — essential for development and testing. The cleanest way to add it is via the Magento CLI before or after running setup:install. From your Magento root directory, run:
sudo -u www-data php bin/magento sampledata:deploy
Composer will pull the sample data modules from the Magento repository. Once complete, run the upgrade and reindex commands to register everything:
sudo -u www-data php bin/magento setup:upgrade
sudo -u www-data php bin/magento indexer:reindex
sudo -u www-data php bin/magento cache:flush
If sample data deployment fails with an authentication error, confirm that your Magento Marketplace keys are correctly stored in your global Composer auth file at ~/.composer/auth.json. Missing or expired keys are the most common cause of this failure.
Step 10: Post-Installation Configuration
After a successful install, a few final steps make your Magento 2 environment stable and production-ready. First, switch Magento out of developer mode and into production mode to enable full-page caching and compiled assets:
sudo -u www-data php bin/magento deploy:mode:set production
Set up a cron job for the www-data user to handle scheduled tasks like catalog price rules, newsletters, and indexing — Magento’s automated processes depend entirely on cron running reliably. Add the following to your crontab:
* * * * * /usr/bin/php /var/www/html/magento2/bin/magento cron:run 2>&1 | grep -v "Ran jobs by schedule"
Finally, if you plan to use Magento’s built-in email marketing features or integrate with third-party marketing tools, configure your SMTP settings under Stores → Configuration → Advanced → System before going live.
Pro Tips for a Smooth Magento 2 Installation
Always run Magento CLI commands as the www-data user using sudo -u www-data, not as root. Running as root creates files owned by root inside the Magento directory, which Apache cannot write to — this breaks cache generation and causes cryptic 500 errors that are painful to diagnose after the fact.
Use Elasticsearch or OpenSearch rather than MySQL for search. Magento 2.4.x made search engine integration mandatory, and trying to disable or work around it adds complexity without any real benefit. Install Elasticsearch 7.x alongside your LAMP stack from the start and you’ll never hit the common “No search engine configured” error.
Keep your PHP memory limit at 2G during development. Magento’s DI compilation and static content deployment are memory-intensive operations. Dropping below 756M will cause silent failures mid-process that are difficult to trace back to a memory limit issue.
When deploying sample data, make sure Composer’s authentication is stored globally in ~/.composer/auth.json, not just in the project directory. If you switch users mid-installation — which is common when moving between root and www-data — the local auth file won’t be found and the download will fail with a 403.
Always run setup:upgrade the correct way: sudo -u www-data php bin/magento setup:upgrade. Skipping the user flag is one of the most common causes of broken permission trees in Magento installations and creates hours of cleanup work.
If you’re building a development environment rather than a production server, enable Xdebug only when actively debugging. Leaving it on permanently adds significant overhead to every PHP request and makes Magento feel sluggish in ways that mask real performance issues in your code.
Back up your app/etc/env.php file immediately after a successful installation. This file contains your database credentials, encryption key, and backend URL. Losing it means losing access to your entire Magento installation.
Frequently Asked Questions
What PHP version does Magento 2 require in 2026?
Magento 2.4.7 and later support PHP 8.2 and PHP 8.3. PHP 7.x is no longer compatible with current Magento releases. Always match your PHP version to the specific Magento release you are installing — the official Adobe compatibility matrix is the authoritative reference.
Can I install Magento 2 without Composer?
You can download a ZIP archive of Magento directly from the Adobe Commerce Marketplace, but using Composer is strongly recommended. Composer handles all dependency resolution automatically, makes upgrades significantly easier, and is the method used in every professional Magento environment.
Why does Magento 2 require Elasticsearch?
From version 2.4.0 onward, Adobe removed the MySQL-based search engine from Magento’s core. Elasticsearch and OpenSearch are now the only supported search backends. This change was driven by performance — Elasticsearch handles large catalog searches far more efficiently than MySQL ever could.
How long does a full Magento 2 installation with sample data take?
On a modern server with a fast internet connection, the Composer download takes 10–20 minutes depending on your connection speed. The setup:install command runs for 3–8 minutes. Deploying sample data adds another 10–15 minutes. Budget around 45 minutes for a complete installation from a clean Ubuntu server.
What is the default Magento 2 admin URL?
Magento 2 generates a randomized admin URL suffix by default for security reasons — for example, yourstore.com/admin_abc123. The exact path is displayed at the end of the setup:install output. You can also find or change it in app/etc/env.php under the backend key.
How do I fix “insufficient permissions” errors after installation?
The most common cause is running Magento CLI commands as root instead of the www-data user. Reset ownership with sudo chown -R www-data:www-data /var/www/html/magento2, then re-run the failed command as www-data. If the issue persists, check that your web server user and the user running CLI commands are in the same group.
Is Magento 2 free to install?
Magento Open Source is completely free to download and use. Adobe Commerce, the enterprise tier, requires a paid license with pricing that varies based on your annual gross merchandise value. For most small to mid-sized businesses and developers, the open-source edition provides everything needed to run a professional store, including the headless commerce architecture that modern storefronts increasingly rely on.
Conclusion
Installing Magento 2 on Ubuntu in 2026 is a structured process that rewards careful preparation. Getting Apache, PHP 8.2, MariaDB, Elasticsearch, and Composer in place before touching the Magento installer is what separates a clean, working environment from one that produces mysterious errors at every turn. The commands in this guide reflect current best practices for a real-world installation — not a simplified demo setup.
Sample data transforms a blank Magento install into a fully explorable store environment, which is invaluable for development, theme work, and extension testing. Once your store is running, the post-installation steps — production mode, cron, and SMTP — are not optional extras. They are the difference between a functional development environment and a store that is ready for real traffic.
Whether you are setting up a local development environment or deploying a client production server, the fundamentals covered here apply directly. Follow the permission model, use Composer for all package management, run CLI commands as www-data, and your Magento 2 installation will be stable, maintainable, and ready to scale.
