Install PHP 8.x on Ubuntu 20.04 for Laravel Projects
Summary:
Installing PHP with essential extensions required by Laravel.
Laravel is one of the most popular PHP frameworks for building robust and modern web applications. If you're setting up a new Ubuntu 20.04 system for Laravel development, it's crucial to install PHP 8.x and all the necessary extensions. This guide will walk you through all the steps required to get PHP 8.x up and running for Laravel projects.
Prerequisites
- A system running Ubuntu 20.04 LTS
- Sudo privileges
Step 1: Update System Repositories
Begin by ensuring your package lists and installed software are up-to-date:
sudo apt update
sudo apt upgrade
Step 2: Add the PHP PPA Repository
Ubuntu 20.04’s default repositories may not provide the latest PHP 8.x versions. Add the Ondřej Surý PHP repository:
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php
sudo apt update
Step 3: Install PHP 8.x
To install PHP 8.2 (recommended latest stable as of writing), run:
sudo apt install php8.2
You can check the installed version:
php -v
Tip: Replace
8.2
with your preferred PHP 8.x release (e.g.,php8.1
).
Step 4: Install Essential PHP Extensions for Laravel
Laravel requires several PHP extensions. Install them all at once with:
sudo apt install php8.2-cli php8.2-fpm php8.2-mysql php8.2-xml php8.2-curl php8.2-mbstring php8.2-zip php8.2-bcmath php8.2-json php8.2-gd php8.2-tokenizer php8.2-redis php8.2-intl php8.2-readline php8.2-soap -y
Core required extensions for Laravel:
mbstring
bcmath
json
xml
ctype
tokenizer
fileinfo
Most of these are included by default or part of the above installation. If you need additional features (like caching, queueing with Redis, or image manipulation), install related extensions like php8.2-redis
and php8.2-gd
.
Step 5: Verify Installation
Check if PHP and all required extensions are installed:
php -m
Scroll through the list or use:
php -m | grep -E "mbstring|bcmath|json|xml|ctype|tokenizer|fileinfo"
This should show all required extensions.
Step 6: Configure PHP Settings (Optional but Recommended)
Edit your php.ini
file for development convenience. Open the configuration file:
sudo nano /etc/php/8.2/cli/php.ini
Suggested settings for Laravel development:
memory_limit = 512M
error_reporting = E_ALL
display_errors = On
date.timezone = "Your/Timezone"
(e.g.,"America/New_York"
)
Make changes, save (Ctrl + X
, then Y
), and exit.
Step 7: Set Up a Web Server
Laravel projects are often served using Nginx or Apache. For development, you can install either.
For Nginx:
sudo apt install nginx
sudo systemctl enable nginx
sudo systemctl start nginx
For Apache:
sudo apt install apache2 libapache2-mod-php8.2
sudo systemctl enable apache2
sudo systemctl start apache2
Step 8: Install Composer
Laravel uses Composer for dependency management. To install Composer globally:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
php -r "unlink('composer-setup.php');"
Verify Composer installation:
composer --version
Conclusion
You now have PHP 8.x and all essential extensions installed on Ubuntu 20.04, ready for Laravel development. With Composer set up, you can easily create new Laravel projects or manage existing ones.
Next Steps:
- Install Laravel with
composer create-project laravel/laravel example-app
- Set up your database and .env configuration
- Start developing your next great web application!
References: