How to Fix WP-CLI Not Working in LocalWP on Windows (Git Bash)

✓ Verified Fix

If you’ve tried running WP-CLI inside LocalWP on Windows and hit a wall of errors, you’re not alone. The default LocalWP shell opens Windows Command Prompt, which can’t run WP-CLI properly. This guide shows you the exact steps to get WP-CLI working in Git Bash on Windows 11 — including the mysqli connection fix that most guides miss.

What you’ll see if you have this problem:

@ECHO: command not found
Could not open input file: %~dp0\..\wp-cli.phar

Or after switching to Git Bash:

Error: Your PHP installation appears to be missing the MySQL extension which is required by WordPress.

Or after fixing that:

Error: Error establishing a database connection.

All three errors have different causes and different fixes. Here’s how to solve all of them in order.


Step 1: Install Git Bash and set up the WP-CLI alias

LocalWP on Windows opens cmd.exe by default. The Windows version of WP-CLI (wp.bat) doesn’t work in bash. You need Git Bash instead.

If you don’t have Git for Windows installed, download it from git-scm.com. Once installed, open Git Bash (not LocalWP’s shell).

First, find LocalWP’s PHP and WP-CLI binaries:

find "/c/Users/YOUR_USERNAME/localwp" -name "php.exe" 2>/dev/null
find "/c/Users/YOUR_USERNAME/localwp" -name "wp-cli.phar" 2>/dev/null

You should see paths like:

/c/Users/YOUR_USERNAME/localwp/resources/extraResources/lightning-services/php-8.2.29+0/bin/win64/php.exe
/c/Users/YOUR_USERNAME/localwp/resources/extraResources/bin/wp-cli/wp-cli.phar

Now create a permanent alias in Git Bash. Open your profile:

nano ~/.bashrc

Add these two lines at the bottom (replace YOUR_USERNAME with your actual Windows username):

PHP_BIN="/c/Users/YOUR_USERNAME/localwp/resources/extraResources/lightning-services/php-8.2.29+0/bin/win64/php.exe"
WP_CLI="/c/Users/YOUR_USERNAME/localwp/resources/extraResources/bin/wp-cli/wp-cli.phar"
alias wp="$PHP_BIN $WP_CLI"

Save with Ctrl+O, then Ctrl+X. Reload:

source ~/.bashrc

Step 2: Enable the mysqli extension

LocalWP’s PHP binary ships without a php.ini file, so the mysqli extension isn’t loaded. Without it, WP-CLI can connect to PHP but not to your WordPress database.

Check if mysqli is loaded:

/c/Users/YOUR_USERNAME/localwp/resources/extraResources/lightning-services/php-8.2.29+0/bin/win64/php.exe -m | grep -i mysql

If you only see mysqlnd and not mysqli, you need to create a php.ini file. Run this command (adjust the path to match what you found in Step 1):

cat > "/c/Users/YOUR_USERNAME/localwp/resources/extraResources/lightning-services/php-8.2.29+0/bin/win64/php.ini" << 'EOF'
extension_dir = "C:\Users\YOUR_USERNAME\localwp\resources\extraResources\lightning-services\php-8.2.29+0\bin\win64\ext"
extension=mysqli
extension=mbstring
extension=openssl
extension=pdo_mysql
extension=curl
extension=gd
extension=intl
extension=zip
EOF

Verify it worked:

/c/Users/YOUR_USERNAME/localwp/resources/extraResources/lightning-services/php-8.2.29+0/bin/win64/php.exe -m | grep -i mysql

You should now see mysqli, mysqlnd, and pdo_mysql.


Step 3: Fix the database connection

Even with mysqli working, WP-CLI will fail to connect because LocalWP doesn’t use the standard MySQL port 3306. It runs MySQL on a random high port — typically something like 10013 or 10016.

Find your site’s actual port: open the LocalWP app → click your site → Database tab. Note the port number shown there.

Now update your site’s wp-config.php to use the correct host and port. Navigate to your site folder and run:

cd "/c/Users/YOUR_USERNAME/Documents/localwpsites/YOUR_SITE/app/public"

sed -i "s/define( 'DB_HOST', 'localhost' );/define( 'DB_HOST', '127.0.0.1:YOUR_PORT' );/" wp-config.php

Replace YOUR_PORT with the port number you found in the LocalWP Database tab.

Verify the change:

grep "DB_HOST" wp-config.php

Should show: define( 'DB_HOST', '127.0.0.1:10016' ); (with your actual port number).


Step 4: Test WP-CLI

Navigate to your WordPress site root and run:

cd "/c/Users/YOUR_USERNAME/Documents/localwpsites/YOUR_SITE/app/public"
wp option get blogname

If WP-CLI is working correctly, it returns your site name:

Fix That Works

You can now run any WP-CLI command from Git Bash.


Step 5: Fix WP-CLI scripts (alias not available inside bash scripts)

If you write a bash script that uses wp commands, the alias from ~/.bashrc won’t be available inside the script. Your script needs to reference the full PHP path directly.

Add this line at the top of any bash script that uses WP-CLI:

WP="/c/Users/YOUR_USERNAME/localwp/resources/extraResources/lightning-services/php-8.2.29+0/bin/win64/php.exe /c/Users/YOUR_USERNAME/localwp/resources/extraResources/bin/wp-cli/wp-cli.phar"

Then use $WP instead of wp throughout the script:

$WP option update blogname "My Site"
$WP plugin activate my-plugin
$WP post delete 1 --force

This works regardless of how the script is called.


FAQ

Why doesn’t LocalWP’s built-in shell work for WP-CLI? LocalWP on Windows opens cmd.exe, which calls the Windows batch version of WP-CLI (wp.bat). This batch file is a wrapper that expects a specific environment that isn’t set up in cmd. Git Bash uses the Linux-compatible version directly, which works correctly.

Will changing DB_HOST in wp-config.php break my local site? No. WordPress handles 127.0.0.1:10016 the same as localhost for database connections. Your local site will continue to work normally in the browser. The only thing that changes is WP-CLI can now connect to the database.

What if my LocalWP MySQL port is different from 10016? Every LocalWP site gets a different random port. Always check the actual port in LocalWP app → your site → Database tab before editing wp-config.php. Common ports are 10013, 10015, 10016, but yours may be different.


Tested on: Windows 11, LocalWP 9.x, WP-CLI 2.12.0, PHP 8.2.29, WordPress 6.9.4