Skip to content

Installing Magento 2 with Composer on Ubuntu

Install Magento 2.4 Open Source on Ubuntu 22.04 with Composer, the PHP package manager, and turn it into a working store in production mode. You will set up PHP 8.2, MariaDB, Redis, OpenSearch and Nginx, then check that each piece is healthy.

This article walks you through a clean install of Magento 2.4.7 Open Source on Ubuntu 22.04, whether you are setting up a new store or taking over one you inherited. Magento depends on more outside services than most PHP applications, and a missed step often shows up later as search that returns nothing or product saves that crawl. You will install PHP 8.2 with PHP-FPM, the process manager that runs PHP behind Nginx, along with MariaDB for the store data. Redis holds the cache and sessions, and OpenSearch is the search engine Magento uses for its product catalog. You then pull the code with Composer, PHP’s package manager, switch the store to production mode and build the indexes that keep catalog data ready to serve. The first section shows what a healthy install looks like, so you can check yours at any point.

How to verify

A correctly installed Magento 2 store shows:

cd /var/www/magento
bin/magento --version
# Magento CLI 2.4.7

bin/magento setup:db-data:upgrade --dry-run
# Nothing to upgrade

bin/magento deploy:mode:show
# Current application mode: production

bin/magento indexer:status
# Catalog Rule Product                       | Ready
# Catalog Product Rule                       | Ready
# Catalog Search                             | Ready
# Customer Grid                              | Ready
# (all rows must be Ready, never Reindex Required)

curl -sI https://shop.example.org/ | head -2
# HTTP/2 200

redis-cli -n 1 dbsize
# (cache db — should be > 0 after first warm)

If any indexer reads “Reindex Required”, run bin/magento indexer:reindex before claiming the install is complete.

What’s happening

Magento 2 is structured as a Composer project — magento/project-community-edition is the metapackage that pins every internal module. The codebase compiles in two places: dependency injection compilation under generated/, and static view files (themes, JS, CSS) under pub/static/. Both must be regenerated after every release; in “production” application mode, Magento refuses to compile on demand and serves only what was deployed.

Six external services are mandatory. PHP-FPM 8.2 runs the application. MariaDB 10.6 (or MySQL 8.0) is the durable store. Redis or Valkey holds cache, sessions, and the full-page cache — three logical databases on the same instance, separated by --database index. OpenSearch 2.x indexes the catalog (Elasticsearch 7 also works on older 2.4.x; Adobe locked OpenSearch as the supported path on 2.4.6+). RabbitMQ is required for async indexing and queue consumers on stores with any meaningful checkout volume. Nginx (or Apache) sits in front.

The license model matters for downloads. Open Source (formerly Community) you can pull anonymously. Adobe Commerce requires a Magento Marketplace account with the public/private key pair set in ~/.composer/auth.json. Either way, the install is composer create-project, never an unpacked archive — the lock file is the audit trail.

The procedure

  1. Install PHP 8.2 and the extensions Magento requires.

    sudo apt update
    sudo apt install -y nginx mariadb-server redis-server \
      php8.2-fpm php8.2-mysql php8.2-gd php8.2-curl php8.2-mbstring \
      php8.2-xml php8.2-zip php8.2-intl php8.2-bcmath php8.2-soap \
      php8.2-opcache php8.2-redis composer git unzip
  2. Install OpenSearch 2.x. Use the upstream Debian repo:

    curl -fsSL https://artifacts.opensearch.org/publickeys/opensearch.pgp \
      | sudo gpg --dearmor -o /usr/share/keyrings/opensearch-keyring
    echo "deb [signed-by=/usr/share/keyrings/opensearch-keyring] \
      https://artifacts.opensearch.org/releases/bundle/opensearch/2.x/apt stable main" \
      | sudo tee /etc/apt/sources.list.d/opensearch-2.x.list
    sudo apt update && sudo apt install -y opensearch
    sudo systemctl enable --now opensearch

    Disable the security plugin on a single-node dev box or configure proper TLS for production.

  3. Provision the database.

    DBPASS=$(openssl rand -hex 24)
    sudo mariadb <<SQL
    CREATE DATABASE magento CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
    CREATE USER 'magento'@'127.0.0.1' IDENTIFIED BY '${DBPASS}';
    GRANT ALL ON magento.* TO 'magento'@'127.0.0.1';
    FLUSH PRIVILEGES;
    SQL
  4. Create the project tree.

    sudo useradd -m -s /bin/bash deployer
    sudo mkdir -p /var/www/magento && sudo chown deployer:www-data /var/www/magento
    sudo -u deployer -- bash -lc '
      cd /var/www/magento
      composer create-project --repository-url=https://repo.magento.com/ \
        magento/project-community-edition:2.4.7 .
    '
  5. Run setup:install.

    sudo -u deployer bin/magento setup:install \
      --base-url=https://shop.example.org/ \
      --db-host=127.0.0.1 --db-name=magento --db-user=magento --db-password="${DBPASS}" \
      --admin-firstname=Owner --admin-lastname=Owner \
      --admin-email=ops@example.org --admin-user=admin \
      --admin-password="$(openssl rand -hex 12)" \
      --language=en_CA --currency=CAD --timezone=America/Toronto \
      --use-rewrites=1 \
      --search-engine=opensearch --opensearch-host=127.0.0.1 --opensearch-port=9200 \
      --session-save=redis --session-save-redis-host=127.0.0.1 --session-save-redis-db=2 \
      --cache-backend=redis --cache-backend-redis-server=127.0.0.1 --cache-backend-redis-db=0 \
      --page-cache=redis --page-cache-redis-server=127.0.0.1 --page-cache-redis-db=1
  6. Switch to production mode and deploy static content.

    sudo -u deployer bin/magento deploy:mode:set production
    sudo -u deployer bin/magento setup:di:compile
    sudo -u deployer bin/magento setup:static-content:deploy en_CA fr_CA -f
    sudo -u deployer bin/magento indexer:set-mode schedule
    sudo -u deployer bin/magento indexer:reindex
  7. Set permissions. Magento ships a script:

    cd /var/www/magento
    sudo find var generated vendor pub/static pub/media app/etc \
      -type f -exec chmod g+w {} +
    sudo find var generated vendor pub/static pub/media app/etc \
      -type d -exec chmod g+ws {} +
    sudo chown -R deployer:www-data /var/www/magento
    sudo chmod u+x bin/magento
  8. Install the cron entries. Magento has three cron groups; bin/magento cron:install writes the right crontab for the deployer user.

    sudo -u deployer bin/magento cron:install
  9. Nginx config. Magento ships /var/www/magento/nginx.conf.sample — include it from your server block, set set $MAGE_ROOT /var/www/magento; and set $MAGE_MODE production;, and your TLS cert paths.

Common pitfalls

  • Forgetting bin/magento setup:upgrade after a composer update leaves the DB schema behind the code, and the admin throws a banner that disappears on cache clear — re-run setup:upgrade after every composer change.
  • indexer:set-mode schedule is required for high-volume stores; left on the default (“Update on Save”) each product save blocks on a reindex.
  • Static content deploy must include every locale your store serves; missing fr_CA here leaves French shoppers with the en_CA theme assets.
  • OpenSearch with the security plugin enabled requires admin credentials in app/etc/env.php — many installs leave the plugin off on dev and turn it on in prod with no password, which silently rejects every search.
  • var/log/exception.log and var/log/system.log accumulate fast — rotate or you will lose the disk during a busy weekend.

For clients running Magento, Stack Harbor operates the full stack — PHP-FPM tuned for Magento’s memory footprint, OpenSearch and Redis monitored and backed up, indexers under cron alert, static content redeploys on every release — so the merchandising team only sees the admin panel. That model lives under managed operations, with related runbooks in the knowledge base.