How to install Magento 2 on Ubuntu 18.04

Installing Magento 2 on Ubuntu 18.04 can be done easily by following these steps:

Prerequisites:

  • Ubuntu 18.04 LTS (or a compatible distribution)
  • Apache or Nginx web server
  • PHP 7.1.3 or later with required PHP extensions
  • MySQL 5.6 or later or MariaDB 10.0.2 or later
  • Composer

Step 1: Update the Ubuntu packages

First, update the Ubuntu packages to the latest version with the following command:

sudo apt update
sudo apt upgrade

Step 2: Install Apache or Nginx

Install the web server you prefer. In this guide, we will install Apache. Use the following command:

sudo apt install apache2

Step 3: Install PHP

Install the required version of PHP and the necessary PHP extensions using the following command:

sudo apt install php libapache2-mod-php php-mysql php-common php-cli php-gd php-curl php-intl php-xsl php-mbstring php-zip php-bcmath php-iconv php-soap

Step 4: Install MySQL or MariaDB

Install MySQL or MariaDB server using the following command:

sudo apt install mysql-server

Step 5: Install Composer

Install Composer using the following command:

sudo apt install composer

Step 6: Create a Magento 2 database and user

Create a new database and user for Magento 2 to use. Use the following command:

sudo mysql -u root -p
CREATE DATABASE magento_db;
CREATE USER 'magento_user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON magento_db.* TO 'magento_user'@'localhost';
FLUSH PRIVILEGES;
exit;

Step 7: Download Magento 2

Download Magento 2 from the official website or using the following command:

cd /var/www/html
sudo composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition .

Step 8: Configure Magento 2

Configure Magento 2 with the following command:

sudo bin/magento setup:install --base-url=http://localhost/ \
--db-host=localhost --db-name=magento_db --db-user=magento_user \
--db-password=password --admin-firstname=admin --admin-lastname=admin \
--admin-email=admin@admin.com --admin-user=admin --admin-password=admin123 \
--language=en_US --currency=USD --timezone=America/Chicago \
--use-rewrites=1

Step 9: Enable Apache Rewrite Module

Enable Apache’s rewrite module using the following command:

sudo a2enmod rewrite

Step 10: Restart Apache

Restart Apache using the following command:

sudo service apache2 restart

Now you have successfully installed Magento 2 on Ubuntu 18.04. You can access your Magento 2 store by going to http://localhost/ in your web browser.