This article will assume you have Ubuntu 24.04 installed on your Windows 11 computer via WSL.

You should understand the basics of Ubuntu’s package manager apt, as well as the basics for terminal usage.

Valet is Laravel’s tool for local development. It simplifies managing Nginx and PHP when working on multiple projects. I’ve always used Docker to power my development environments, but I’ve recently decided to pivot to installing and managing my LAMP stack directly in my Ubuntu WSL environment.

Installing NGINX and PHP

To install both NGINX and PHP into Ubuntu, we must first add the PPA repository. This will allow apt to access these packages.

sudo add-apt-repository ppa:ondrej/php
sudo add-apt-repository ppa:ondrej/nginx

Once added, you’ll then update the available apt packages.

sudo apt-get update

Install NGINX

sudo apt install nginx -y

Once this has been installed, you should be able to see the NGINX landing page on your localhost.

Install PHP

One of the advantages of valet-linux is being able to swap between different PHP versions quickly. For this to work, we need to have multiple versions installed. I’d recommend installing specific versions by using the version number in the package name. E.g: php8.3.

sudo apt-get install php8.3-fpm php8.3-curl php8.3-mysql
php8.3-mbstring php8.3-mcrypt php8.3-xml php8.3-zip

If you then want to also install version 7.4, you can re-run the above with the different version numbers.

I’ll explain version swapping shortly, but for now, you can check PHP is working correctly by running a command such as:

php -v
# PHP 8.3.14 (cli) (built: Nov 25 2024 18:07:43) (NTS)
# Copyright (c) The PHP Group
# Zend Engine v4.3.14, Copyright (c) Zend Technologies
#    with Zend OPcache v8.3.14, Copyright (c), by Zend Technologies

Install Valet Requirements

Ubuntu Packages

Valet requires a few packages to run correctly. You can install them with the following command:

sudo apt-get install network-manager libnss3-tools jq xsel

Composer

This requires PHP to be installed and working first.

We need PHP’s package manager: Composer. Their installation instructions can be found on their website: https://getcomposer.org/download/

Installing Valet-Linux

First, we must download valet-linux via Composer so it’s globally accessible.

composer global require cpriego/valet-linux

We can then install it into the system:

valet install

If the valet command cannot be found, it might be because the install location isn’t set in your $PATH. This means that your terminal session won’t look in its directory. To add it to $PATH, you can run the following:

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

If you use ZSH instead of Bash, then swap .bashrc for .zshrc at the end.

You can then restart your terminal session and try the install again.

Using Valet-Linux

You can view all of the available commands by running Valet without any arguments.

valet

Or you can read their documentation: https://cpriego.github.io/valet-linux/

Serving Sites

Navigate to the directory housing your projects and then run:

valet park

This will serve every folder from the current directory as its own website on *.test. Speaking of the test subdomain, however, if you’re using Windows’ hosts file, then I’d suggest changing this:

valet domain localhost

Windows is configured to server all localhost subdomains via the browser. If you’re not sure why I’m suggesting this, I’d encourage you to just do it and save yourself the headache.

Swapping PHP Versions

Valet allows you to easily swap between installed versions of PHP by running:

valet use 7.4 # Or whichever other version you want to use.

The only nuance here is that if you switch to a different major version, such as going from 8.3 down to 7.4, then Valet commands won’t work until you switch back, as it’ll be set to require a higher PHP version. In this case, you can swap back using:

sudo update-alternatives --set php /usr/bin/php8.3

To simplify this process, I wrote a quick alias:

# Set the PHP version for both CLI and Valet
function set_php_version() {
    if [[ $(php -v | grep -oP '^PHP \K[0-9]+\.[0-9]+') == "8.3" ]];
    then
        valet use $1
    else
        sudo update-alternatives --set php /usr/bin/php$1
        valet use $1
    fi
}
alias php_set=set_php_version

Troubleshooting

If you have any issues with the Valet installation process, please go back through and verify each requirement installed successfully.

If you’re finding that a command which is provided here is broken, it might be that something has changed, or your environment is different to what is required for this guidance. In that case, please go to the package’s own website or GitHub to see the latest installation instructions, then drop me a comment so I update this article to help save any confusion for future readers.

If Valet has installed but isn’t working, you can see its current state with:

valet status

If NGINX says it is not running, you can enable it with:

sudo systemctl start nginx

Leave a Reply

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