How to Install Magento 2 on Windows 11 WSL

This brief tutorial shows students and new users how to install Magento eCommerce platform on Windows 11 using Windows Subsystem for Linux (WSL or WSL2).

If you want to run an eCommerce store in Windows, you can now do it with WSL. You do not need to install any software or run a virtual machine in Windows 11 in order to install Magento. You can now install and run Magento directly from Windows 11 via Windows Subsystem for Linux (WSL). Back in 2017, Microsoft released WSL and later WSL2 that lets developers run a GNU/Linux environment, including most command-line tools, utilities, and applications directly in Windows without setting up a virtual machine or dual-boot.

WSL2 is the new and improved version comes with performance boost, full system call compatibility, and built with a new architecture and that delivers features that make WSL an amazing way to run a Linux environment in Windows.

The new Windows 11 will come with many new features and enhancements that will work great for some while adding some learning challenges for others. Some things and settings have changed so much that folks will have to learn new ways to work with and manage Windows 11.

With Windows 11, installing WSL is a bit different than in Windows 10. However, the steps below is going to show you how to do that.

To install and run Magento on Windows 11 you must first install and enable WSL.

Install Windows Subsystem for Linux in Windows 11

To enable WSL in Windows, you will want to open the Command Prompt as administrator. Click on Start then begin typing Command Prompt.

Next, right-click Command Prompt app and choose to Run as administrator.

When the console opens, run the commands below to install Windows Subsystem for Linux (WSL):

wsl --install

Wait for WSL to be installed.

After installing, you should get a success message similar to the lines below:

Installing: Virtual Machine Platform
Virtual Machine Platform has been installed.
Installing: Windows Subsystem for Linux
Windows Subsystem for Linux has been installed.
Downloading: WSL Kernel
Installing: WSL Kernel
WSL Kernel has been installed.
Downloading: GUI App Support
Installing: GUI App Support
GUI App Support has been installed.
Downloading: Ubuntu
The requested operation is successful. Changes will not be effective until the system is rebooted.

Restart your computer.

WSL should be installed and ready to use. When you want to update, simply run the commands below:

wsl --update

Install Specific Linux distro on Windows 11

Now that WSL is installed, you can now install Linux distro of your choice. To list the available distributions to install, simply run the commands below:

wsl --list --online

You should then see all available distributions that can be installed on WSL.

NAME            FRIENDLY NAME
Ubuntu          Ubuntu
Debian          Debian GNU/Linux
kali-linux      Kali Linux Rolling
openSUSE-42     openSUSE Leap 42
SLES-12         SUSE Linux Enterprise Server v12
Ubuntu-16.04    Ubuntu 16.04 LTS
Ubuntu-18.04    Ubuntu 18.04 LTS
Ubuntu-20.04    Ubuntu 20.04 LTS

To install a Linux distribution from the list above, simply run the commands below using the distribution name. For example, to install Ubuntu 20.04, run the commands below:

wsl --install -d ubuntu-20.04

You should then get a message that the distribution is installed.

Downloading: Ubuntu 20.04 LTS
Installing: Ubuntu 20.04 LTS
Ubuntu 20.04 LTS has been installed.
Launching Ubuntu 20.04 LTS.

After installing, you should get a Ubuntu command console with setup details.

Installing, this may take a few minutes.
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: 
Enter new UNIX username: Richard
New password:
Retype new password:
passwd: password updated successfully
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

Welcome to Ubuntu 20.04 LTS (GNU/Linux 4.4.0-22000-Microsoft x86_64)

Some troubleshooting commands to run when you run into issues.

wsl --set-default-version 1
bcdedit /set hypervisorlaunchtype auto start

Install Nginx HTTP Server

Nginx is probably the second most popular open source web server powering majority of the websites online. Since Magento needs it, use the commands below to install it on Ubuntu.

To install Nginx on Ubuntu, run the commands below:

sudo apt update
sudo apt install nginx

After installing Nginx, the commands below can be used to stop, start and restart Nginx services.

sudo service nginx stop
sudo service nginx start
sudo service nginx restart

To validate that Nginx is installed and functioning, open your web browser and browse to the server’s hostname or IP address.

You should get a test page if every works.

nginx default home page test

Install MariaDB Database Server

For this tutorial, we’re going to be installing MariaDB. Magento needs a database server to store its content.

MariaDB is a true open source database server you can run with your projects. It is fast, secure and the default server for almost all Linux.

To install MariaDB, run the commands below:

sudo apt-get install mariadb-server mariadb-client

After installing MariaDB, the commands below can be used to stop, start and restart MariaDB services.

sudo service mysql stop
sudo service mysql start
sudo service mysql restart

Next, run the commands below to secure the database server with a root password if you were not prompted to do so during the installation.

sudo mysql_secure_installation

When prompted, answer the questions below by following the guide.

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

To verify and validate that MariaDB is installed and working, login to the database console using the commands below:

sudo mysql -u root -p

Type the root password when prompted.

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 46
Server version: 10.3.29-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>

If you see a similar screen as shown above, then the server was successfully installed.

PHP and Related Modules

PHP is a general-purpose scripting language the glues all the above components together. Magento is a PHP application and requires PHP and related modules to function.

To install PHP and recommended modules, run the commands below.

sudo apt install php-fpm php-common php-mysql php-gmp php-curl php-intl php-mbstring php-xmlrpc php-gd php-xml php-cli php-zip php-soap php-bcmath

That should get PHP installed with recommended PHP modules that you can run with many PHP based applications.

To validate that PHP is installed, run the commands below:

php -v

You should see an output like the one below:

PHP 7.4.3 (cli) (built: Oct  6 2020 15:47:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

You can also test with a test php script and displays installed version as well as related modules that are enabled or disabled.

To do that, run the commands below to create a php test file called phpinfo.php

sudo nano /var/www/html/phpinfo.php

Then type the content below and save the file.

<?php phpinfo( ); ?>

Save the file.

Open your browser and browse to your server hostname followed by phpinfo.php

Restart Nginx, then type the address and browse to the file.


You should see PHP default test page.

At this stage, all required components that Magento needs are installed above. Continue below to downloading and configuring Magento settings.

Create Magento Database

When all the servers installed above, it’s now time to begin setting up Magento environment. First, run the steps below to create a blank database for Magento to use.

Logon to MariaDB database console using the commands below:

sudo mysql -u root -p

Then create a database called magentodb

CREATE DATABASE magentodb;

Next, create a database user called magentodbuser and set password

CREATE USER 'magentodbuser'@'localhost' IDENTIFIED BY 'new_password_here';

Then grant the user full access to the database.

GRANT ALL ON magentodb.* TO 'magentodbuser'@'localhost' WITH GRANT OPTION;

Finally, save your changes and exit.

FLUSH PRIVILEGES;
EXIT;

Download Magento

At this point, Magento is ready to be downloaded and installed. Use the commands below to download the latest version of Magento.

To get Magento latest release you may want to use GitHub repository… Install Composer, Curl and other dependencies to get started…

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

When prompted, enter your authentication keys. Your public key is your username; your private key is your password….  ( 

Magento Ubuntu Key

You’ll have to register for an account to create the key above.

After installing curl and Composer above, change into the Nginx root directory and download Magento packages from GitHub… Always replace the branch number with the latest branch.

To view Magento releases, see this page.

cd /var/www/
sudo composer create-project --repository= magento/project-community-edition magento

Copy and paste the authentication key… (Your public key is your username; your private key is your password)

Output:
Authentication required (repo.magento.com):
Username: 234f2343435d190983j0ew8u3220
Password: 
Do you want to store credentials for repo.magento.com in /opt/magento/.config/composer/auth.json ? [Yn] Y

After downloading Magento packages, run the commands below to install Magento with the following options:

cd /var/www/magento
sudo bin/magento setup:install --base-url= --db-host=localhost --db-name=magentodb --db-user=magentodbuser --db-password=db_user_password_here --admin-firstname=Super --admin-lastname=Admin --admin-email=[email protected] --admin-user=admin --admin-password=admin123 --language=en_US --currency=USD --timezone=America/Chicago --use-rewrites=1
  • The Magento software is installed in the root directory on localhost…. Admin is admin;  therefore: Your storefront URL is http://exmaple.com
  • The database server is on the same localhost as the webserver….
  • The database name is magentodb, and the magentodbuser and password is db_user_password_here
  • Uses server rewrites
  • The Magento administrator has the following properties:
    • First and last name are: Admin User
    • Username is: admin
  •  and the password is admin123
  • E-mail address is: [email protected]
  • Default language is: (U.S. English)
  • Default currency is: U.S. dollars
  • Default time zone is: U.S. Central (America/Chicago)

Elasticsearch is now enabled with Magento packages. If you’re not using it and you run into trouble after running the commands above, use this line to disable Elasticsearch module.

sudo php bin/magento module:disable {Magento_Elasticsearch,Magento_InventoryElasticsearch,Magento_Elasticsearch6,Magento_Elasticsearch7}

If you want to run Magento with Elasticsearch instead, read this post.

After that, run the commands below to set the correct permissions for Magento 2 to function.

Then run command below to allow www-data user to own the Magento directory.

sudo chown -R www-data:www-data /var/www/magento/
sudo chmod -R 755 /var/www/magento/

Configure Nginx VirtualHost

Below is where you configure Nginx VirtualHost file for the Magento site you’re creating. This file defines how client requests are handled and processed.

Run the commands below to create a new VirtualHost file called Magento in the /etc/nginx/sites-available/ directory.

sudo nano /etc/nginx/sites-available/magento

A very good configuration settings for most Magento site on Nginx server is below. This configuration should work great.

Copy the content below and save into the file created above.

upstream fastcgi_backend {
  server unix:/run/php/php7.4-fpm.sock;
}

server {
    listen 80;
    listen [::]:80;

    server_name  example.com www.example.com;
    index  index.php;

    set $MAGE_ROOT /var/www/magento;
    set $MAGE_MODE production;

    access_log /var/log/nginx/example.com-access.log;
    error_log /var/log/nginx/example.com-error.log;

    include /var/www/magento/nginx.conf.sample;
}

Save the file and exit.

After saving the file above, run the commands below to enable the new site, then restart Nginx server.

sudo ln -s /etc/nginx/sites-available/magento /etc/nginx/sites-enabled/
sudo service nginx restart

At this stage, Magento is ready and can be launched by going to the server’s IP or hostname.

That should bring up Magento home page.

Login with account above and you’re done.

That’s it!

Conclusion:

This post showed you how to install Magento on Windows 11 with Ubuntu. If you find any error above, please use the comment form below to report.