DevPush

How to setup PHP on a Linux desktop - Ubuntu 23.10

Introduction

This is a guide to a set-up that I use personally on my main home PC, yes my PC uses Ubuntu as the main operating system.

As a PHP / Javascript developer, I work on personal projects therefore I need to use specific applications to work on them.

This guide will focus on the use of the Linux terminal to install applications and configure setups. This can be done more easily by using Ubuntu's App Center application however I much prefer using the terminal plus some of these steps can be applied when setting up an Ubuntu web server.

Please note: Ubuntu 23.10 is an interim version which will be supported till July 2024. However, I will be upgrading to Ubuntu 24.04 on April 25, 2024, as this version is a Long term support version it will come with 5 years of support upon release.

Ubuntu Install

If you want to do a clean install of Ubuntu 23.10 then the Ubuntu website provides an install guide.

Follow the guide to create an Ubuntu bootable USB stick and install the OS on a PC. Once Ubuntu is installed and booted you are ready to follow this guide.

Development setup

Once you have Ubuntu up and running you can now start to set it up.

This guide uses the terminal for setting up all the applications, the benefit of doing this is that I can create a setup script that will install all applications much faster.

The very first thing to do is to run the update and upgrade commands. Update will update the repository package lists for all repositories and upgrade will download and update applications that are out of date.

sudo apt update && sudo apt upgrade -y

The next command installs a few utilities that I like to use.

Here is a description of each of them...

  • vim - Text editor to be used inside of the terminal
  • neofetch - A command to show system information
  • curl - Get data from a URL
sudo apt install -y vim neofetch curl

As this guide uses vim here is a list of commands to using the vim text editor.

  • Go into file by typing vim filename
  • A key - Switch to write mode
  • Esc key - Get out of write mode
  • Type ":w" and press Enter key - Save changes to file
  • Type ":q" and press Enter key - Exit out of file

Applications

Google Chrome

Ubuntu already comes with the Firefox web browser however my personal preference is Google Chrome plus it's currently the most used web browser worldwide.

Also as a developer logically it makes sense to use Google Chrome as you would want your web apps to work in the most popular browser first.

Download the deb file for Google Chrome stable version with the following command...

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

Install Google chrome using the deb file downloaded.

sudo apt install ./google-chrome-stable_current_amd64.deb

Visual Studio Code

An integrated development environment (IDE) is an application used to program code. My IDE of choice is Visual Studio Code from Microsoft which I use for both PHP and Javascript development.

The following command will get the Microsoft package key, verify the package using the key and create the source package list file.

sudo apt-get install gpg
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg
sudo install -D -o root -g root -m 644 packages.microsoft.gpg /etc/apt/keyrings/packages.microsoft.gpg
sudo sh -c 'echo "deb [arch=amd64,arm64,armhf signed-by=/etc/apt/keyrings/packages.microsoft.gpg] https://packages.microsoft.com/repos/code stable main" > /etc/apt/sources.list.d/vscode.list'
rm -f packages.microsoft.gpg

Install APT transport to be able to install packages with a HTTPS URL.

sudo apt install apt-transport-https

Update the vscode package list.

sudo apt -y update

After the update, you can now install VS Code.

sudo apt install -y code 

Sublime Text 3

Even though I have VS Code to work on code I still like to use an alternative text editor when I want to open a single file.

Download and add the sublime text package key.

curl -fsSL https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add

Add a repository for the Sublime Text application in the source list.

sudo add-apt-repository "deb https://download.sublimetext.com/ apt/stable/"

Install Sublime Text editor 3.

sudo apt install -y sublime-text

Beekeeper Studio

For every PHP application I work on and sometimes Node.js I will use MySQL to retrieve and store data. My chosen MySQL client that I like to use is Beekeeper Studio because of its simplicity and ease of use.

Get and add the Beekeeper studio key.

wget --quiet -O - https://deb.beekeeperstudio.io/beekeeper.key | sudo apt-key add -

Create the Beekeeper studio source list file.

echo "deb https://deb.beekeeperstudio.io stable main" | sudo tee /etc/apt/sources.list.d/beekeeper-studio-app.list

Run the update command to update the Beekeeper source list.

sudo apt -y update

Install the Beekeeper Studio application.

sudo apt install -y beekeeper-studio

MongoDB Compass

MongoDB is a popular NoSQL solution that's usually paired with Node.js, there have been times where I've worked with it on projects.

Get the deb file for mongodb-compass.

wget https://downloads.mongodb.com/compass/mongodb-compass_1.41.0_amd64.deb

Install MongoDB Compass via the deb file.

sudo apt install ./mongodb-compass_1.41.0_amd64.deb

Postman

Postman is the most popular API client, almost all backend applications that I create are API applications so I would create and store endpoints in Postman to make it easy to run and test.

I used the snap package manager to install Postman in Ubuntu.

sudo snap install postman

PHP & Node.js

PHP 8.3

Add the personal package archive for PHP packages.

sudo add-apt-repository ppa:ondrej/php

Ubuntu 23.10 specific change

The ondrej/php archive only supports LTS versions of Ubuntu so the source list needs to be updated to the Ubuntu 22.04 version.

sudo vim /etc/apt/sources.list.d/ondrej-ubuntu-php-mantic.sources

While in the text editor change "mantic" to "jammy" and then save the change to the file.

Now run the update command which should process through without any issue.

sudo apt -y update

After the update, you can now install PHP 8.3.

sudo apt install php8.3 -y

PHP 8.3 modules

The list of modules I installed are the required modules to run the PHP framework Laravel as well as other commonly used modules.

sudo apt install -y php8.3-bcmath php8.3-redis php8.3-curl php8.3-zip php8.3-gd php8.3-mbstring php8.3-gmp php8.3-dom php8.3-mysql php8.3-dev php8.3-sqlite3

You can see the new modules installed by using the PHP module list command.

php -m

Composer

Download and move the composer setup file to the tmp directory.

curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php

Run the composer setup file and install to the /usr/bin/local folder to enable Composer to be run globally.

sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer

To verify that Composer was installed correctly run the composer command.

composer

Node.js / Node package manager

Typically Node.js is required for PHP development when working with Javascript to install npm packages.

I use the node version manager to install Node.js as it grants the current user account access to the npm commands.

Get and run the install script from the git repository.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

Check that nvm is installed and working.

command -v nvm

Use nvm to install the latest version of Node.js, go to the Node website to check or the latest LTS version.

nvm install 20.11.1

Verify that Node is installed and that the Node version is correct.

node -v

Also, check that the node package manager is also installed.

npm -v

Services

Apache (Removal)

Ubuntu comes pre-installed with the Apache web server.

As I don't use Apache for local web development then I tend to remove it to prevent clashes when using the PHP / JavaScript built-in web server for local development.

Check that the apache2 command returns the current directory for where Apache is located.

which apache2

Also verify that Apache is running with the status service command.

sudo service apache2 status

Stop Apache from running.

sudo service apache2 stop

Now remove the Apache application.

sudo apt remove -y apache2

Also, run the autoremove command to remove any unneeded related packages.

sudo apt -y autoremove

Docker

For storing data in my applications I use docker containers to run and host database related services.

Get and store the docker authentication key.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Run the deb file and set up the source list for docker.

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Update and install docker.

sudo apt -y update && sudo apt install -y docker-ce

To verify check that docker is running.

sudo service docker status

Grant the user account access to docker.

sudo usermod -aG docker ${USER}

Verify that docker is working.

docker -v

Also check that docker compose is available and working.

docker compose version

Terminal

Oh My Zsh

To begin install Z shell with the Apt package manager.

sudo apt install zsh -y

Check that Z shell is working, the path should return after running the below command.

which zsh

Finally download and run the install script for Oh My Zsh.

sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

Alias commands

I use Zsh to set up alias commands so it's easier to run commands that that are longer to type and/or difficult to remember.

Change the current directory into the .oh-my-zsh/custom folders.

cd ~/.oh-my-zsh/custom

Create a new file called aliases.zsh.

sudo vim aliases.zsh

Copy and paste the below commands or add in your own, save the file and then exit it.

alias shortcuts="cat ~/.oh-my-zsh/custom/aliases.zsh"
alias dc="docker compose"
alias hosts="cat /etc/hosts"
alias ip="hostname -I && curl icanhazip.com"

Plugins

Oh My Zsh comes with plugins with some that are bundled with the software already and others that require downloading.

To begin download and setup the Zsh Autosuggestions plugin.

git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

Open the .zshrc file.

sudo vim ~/.zshrc

Locate the plugin key and change the value to include the below plugin list, finally save and close the file to enable the change.

plugins=(git copypath copyfile history z zsh-autosuggestions zsh-tab-title)

Theme

I use the theme Power Level 10k for my terminal usage.

Download the theme and add it to the themes folder inside of Oh My Zsh.

git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

Open the .zshrc file.

sudo vim ~/.zshrc

Locate the ZSH_THEME key and change the value to "powerlevel10k/powerlevel10k".

ZSH_THEME="powerlevel10k/powerlevel10k"

Code Sniffer

Code sniffer is used to validate the format of PHP code to ensure it follows the rules of the set coding standard.

Use Composer to install PHP Codesniffer globally.

composer global require --dev squizlabs/php_codesniffer

Change the current directory to the Oh My Zsh - Custom folder.

cd ~/.oh-my-zsh/custom

Create a new file called scripts.zsh.

sudo vim scripts.zsh

Add in the following command to update the PATH variable, save and close the file.

export PATH="$PATH:/var/opt/sonar-scanner/bin"

Now PHP Codesniffer should be working in the terminal, check for the version to verify.

phpcs --version

You can also print a list of the included coding standards available.

phpcs -i

Version control

SSH Key

An SSH key is required to authenticate Git so that code can be sent and received from Git repositories in GitHub.

Make a .ssh folder in your user directory and change to it.

mkdir ~/.ssh && cd ~/.ssh

Call the ssh-keygen command to generate an SSH key using your email address.

Change to the email-address you want to use where it states "[email protected]".

ssh-keygen -t ed25519 -C "[email protected]"

Git

Git is required to be installed to connect to GitHub repositories. Install via the Apt package manager.

sudo apt install -y git

After installation set the current username for your git config.

Replace "Me" with your username.

git config --global user.name "Me"

Also, set your email address for your git config.

Repalce "[email protected]" with your email address.

git config --global user.email "[email protected]"

Run the list command for git config to verify that the username and email address were set up correctly.

git config -l

Gnome extensions

There are a couple of Gnome extensions that I use along with the Ubuntu OS. Install the Gnome extension manager to install and be able to set up those extensions.

sudo apt install -y gnome-shell-extension-manager

Vitals

Vitals shows real-time information on hardware components inside of your PC such as your CPU, RAM, HDD etc.

This extension requires two applications to be installed.

sudo apt install -y gir1.2-gtop-2.0 lm-sensors

Open the extension manager application, once the app is open select the Browse tab. Do a search for Vitals and install, after installation, you should see the Vitals widget appear in the top right corner of your screen.

Space Bar

Space bar is another extension I rely on to enhance my usage of workspaces since I make heavy use of workspaces, especially for web development which requires the use of a good number of applications.

Inside of the extension manager within the Browse tab search Space bar and install. After installation in the top left corner, you will see the number/s representing your current workspaces in your Ubuntu system.

Extra applications

These applications aren't required for PHP / Javascript development however since this setup is on a personal PC then I make use of other applications for general use outside of professional work.

Spotify

Get the Spotify key to authenticate the package for installation.

curl -sS https://download.spotify.com/debian/pubkey_7A3A762FAFD4A51F.gpg | sudo gpg --dearmor --yes -o /etc/apt/trusted.gpg.d/spotify.gpg

Create the source list file for Spotify.

echo "deb http://repository.spotify.com stable non-free" | sudo tee /etc/apt/sources.list.d/spotify.list

Update and install Spotify.

sudo apt -y update && sudo apt install -y spotify-client

VLC Player

The Apt package manager already has VLC Player available so its just a single command to install.

sudo apt install -y vlc

Steam

Get the deb file for Steam.

wget https://cdn.akamai.steamstatic.com/client/installer/steam.deb

Install the application, after installing Steam may need to update after you open the application.

sudo apt install ./steam.deb

VirtualBox

I solely use Ubuntu on its own as a desktop PC. If I wanted to use Windows then I would use VirtualBox to set up a virtual machine with Windows installed so it runs within the Ubuntu OS.

Ubuntu comes with the VirtualBox package source so can be installed easily.

sudo apt install -y virtualbox

Flatpak

If I can't install applications because there isn't a Debian package available or there are compatibility issues with the version of Ubuntu then I would resort to using Flatpak to install some applications.

sudo apt install -y flatpak

Wine

I've had some difficulty with installing Wine with the Debian package so I resorted to installing via flatpak.

I had a GitHub repository setup which bundles Wine with the WineZGui application, to install and run the list of commands below.

flatpak --user remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
flatpak --user -y install flathub org.winehq.Wine/x86_64/stable-22.08
flatpak -y remove io.github.fastrizwaan.WineZGUI
wget -c https://github.com/fastrizwaan/flatpak-wine/releases/download/0.97.12-1/flatpak-winezgui_0.97.12_20230908.flatpak
flatpak --user install flatpak-winezgui_0.97.12_20230908.flatpak

To run the WineZGui application use the following command.

flatpak run io.github.fastrizwaan.WineZGUI

Also, wine can be used as normal through the following command.

flatpak run --command=wine io.github.fastrizwaan.WineZGUI

Conclusion

After following this guide you will have a fresh install of Ubuntu desktop OS with the applications and libraries set up to be ready to develop PHP applications.

As this is my personal setup I will aim to keep this guide in sync with my current setup plus it allows me to refresh my memory on how I set up everything in the first place.

As mentioned in the introduction the next release of Ubuntu is 24.04 Noble Numbat scheduled for April 25th of this year. I will be upgrading to that new version as it will be a long-term release version as the current 23.10 will have support dropped in July 2024 which gives me two to three months before this version reaches the end of life.