How to install WordPress locally

There are many ways to set up a local copy of WordPress. I used XAMPP on Windows for a number of years, and while it worked well for a time, it seems to struggle with large databases. WAMP has served me better in recent years when I’m using a PC, but these days I use a Mac for most of my development work.

At Happy Prime, we use Laravel Valet to make setting up a local site almost painless—once you get the initial setup complete.

On a Mac, your local sites will always be available. There’s no Docker container to spin up, no WAMP server to start—just an always-ready lightweight local setup.

Another bonus: Valet is also available for Windows and Linux, so you can set it up on almost any computer.

(However, with Windows, there are huge caveats. I’ve been trying to get everything working the same on my PC as it does on my Mac and so far, I haven’t been able to brute-force it. You may have more luck with WSL.)

How to install WordPress locally on a Mac M1

I have a M1 Mac, so my setup may vary slightly from other Mac setups—mostly in where files are stored.

I installed tools in this order:

The hard part is over!

My last local setup step is to create a local folder where all my local sites will live. Following Valet’s recommendations, I ran

cd ~/Sites
valet park

The park command tells Valet to make all of the subdirectories available in the browser.

Now, to create a local site, you open the ~/Sites directory in a command prompt and type:

wp valet new newsitename --admin_password=password

Change newsitename to whatever you want your “domain” to be. This example would create a site at https://newsitename.test. You’ll be asked for your machine’s administrator password, possibly multiple times, because in the background Valet is setting up a local SSL certificate. That’s right, your local sites will automatically be secure!

Getting a local copy of a site

If you’re working on an existing site, you’ll first use that wp valet new command. Next, you’ll want a copy of the site’s files, generally just /wp-content/themes/ and /wp-content/plugins/. Skip the uploads folder—we’ll use a special driver in a minute that will tell Valet to look for any missing images on the live site.

Next, you’ll need the database. Export it with whatever tools you have—many hosts offer phpMyAdmin; you might also be able to use WP CLI if your host has it enabled. If the file you download is zipped, unzip it, so you’re just dealing with a .sql file, and place it into your site’s root folder. You can now run:

cd ~/Sites/newsitename
wp db import nameoffile.sql

You almost have a working local copy, but right now the database is pointing to live URLs – WordPress doesn’t know it’s been cloned locally. Since WordPress stores URLs in multiple places, and character count matters, it’s safest to run a script to update them all.

If you don’t want to install any more tools, you can use wp-search-replace, which comes with WP CLI. I tend to feel safer manipulating databases with a GUI, so I use Interconnect’s Search and Replace script.

If you go the GUI route too, copy the script into the local site you’re working on, open up the settings in your browser, punch in the old URL and new URL, and it fixes everything for you. (Then delete the script, or move it out to a utilities folder, so you won’t accidentally run it again – or worse, accidentally deploy it to your live site.)

Proxy images from a live site

Laravel has a driver system that works similarly to how plugins use action hooks in WordPress. You can use this custom local driver to proxy images from a remote URL when they’re requested locally, if a local copy of the file doesn’t exist. Copy the code from the gist and place it in a LocalValetDriver.php file in the site root. Change the REMOTE_HOST constant in that file to point to the live site domain.

Now when you load a page locally, Valet will pull locally-missing images from the live site, like magic.

Database tool: phpMyAdmin

While I love new tools like Valet, I also find comfort in old familiar tools like phpMyAdmin. Luckily, it’s not too hard to get it working on a Mac. It’s much quicker than getting Valet ready!

  • Install phpMyAdmin.
  • Find out where it’s installed:
    • If your Mac has an Intel chip, it should be in /usr/local/share/phpmyadmin/.
    • If your Mac has an M1, it should be in /opt/homebrew/share/phpmyadmin/.
  • In a command prompt, cd to that directory.
  • Type valet link – this will point https://phpmyadmin.test to your Homebrew-installed phpMyAdmin.
  • Type sudo nano and change the $cfg['Servers'][$i]['AllowNoPassword'] setting to true.
  • When you’re done, press Control + X, then Y to save the changes, and Enter to keep the file in the same place.
  • Visit https://phpmyadmin.test in a browser, enter root for the username with no password, and see all your local databases.
  • Even though we’re working locally here, using root with no password is a bad habit to get into, so next:
  • Go to the User Accounts tab.
  • Add a user account, naming it something you’ll remember but something safer than root. Make sure to set the host name to localhost and set up a password. Grant it all privileges.
  • Log out of phpMyAdmin and log in again as your new user, to make sure you typed everything correctly.
  • Final step: re-edit and change that AllowNoPassword setting to false. Use your new account from now on instead of root.

Other local setup options

One of those things that’s both a blessing and a curse is the fact that local setups can vary widely. This can be very convenient—say you work for a variety of clients with a variety of setups, but since local setups are flexible, you can set all of them up your preferred way and always know how to interact with them. But it can also be frustrating—say some of those clients use Docker and others use Lando, whose versions aren’t always compatible. For me, skipping the containers and installing everything straight onto my local machine saves a lot of time and headaches.

Leave a Reply