# Existing Magento 2 setup

You might want to install ScandiPWA as a Magento theme (without using [Create Magento App](http://docs.create-magento-app.com/)) – and this is supported out of the box! You can install the theme as a local composer module, continuously build it, and Magento will be able to recognize it as a valid Magento theme. Then, you will be able to select your ScandiPWA-based theme in the Magento configuration.

{% hint style="info" %}
If you are using[ Create Magento App ](https://scandipwa.gitbook.io/create-magento-app/)you're lucky! It has a built-in ScandiPWA linking function built-in. Read how to [setup ScandPWA with CMA](https://docs.scandipwa.com/getting-started-1/magento-theme-mode) in minutes!
{% endhint %}

## Prerequisites

Make sure that you have a [supported Magento version](https://manual.scandipwa.com/pwa/magento-version-mapping)! Also, make sure your server is configured to point into `<MAGENTO ROOT>/pub` directory.

#### Node v14+

```bash
node -v # should be 14^
```

You can instal Node using [`nvm`](https://github.com/nvm-sh/nvm) (recommended) or the [official guide](https://nodejs.org/en/download/package-manager/).

#### Varnish v5+

```bash
varnishd -V # should be 5^
```

In Magento admin go to *Stores > Configuration > Advanced > System > Full Page Cache*. Make sure the `Varnish Cache` is selected in the dropdown, varnish configuration has proper values set in it.

If it is not, please follow [official documentation](https://devdocs.magento.com/guides/v2.3/config-guide/varnish/config-varnish.html) to set it up.

#### Redis v2.5+

```bash
redis-cli -v # should output 2.5^
```

If it is not installed, please follow [this guide](https://codewithhugo.com/install-just-redis-cli-on-ubuntu-debian-jessie/) to obtain it.

## Install the ScandiPWA theme with Composer

We recommend you keep your theme source in a `src/localmodules` directory. You will then be able to configure composer to install the theme from here as a local module.

```bash
mkdir src/localmodules
cd src/localmodules
```

{% tabs %}
{% tab title="If you're just getting started" %}
Verify that your system has the [required tools](https://docs.scandipwa.com/getting-started-1/..#prerequisites) installed, and [create a new theme](https://docs.scandipwa.com/getting-started-1/..#installation)!
{% endtab %}

{% tab title="If you already have a theme" %}
Simply move your ScandiPWA theme into the `localmodules` directory:

```bash
mv ~/Projects/<your-theme> . # path to your theme
cd <your-theme>
```

{% endtab %}
{% endtabs %}

Build or start the application in Magento mode:

{% tabs %}
{% tab title="yarn (recommended)" %}

```bash
BUILD_MODE=magento yarn start # or build
```

{% endtab %}

{% tab title="npm" %}

```bash
BUILD_MODE=magento npm start # or build
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
I**f you chose `start` – you will need to keep this process running**. It continuously re-builds the theme when changes are made. Open a new terminal tab to enter new commands.
{% endhint %}

Now the new theme is created, but we need to install it using Composer. We will install the newly-created theme by taking advantage of Composer's ability to install from [local repository sources](https://getcomposer.org/doc/05-repositories.md#path).

First, we add our theme as a local repository source. This will alter `composer.json` to add a new item in the `repositories` field:

```bash
composer config repo.theme path localmodules/<your-app-name>
```

Next, we install our theme by using `require`. This will resolve the package to the localmodules directory we configured above:

```bash
composer require scandipwa/<your-app-name>
```

## Configure persisted-query

For improved ScandiPWA query caching to work, you must configure `scandipwa/persisted-query`. For convenience, there are additional flags available for `php bin/magento setup:config:set` command:

| Flag            | Required? | Description                                                              | Example     |
| --------------- | --------- | ------------------------------------------------------------------------ | ----------- |
| `--pq-host`     | Yes       | Persisted query Redis host                                               | `127.0.0.1` |
| `--pq-port`     | Yes       | Persisted query Redis port                                               | `6379`      |
| `--pq-database` | Yes       | Persisted query Redis database                                           | `5`         |
| `--pq-scheme`   | Yes       | Persisted query Redis database                                           | `tcp`       |
| `--pq-password` | No        | <p>Persisted query Redis password<br>(empty password is not allowed)</p> | *empty*     |

## Enable the ScandiPWA Theme

Run the `upgrade` command and disable full-page caching:

```bash
bin/magento setup:upgrade
bin/magento cache:disable full_page
```

It is now time to enable the new theme. In the Magento admin panel, navigate to **Content** > **Design** > **Configuration**. Edit the scope you want to change (typically the most general one in the list), and select the new theme. Finally, flush the cache:

```bash
bin/magento cache:flush
```

{% hint style="success" %}
The new theme should now be served on the frontend. Congratulations, you now have a ScandiPWA Magento Theme!
{% endhint %}
