Building up on my last post, this is a guide to setting up a buttery smooth PHP development workflow on Ubuntu/Linux.
Requirements and Prerequisites
Going ahead make sure you have these checked off -
- Ubuntu 22.04 LTS or newer
- Homebrew (Linuxbrew) installed
Before we begin, let’s quickly run through pending some system checks -
- Check for updates and upgrades
sudo apt update && sudo apt upgrade
- Then some other packages we’ll need to install PHP
sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https
- Now, let’s add the Ondřej Surý PHP PPA
sudo add-apt-repository ppa:ondrej/php
- And finally, let’s update the package manager cache
sudo apt update
Installing one or more PHP versions
If you have worked on PHP frameworks or libs, you know we have to work on multiple PHP versions at the same time swicthing between projects. So, let’s get started with installing PHP versions.
Now, let’s start by installing PHP 8.1, 8.0 and 7.4 using Homebrew -
brew install php@8.1 php@8.0 php@7.4
You can also install a single PHP version by skipping other params, like this -
brew install php
What the above will do is install the latest PHP version available on Homebrew.
Switching between PHP versions
To switch between these PHP versions at any time, you can use the update-alternatives
command -
sudo update-alternatives --config php
Installing PHP extensions
Most of the time, we need to install some PHP extensions to get our projects up and running. So, let’s install some of the most commonly used extensions -
sudo apt install php8.1-mysql php8.1-mbstring php8.1-xml php8.1-curl
You can replace php8.1
with any other PHP version you have installed or do multiples.
Installing Composer
Composer is a dependency manager for PHP (like NPM for NodeJS). It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
To install Composer, run the following command in your terminal -
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { 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 also like to add the Composer’s global bin directory to your PATH
so the composer
executable can be located by your system. To do so, run the following command -
sudo mv composer.phar /usr/local/bin/composer
Installing Valet for Linux
Valet is a Laravel development environment for Mac minimalists, sadly it only supports macOS at the time of writing this. There is a fork of Laravel Valet available in the name of Valet Linux. It brings all the goodness of Laravel Valet to Linux.
To install Valet Linux, run the following command -
composer global require cpriego/valet-linux
Then, run the following command to initialize Valet Linux -
~/.config/composer/vendor/bin/valet install
or beter yet if you have added the Composer’s global bin directory to your PATH
-
valet install
This will install and configure Nginx, dnsmasq, and the active PHP version you want to use for serving sites on the go.
Setting up a sites folder
To load your sites, you need to create a Sites
directory in your home directory, and link the Sites
directory to Valet Linux’s www
directory.
You can do so by running the following command -
mkdir ~/Sites && cd ~/Sites && valet park
Now any folder you create inside the Sites
directory will be accessible through a URL. For example, if you create a ~/Sites/laravel
directory, it will be accessible at http://laravel.test
.
To get more information about Valet Linux, and configuring it to your wildest dreams visit here - Valet Linux.
Installing MySQL
With site serving setup, you will most likely need a database to store your data. We’ll be using MySQL for this.
To install MySQL, run the following command -
sudo apt install mariadb-server
Then, run the following command to secure your MySQL installation -
sudo mysql_secure_installation
Just follow the questions next and you’ll be good to go.
Installing MySQL GUI client aka TablePlus
TablePlus is a modern, native GUI tool for relational databases, I fell in love with it since I have been using it on macOS. To my surprise, it’s available for Linux as well. So, let’s install it -
- First, add the TablePlus GPG key
wget -qO - https://deb.tableplus.com/apt.tableplus.com.gpg.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/tableplus-archive.gpg > /dev/null
- Then, add the TablePlus repository to your system
sudo add-apt-repository "deb [arch=amd64] https://deb.tableplus.com/debian/22 tableplus main"
- Finally, install TablePlus
sudo apt update && sudo apt install tableplus
You can now configure TablePlus app which is installed in Ubuntu softwares, to connect to your MySQL database.
Conclusion
This was a quick documentation on my setup for PHP development on Ubuntu/Linux. For those who can’t afford a macOS device right now, I personally think this is the best development flow you can have almost identical to that of desired. I truly hope this helps you get started with PHP development on Linux.
This is a work in progress and I’ll keep updating it as I go along. I hope you found it useful. If you have any questions or suggestions, feel free to reach out to me on Twitter.
Happy coding!