Creating a local environment can be frustrating when you’re new to Magento 2. I get asked a lot by developer friends how to do this so I thought this post might be useful.
Since most people reading this post already know how to do most of these tasks, I’m going to list the steps without going into detail.
- Checkout your site’s repository on a local folder (if your site isn’t setup to use git, do that first).
- If you used composer to install your site originally or have ignored your vendor folder in your repository, you’ll need to run
composer install && composer update.
- Backup your site’s database to your local machine.
- Point Apache or Nginx to the local folder you checked out your repository.
- Create a new database and import the database you backed up to your local machine.
- In the database’s core_config_table change the base URLs (secure and unsecure) to your local domain and clear the cookie domain. If you’re using a CDN you will need to change the related entries for JS/CSS/Media files.
- Navigate to http://local.domain/setup.
- Complete the Magento 2 setup wizard. Don’t worry, all your files and database info will stay the same. This is just a simpler way of creating an env.php file for your local environment.
- For good measure, run
php bin/magento setup:upgradejust in case composer updated any extensions in the second step.
- If your repository isn’t ignoring caching folders you’ll need to clear those first to make sure nothing is pointed to the wrong domain.
That’s it! You should now have a local environment that’s ready to develop on.