Installing FFmpeg on Laravel Cloud

Laravel Cloud is out, and I’ve already been using it for a few projects. Recently, I ran into a limitation: Laravel Cloud doesn’t allow installing persistent dependencies like FFmpeg. While they might offer this feature in the future, for now, we need to get creative.

Using Build Commands to Install FFmpeg

Laravel Cloud provides a Build Commands feature that lets you execute shell scripts during the build process. We’ll leverage this to install FFmpeg every time the app is deployed.

Initial Build Commands

By default, your build commands should look something like this:


_10
composer install --no-dev
_10
_10
# npm ci --audit false
_10
# npm run build

Now, let’s modify these steps to install FFmpeg.

Installing FFmpeg

We’ll download and install FFmpeg into the project’s home directory ($HOME/bin) so that it’s available during runtime.


_26
# Install & Setup FFmpeg
_26
_26
# Create a `bin` directory for custom binaries
_26
mkdir -p $HOME/bin
_26
cd $HOME/bin
_26
_26
# Download the FFmpeg binaries
_26
curl -L -o ffmpeg.tar.xz https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-arm64-static.tar.xz
_26
_26
# Extract the archive
_26
tar -xf ffmpeg.tar.xz
_26
_26
# Rename the extracted directory
_26
mv ffmpeg-*-static ffmpeg
_26
_26
# Grant execution permissions
_26
chmod +x $HOME/bin/ffmpeg/ffmpeg
_26
_26
# Move back to the project root
_26
cd $HOME/html
_26
_26
# Composer installation
_26
composer install --no-dev
_26
_26
# npm ci --audit false
_26
# npm run build

Using the Correct Binaries

Now that FFmpeg is installed, you can use it in your Laravel application. If you’re using the php-ffmpeg/php-ffmpeg package, configure it like this:


_10
$ffmpeg = FFMpeg\FFMpeg::create([
_10
'ffmpeg.binaries' => '/var/www/bin/ffmpeg/ffmpeg',
_10
'ffprobe.binaries' => '/var/www/bin/ffmpeg/ffprobe',
_10
]);

Refactoring the Installation Script

While adding the installation steps directly into your build commands works, it’s cleaner and more maintainable to move them into a dedicated script.

Creating a Deployment Script

Let’s create a file at ./deploy/ffmpeg.sh to handle the installation.

./deploy/ffmpeg.sh

_32
#!/bin/bash
_32
set -e # Exit immediately if any command fails
_32
_32
# Define the target directory
_32
BIN_DIR="$HOME/bin"
_32
_32
# Ensure the bin directory exists
_32
mkdir -p "$BIN_DIR"
_32
_32
# Change to the bin directory
_32
(
_32
cd "$BIN_DIR"
_32
_32
# Download FFmpeg
_32
echo "Downloading FFmpeg..."
_32
curl --silent --show-error --fail -L -o ffmpeg.tar.xz "https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-arm64-static.tar.xz"
_32
_32
# Extract the archive
_32
echo "Extracting FFmpeg..."
_32
tar -xf ffmpeg.tar.xz
_32
_32
# Rename the extracted directory
_32
mv ffmpeg-*-static ffmpeg
_32
_32
# Set execute permissions
_32
chmod +x ffmpeg/ffmpeg
_32
_32
# Clean up
_32
rm -f ffmpeg.tar.xz
_32
)
_32
_32
echo "FFmpeg installation completed successfully!"

Updating Build Commands

Now, update your Laravel Cloud Build Commands to use the script:


_10
# Install & Setup FFmpeg
_10
./deploy/ffmpeg.sh
_10
_10
# Composer installation
_10
composer install --no-dev
_10
_10
# npm ci --audit false
_10
# npm run build

Deploying & Verifying

Push your code and trigger a new deployment. If everything works correctly, your build logs should contain something like this:


_10
00:00:48 Running build commands ————————————————————— Finished
_10
Downloading FFmpeg...
_10
Extracting FFmpeg...
_10
FFmpeg installation completed successfully!
_10
Setting up Composer...
_10
... Composer logs ...

And that’s it! You now have FFmpeg installed and running on Laravel Cloud. 🎉