Latest Guide on How to Install Magento 2 on Ubuntu

Are you looking for a step-by-step guide to Install Magento 2 latest version on Ubuntu/Debian from the Magento repo or GitHub with Apache/NginX, MySQL/MariaDB, and PHP7.x? This is the best place for you. In this post, I will show you how to install Magento 2.3 on an Ubuntu latest with Apache2, MariaDB. In previous posts, I talked about Install Magento 2 on MAC OS, Centos or Windows.

Before jumping in, please check on Magento 2 Requirements:

You should double-check 8 main requirements for Magento 2 here.

Learn more about Magento 2 Module Development

Step 1: Install Apache2 PHP and Required Extensions

Step 1.1 Install Apache2 Server

The Apache HTTP Server, called Apache, is free and open-source cross-platform web server software. Apache is the most popular HTTP Server now. It runs on Linux, Windows, OpenVMS, NetWare and other operating systems.

To install Apache, you should update packages before running the install Apache install command:

sudo apt update
sudo apt install apache2

To run Apache automatically during startup, run the following command line:

sudo systemctl enable apache2.service

Magento 2
Create an Online Store

Let us help you build a converting website with experienced e-commerce experts at an affordable price

Get Started
build online store

Step 1.2 Configure Apache2 Virtual Host

To declare the Apache2 site configuration for Magento 2 store, you have to create a new configuration file magento2.conf:

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

Copy and paste the following content to the above file. Remember, you should change to your domain.

<VirtualHost *:80>
	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/html
	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined
<VirtualHost *:80>
    DocumentRoot /var/www/html/magento2/pub

If you are installing Magento locally, you can change to, or Then you have to update the hosts file at /etc/hosts with

IMPORTANT: In this tutorial, I use Enable Rewrite mod Make sure you rewrite the mod to use site-friendly URLs:

sudo a2ensite magento2.conf
sudo a2enmod rewrite

Step 1.3: Install PHP 8.1 and extensions

On Ubuntu, you can install PHP 8.1 easily with the following command line:

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

Step 1.4: Update the php.ini file

Now it’s time to increase values in the php.ini file. Open php.ini file:

sudo nano /etc/php/8.1/apache2/php.ini

Change the following data:

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

Then save this php.ini file. After that, you should restart apache2. Run this command:

sudo systemctl restart apache2.service

Step 2: Install Database Server

Magento preferred MariaDB Database Server to the default MySQL Database Server because of its faster and better performance. To install MariaDB Server and Client, run this command line:

sudo apt-get install mariadb-server mariadb-client

Make sure it starts and startup every time you reboot the server:

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

You’ve just installed MariaDB server, now you have to set up this database server initially.

sudo mysql_secure_installation

It will prompt, and you need to choose the following option:

Enter current password for root (enter for none): 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 (Required)

From Magento 2.3.x, Magento requires a unique user for Magento installation, it cannot default user: root.

First of all, you have to log in to MariaDB:

sudo mysql -u root -p

Create a new database for Magento 2:


Then create a new user name called: mageplaza


Grant mageplaza user to magento2 database:


Ok, time to flush privileges and exit.


Step 5: Install Composer

Download Composer and install, or you can use the command line to install Composer

curl -sS | sudo php -- --install-dir=/usr/local/bin --filename=composer

Check whether Composer is installed or not just type:

composer -v


  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
Composer version 1.8.5 2019-04-09

Step 6: Download Magento 2 Pack

You can download it from one of the following resources:

  • Run Command in folder /var/www/html/ : composer create-project –repository-url= magento/project-community-edition=2.4.6 magento2

After downloading, you should extract the pack to /var/www/html/. E.g., you have a folder called: magento2 in /var/www/html/ Set permissions: Run this command

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

Step 7: Install Magento 2

Run this command in the Magento folder:

php bin/magento setup:install --base-url= --db-host= --db-name=magento2 --db-user=mageplaza --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

This command is used to install and set up a Magento 2.4.6 instance using the command-line interface. Let’s break down the provided command and its parameters:

  • php bin/magento setup:install: This is the command to initiate the installation process for Magento.
  • --base-url= Specifies the base URL for your Magento store.
  • --db-host= Specifies the database host (in this case, the IP address of the database server).
  • --db-name=magento246: Specifies the name of the database where Magento will store its data.
  • --db-user=magentouser: Specifies the database user for accessing the database.
  • --db-password=password: Specifies the password for the database user.
  • --admin-firstname=admin: Sets the first name of the admin user.
  • --admin-lastname=admin: Sets the last name of the admin user.
  • [email protected]: Sets the email address of the admin user.
  • --admin-user=admin: Specifies the username for the admin user.
  • --admin-password=admin123: Specifies the password for the admin user.
  • --language=en_US: Sets the default language for the Magento store to American English.
  • --currency=USD: Sets the default currency for the store to US Dollars.
  • --timezone=America/Chicago: Sets the default timezone for the store to America/Chicago.
  • --use-rewrites=1: Enables URL rewrites.
  • --backend-frontname="admin": Specifies the URL slug for accessing the admin backend. In this case, it will be “”.
  • --search-engine=elasticsearch7: Specifies the search engine to be used. In this case, Elasticsearch 7 will be used for search functionality.

You can install a Magento 2.4.6 instance with the specified settings and configurations by running this command with the provided parameters. Make sure to adjust the values as needed to match your environment and preferences. You must check your environment following:

Check the result

After the command is finished, let’s check your site with

  • Front end:
  • Admin: , with user name is: admin and password is: admin123

Benefits of using Ubuntu/Debian to install Magento 2

Ubuntu or Devian has some advantages when it comes to installing Magento 2. Here are they:

  • Magento itself strongly recommends developers install the platform on Linux-based systems for full capacity and compatibility
  • Using Ubuntu or Debian will cost less than macOS or Windows because it’s open-sourced and supported by much free software
  • Linux-based system “naturally” has the ability to prevent virus attacks properly
  • Linux community is quite active in helping with your specific problem in your situation

Magento 2 extensions

Magento 2 extensions

Allow you to achieve more with your online store

Check it out!


Now, you can start configuring your store using the Ubuntu /Debian system. Magento is a versatile yet powerful platform that enables users to build up and customize special features. Therefore, there are a lot of practices that we all need to learn and update. If you have any questions, don’t hesitate to let us know in the comment section down below!

Image Description
Hello, I'm the Chief Technology Officer of Mageplaza, and I am thrilled to share my story with you. My deep love and passion for technology have fueled my journey as a professional coder and an ultra-marathon runner. Over the past decade, I have accumulated extensive experience and honed my expertise in PHP development.