Installing WordPress on Localhost: OSX | WordPress for Beginners

In this post, we will discuss how you can set up WordPress locally on your OSX machines. There are a number of ways to install WordPress locally on your OSX. We will discuss the easiest and fasted way here.

We will use Laravel Valet and adminer.

Updating Homebrew on your OSX

Before going any further, you need to update your Homebrew via the Terminal or iTerm (Depending on what Terminal emulator you use). To update the Homebrew, fire up your Terminal and write the following command and hit enter.

brew update

Installing PHP

You need to install PHP for running Laravel Valet on your system. To install PHP, type the following command and hit enter.

brew install php

If you already have PHP installed, you should update your PHP version via the following command.

brew upgrade php

You might see some suggestions on your Terminal while installing PHP. I recommend following the suggestions. The suggestions might look like as follow:

echo 'export PATH="/usr/local/opt/openldap/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/usr/local/opt/openldap/sbin:$PATH"' >> ~/.zshrc
export LDFLAGS="-L/usr/local/opt/curl-openssl/lib"
export CPPFLAGS="-I/usr/local/opt/curl-openssl/include"
export PKG_CONFIG_PATH="/usr/local/opt/curl-openssl/lib/pkgconfig"
echo 'export PATH="/usr/local/opt/libpq/bin:$PATH"' >> ~/.zshrc
export LDFLAGS="-L/usr/local/opt/libpq/lib"
export CPPFLAGS="-I/usr/local/opt/libpq/include"
export PKG_CONFIG_PATH="/usr/local/opt/libpq/lib/pkgconfig"

Or you might get some suggestions like this:

To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so

    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
    /usr/local/etc/php/7.3/

To have launchd start php now and restart at login:
  brew services start php
Or, if you don't want/need a background service you can just run:
  php-fpm

Since the suggestions totally depend on your system, I suggest to follow them carefully. If you get confused or not sure which command you should use, write down a comment in the comment section. We will get back to you ASAP.

Installing Composer

Now, its time to install Composer! Composer will let you install Valet and configure necessary files for your system. Composer is a dependency manager for PHP.

To install Composer on your system, run the following command on your Terminal.

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'a5c698ffe4b8e849a443b120cd5ba38043260d5c4023dbf93e1558871f1f07f58274fc6f4c93bcfd858c6bd0775cd8d1') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

You will get a message on your Terminal saying the Composer is successfully installed.

If you are fond of shell script like me, you can download this script and run it.

To run this script, from Terminal, go to the script directory and type the following line and hit Enter.

chmod -R 777 composer.sh 
./composer.sh

You might need to enter your password to install Composer from the script.

Installing Laravel Valet

Now it is time to install Valet. We will install Laravel Valet via Composer. So, write the following command on your Terminal and hit Enter.

composer global require laravel/valet

Now we need to add something to your .bash_profile. Add the following line to the .bash_profile.

PATH="$HOME/.composer/vendor/bin:$PATH"

If you are not sure how you can add the above file to yours .bash_profile then follow this.

Fire up your Terminal and write the following code

sudo vi ~/.bash_profile

This will open the .bash_profile in Vim editor. Press the i key on your keyboard and find the best place to paste the above code. the type the following command to escape from Vim and save the file.

Alternatively, if you have .bashrc then you can run the following command on your Terminal.

echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bashrc
source ~/.bashrc
:wq

Finally, you need to run the following command to install Valet into your system.

valet install

This command will set up DnsMasq and it will also set up the Valet’s demon to start when the system starts.

You will get a message on your Terminal that Laravel Valet installed on your system.

Stopping nginx...
Installing nginx...
[nginx] is not installed, installing it now via Brew... 🍻
Installing nginx configuration...
Installing nginx directory...
Updating PHP configuration...
Restarting php...
Installing dnsmasq...
[dnsmasq] is not installed, installing it now via Brew... 🍻
Restarting dnsmasq...
Valet is configured to serve for TLD [.test]
Restarting nginx...

Valet installed successfully!

To check if the Valet is correctly installed, you need to ping any *.test domain. Write the following command on your Terminal and see what is the result.

ping myDomain.test
PING mydomain.test (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.029 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.058 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.113 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.048 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.090 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.063 ms

If you have installed Valet correctly, you will see the myDomain is responding to your localhost.

Changing the TLD of Valet

To change the TLD of Valet, you can write the following command on your Terminal. This will change your TLD from .test to .gb.

valet tld gb
Password: [Your PC's Password]
Restarting dnsmasq...
Valet is configured to serve for TLD [.gb]
Restarting php...
Restarting nginx...
Your Valet TLD has been updated to [gb].

Since valet is installed now, let’s move on installing the adminer!

Installing Adminer

Adminer is a single page for maintaining your database. Since we will use MySQL for our WordPress site, we need to install MySQL first. To install MySQL, you need to type the following command on your Terminal.

brew install mysql

Now, start MySQL using the following command

brew services start mysql

We can test if MySQL is started by typing the following command.

mysql -u root -p

You will be asked for typing your MySQL database password. Just hit enter to use MySQL on the command line as the fresh installation of the MySQL has the following credentials.

user: root
password: [blank]

Make your Valet use Php by typing the following command;

valet use php

Now, we need to set a folder where our WordPress will be installed. To do so, go to any directory of your computer using the Terminal and type the following command.

valet park

If you re not sure how can you go to a folder from Terminal, here is an example: I want to add a Project folder in my Documents directory and in the Project folder I will keep all the sites in this folder creating separate folders. So, I need to go to Documents and create a folder and then use the above command; here is the set of commands.

cd ~/Documents
mkdir Projects
cd Projects
valet park. 

You will get the following message.

This directory has been added to Valet's paths.

Let’s test if everything is working.

Create a database using the Terminal.

mysql -u root -p
password: [blank]
create database testdb;

To see if the database is created successfully, type the following command.

show databases;

You will see testdb database is created.

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testdb             |
+--------------------+

for security purpose, let’s change the MySQL’s default password. I am going to change the null password to root. To do so, start your MySQL and follow the following commands;

mysql -u root -p
password: [blank]
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password
BY 'root';  

Your MySQL password is now changed to root.

Now let’s download Adminer from here

For making things easier let’s rename the downloaded file to index.php and place the file into a folder. In my case the tree view of the file is as follow;

I have created a folder in my root folder named mySites

After doing that, you need to go to the mySites folder from your Terminal and write the following command. (You may also keep the admin folder on the folder where you just parked your Valet domains. e.g. Projects folder.)

valet park
You will see the above message after parking a directory

Now from your browser, go to admin.gb and you will see the following page;

Using the username and password you can login now.

From the Create Database, create a database as I have created one as wpdb;

A database wpdb is created.

Let’s Download WordPress now.

Downloading and Installing WordPress

Now we need to download the latest version of WordPress from here.

Now extract the zip folder you have downloaded. You may rename the folder or you can copy all the contents inside the folder. Keep it by creating a folder where you parked your Valet domains. Remember to use the good short name for the new folder because it will be your Valet domain’s name. (In our case this is )

I am going to copy all the contents of the newly downloaded WordPress zip file after unzipping it. And paste in a folder named wordpress. Now I will move the wordpress folder to the /Documents/Projects folder.

From our browser, if we go to wordpress.gb we will now get the famous WordPress installation page!

Famous five minute WordPress installation page!

Now the rest of the process is self-explanatory.

Fill up the text fields with proper text data and press the Install WordPress button. You will see the following page.

Setting up the database connection for WordPress

We already have created our database and other necessary options. Fill out the above form and press the Submit button.

WordPress install confirmation

After the successful installation, you will see the following page.

Now you can log in to your WordPress dashboard by clicking the Login button or going to wordpress.gb/wp-admin URL.

WordPress Dashboard

Congratulations! You have successfully installed WordPress on your OSX machine!

Leave a Reply

Your email address will not be published. Required fields are marked *