How to install Magento 2 on Ubuntu

//How to install Magento 2 on Ubuntu

How to install Magento 2 on Ubuntu

OK so today we’re doing a fresh install of Magento 2 (2.2 to be precise).  Follow along to see what’s involved.

(Note: We’re doing this on a machine that’s already running Magento 1.9, on Ubuntu 16.04. Don’t worry if you don’t have Magento 1.x – it’s not needed for this exercise – it will just affect some minor details in the steps below).

The first thing we need to do is make sure the machine meets all of the Magento 2.2 resource requirements.  In a nutshell: At least 2GB of RAM; Apache 2.2 or 2.4; MySQL 5.6 or 5.7; PHP 7.0.2, 7.0.4, 7.0.6+, 7.1+; Specific PHP settings and extensions (see the link above for a complete list).

Checking that we have the necessary resources

So – how much RAM do we have? Here’s the Ubuntu command:

sudo lshw -class memory

It tells us we have 2GB – just enough.

What Apache version do we have?

apache2 -v

It tells us 2.4.18 – perfect! (2.2.x or 2.4.x are fine).

So far, so good – now for MySQL:

mysql -uUserName -pPassword -Bse "SELECT VERSION();"

Our MySQL is 5.7.16 – this is fine. 5.6 or 5.7 will do.

And the final version check is for PHP.

php -v

We’ve got 7.0.22 – this is fine too. Any of 7.0.2, 7.0.4, 7.0.6+, 7.1+ are OK.

So we seem to have the resources we need. We haven’t checked the list of PHP extensions needed – it’s long, and we likely already have all/most of them.  In any case these can be installed during the process if needed.  Let’s go ahead and install.

Installing Magento 2

First we need to do some preparatory work.


We have root access to this machine.  This is a requirement – if you’re doing an install yourself you will need root access too.

O/S User(s) for Magento 2

We need to set up a couple of Linux users.  This area has changed since Magento V1. Also, there are now two different approaches from which we need to choose depending on whether your host machine is shared or you have a dedicated host. For this exercise we’ll use the steps for the dedicated host.

First we create the web admin user (“magweb” – to be used for Magento administration through the browser).

sudo adduser magweb

Next we create the command line user (“magos” – to be used to log in to the O/S, and to be the owner of all the Magento files):

sudo adduser magos

This user needs to be a member of the apache user’s primary group. This group is usually “www-data” on Ubuntu, (and “apache” on CentOS,) but to check we do:

ps -ef | egrep '(httpd|apache2|apache)' | grep -v `whoami` | grep -v root | head -n1 | awk '{print $1}'

Sure enough – it tells us www-data. So next we do:

sudo usermod -a -G www-data magos

…aaand now it’s time for an apache restart:

sudo service apache2 restart

The Magento 2 root directory

Now we’ll make the directory that’s going to hold all of the Magento files.   On this machine Magento V1 is installed at /var/www/magento so we’re going to put V2 at /var/www/magento2.  After creating the directory we’ll give our new user “magos” ownership of it and make “www-data” the group.

cd /var/www
sudo mkdir magento2
sudo chown magos magento2
sudo chgrp www-data magento2


Composer Installation

Magento 2 uses the component manager “Composer”, so we need to install that first:

sudo apt install composer

Composer needs authentication keys, which we can create via the Magento Marketplace, like so:

You need to register first if you don’t already have an account.  Once signed in, go to “My Profile”:

Click “Access Keys”:

Click “Create A New Access Key”:

Enter your name or company name (no spaces) and click OK:

This gives us a Public Key and a Private Key.  The public key is our Composer user name, and the private key is our password.

Time to log in as “magos”:

su magos

We need to switch to the root Magento 2 folder:

cd /var/www/magento2

Then create the composer project:

composer create-project --repository-url= magento/project-community-edition .

We’re prompted for a user name – this is where we use the Public Key from the Magento Marketplace above.  Then the password – this is the Private Key from above.  We’ll just accept the defaults on further prompts.

First time through it tells us:

Your requirements could not be resolved to an installable set of packages.

Problem 1
 - Installation request for magento/product-community-edition 2.2.4 -> satisfiable by magento/product-community-edition[2.2.4].
 - magento/product-community-edition 2.2.4 requires ext-bcmath * -> the requested PHP extension bcmath is missing from your system.

We didn’t do an extensive check of the PHP extensions we’d be needing when we started, preferring instead to let it tell us.  And it’s telling us now – we need bcmath. We need to drop back to our root user to install this.

exit #drop back to root user
sudo apt install php7.0-bcmath

OK that should do it.  Let’s try again to create the composer project

su magos #log back in as magento file owner
cd /var/www/magento2
rm -rf * #clean up the failed create
composer create-project --repository-url= magento/project-community-edition .

That worked fine this time.

Now we need to set some file permissions…

#NOTE: we're logged in as magos at this point
cd /var/www/magento2
find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} \;
find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} \;
cd /var/www/magento2
sudo chown -R :www-data .
su magos
chmod u+x bin/magento

Running the Magento 2 installer

Our prep is nearly finished, but before running the installer we need to sign in to mysql and create the magento2 database (the installer doesn’t do this for us):

mysql -u root -p

And we’re finally ready to run the install! If you’re doing an upgrade you’ll need to modify the –base-url, –admin-email, –admin-password and –db-password parameters below (at a minimum). For a full list of the parameters and their meanings see the Magento docs.

cd /var/www/magento2/bin
./magento setup:install --base-url= --admin-user=admin --admin-firstname=Magento --admin-lastname=User --admin-password=newpassword --db-user=root --db-password=mysqlrootpassword

The above creates all of the Magento files. Now we need make sure all file and directory permissions are correct:

exit # back to root user
cd /var/www/magento2
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 find var generated vendor pub/static pub/media app/etc -exec chgrp www-data {} \;

To access this fresh install of Magento 2 we need to point the Apache document root at it:

cd /etc/apache2/sites-available
sudo vi 000-default.conf # or your favourite editor

In our case here we just need to change this:

        DocumentRoot /var/www/magento
        <Directory /var/www/magento>

to this:

        DocumentRoot /var/www/magento2
        <Directory /var/www/magento2>

then we need to restart apache once more:

sudo service apache2 restart

Verifying the Install

OK – done! Time to check that it all worked ok. Let’s visit the site in our browser:

That’s the default empty Magento 2 site – we’re done with our Magento 2 install.

If you’d like to see how to transfer data from a Version 1 Magento database to your new installation, check out this page: How to upgrade from Magento 1 to Magento 2.

If you’re having problems with any of the above, leave a comment below, or contact us for any of your Magento needs.

By | 2018-06-25T06:04:12+00:00 June 22nd, 2018|Blog|1 Comment

About the Author:

One Comment

  1. […] First we need to install Magento 2. […]

Leave A Comment