🔥 Just Launched! Werra Premium Template for HyväSee it in Action

How to Install Magento 2 on Ubuntu: Step-by-step [2025]

By Sanjay JethvaUpdated on Mar 17, 2025 4 min read

Hey, Magento Geeks!

You’ve already made a great choice to install Magento 2 on Ubuntu.

Ubuntu is a preferred OS for installing Magento 2 for many developers and merchants. It is a feature-rich, reliable, and secure Linux-based system that Adobe recommends for full capacity and compatibility.

In this post, find the complete steps to install Magento 2 on Ubuntu.

How to Install Magento 2 on Ubuntu?

Before starting the Ubuntu Magento installation process–check the Magento 2 system requirements. Double-check the required hardware setup for the Magento version you’re installing.

Follow these steps to install Magento 2 on Ubuntu:

Step 1: Install & Configure Apache2 Server & PHP Extensions

Install & Configure Apache2 Server

Apache is an open-source web server software that you’ll need to install Magento 2 on Ubuntu.

Run the following commands to install Apache in Ubuntu:

sudo apt update
sudo apt install apache2

To auto-run Apache2 during system startup, use the following command:

sudo systemctl enable apache2.service

Create a magento2.conf file using the following command to declare Magento 2 site configuration for Apache2:

sudo nano /etc/apache2/sites-available/magento2.conf

Now, open the created file. Copy and paste the following configuration into it:

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined


    ServerName your-magento-2-store-url.com
    DocumentRoot /var/www/html/magento2/pub

Remember to replace the server address from “your-magento-2-store-url.com” to your actual Magento 2 store URL.

If installing Magento 2 on Ubuntu locally, you can change it to localhost.com. You’ll also need to update the hosts file at /etc/hosts with

127.0.0.1 your-magento-2-store-url.com

Run the following command to enable mod rewrite:

sudo a2ensite magento2.conf
sudo a2enmod rewrite

Install PHP 8.2 & Required Extensions

You’ll also need to install PHP 8.2 and its extensions required for installing Magento 2 in Ubuntu. You can run the following single command to do that:

sudo apt install php8.2 libapache2-mod-php8.2 php8.2-common php8.2-gmp php8.2-curl php8.2-soap php8.2-bcmath php8.2-intl php8.2-mbstring php8.2-xmlrpc php8.2-mysql php8.2-gd php8.2-xml php8.2-cli php8.2-zip

Now, open thephp.inifile and configure it as following:

file_uploads = On
allow_url_fopen = On
short_open_tag = On
memory_limit = 512M
upload_max_filesize = 128M
max_execution_time = 3600

Save the file and restart the Apache2 server using the following command:

sudo systemctl restart apache2.service

Step 2: Install Database Server

MariaDB is a preferred database server for Magento 2 and Adobe Commerce. To install MariaDB server and client in Ubuntu, run the following command:

sudo apt-get install mariadb-server mariadb-client

To auto-start the database server on system startup, run the following command:

sudo systemctl restart mariadb.service
sudo systemctl enable mariadb.service

Now, run the following command to set up a MariaDB server in Ubuntu:

sudo mysql_secure_installation

Select the following options when prompted:

  • Enter current password for root (enter for none): Press Enter
  • Set root password? [Y/n]: Y
  • New password: Type your password
  • Re-enter new password: Type your password
  • Remove anonymous users? [Y/n]: Y
  • Disallow root login remotely? [Y/n]: Y
  • Remove the test database and access it. [Y/n]: Y
  • Reload privilege tables now? [Y/n]: Y

Step 3: Create MySQL User

Magento 2 can not be installed to the default root user. You’ll need to create a new database user.

Run the following command to log into MariDB:

sudo mysql -u root -p

Now, run the following command to create a new database:

CREATE DATABASE magento2

Create a new user called ‘Meetanshi’ using the following command:

CREATE USER 'meetanshi'@'localhost' IDENTIFIED BY 'YOUR_PASSWORD';

Grant meetanshi user to magento2 database:

GRANT ALL ON magento2.* TO 'meetanshi'@'localhost' IDENTIFIED BY 'YOUR_PASSWORD' WITH GRANT OPTION;

Finally, flush the privileges and exit.

FLUSH PRIVILEGES;
EXIT;

Step 4: Install Composer

Magento 2 requires Composer for dependency management. You can either download and install Composer in Ubuntu manually or run the following command to do that:

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

To check if the Composer has been successfully installed, run the following version check command:

composer -v

Step 5: Download & Install Magento 2 in Ubuntu

Download Latest Magento 2 Package

Go to one of these resources and download the zip file of the latest Magento 2 version:

Now, extract the zip file to the /var/www/html/ folder.

Run the following command to set the permissions:

sudo chown -R www-data:www-data /var/www/html/magento2/
sudo chmod -R 755 /var/www/html/magento2/

Install Magento 2 on Ubuntu

Open the /var/www/html/magento2 folder and run the following command:

php bin/magento setup:install –base-url=https://magento2.com/ –db-host=127.0.0.1 –db-name=magento2 –db-user=meetanshi –db-password=password –admin-firstname=admin –admin-lastname=admin –[email protected] –admin-user=admin –admin-password=admin123 –languag e=en_US –currency=USD –timezone=America/Chicago –use-rewrites=1 –backend-frontname=”admin” –search-engine=elasticsearch7

The above command installs the Magento 2 on Ubuntu using with preset configuration. You can adjust the values of these parameters to install Magento 2 as per your needs. For e.g., you can modify the login username and password.

Once the command has executed successfully, you can access the Magento 2 in the local server.

  • Frontend URL: https://magento2.com/
  • Admin Login URL: https://magento2.com/admin
  • Admin Username: admin
  • Admin Password: admin123

That’s it! Congratulate yourself on successfully setting up Magento 2 store on Ubuntu.

If you’ve any doubts regarding how to install Magento 2 on Ubuntu or if you face any problems, feel free to reach out through comments. I will be happy to help you.

Sanjay Jethva Full Image
Article bySanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.