Warning: You are reading an outdated version of this guide. Unless you're here for a specific reason, you probably want the updated version instead.
This article shows how to install Composer, the standard package manager for PHP, and how to install the PHP packages required for serving a Laravel application.
You might also like this guide from Digital Ocean on the same topic.
Rumor has it you need about 2 GB of RAM to comfortably install Composer packages. If your server has less than 2 GB RAM, consider creating and activating a swap file to make up for the missing RAM (the swap file lets the server use hard disk space to supplement RAM):
# Create a 1 GB swap file (adjust as needed to get about 2 GB total swap + RAM)
laravel@server$ sudo dd if=/dev/zero of=/swapfile bs=1M count=1024 status=progress
laravel@server$ sudo chmod 0600 /swapfile
laravel@server$ sudo mkswap /swapfile
laravel@server$ sudo swapon /swapfile
If you want to permanently keep the swap file, you need to add an entry in the /etc/fstab
file.
To do this, open /etc/fstab
and at the bottom (carefully!) place the following line:
# Place this line at the bottom of /etc/fstab
/swapfile none swap defaults 0 0
If you won’t need the swap file after installing Composer, you can disable the swap file with swapoff
(after installing Composer!), remove any relevant entries from /etc/fstab
, and delete the swap file.
First install dependencies for installing Composer (you’ll probably have many of these already installed):
# Dependencies for installing Composer
# (curl downloads Composer, unzip unpackages it, and php-cli is needed to run it)
laravel@server$ sudo apt install curl unzip php-cli
We’ll perform the installation with Composer’s official installer. I’m essentially following the official instructions; here is what to do:
# Change into your home directory.
# (Or really just somewhere you can keep track of the downloaded files.)
laravel@server$ cd
# Download the Composer installer and save it to the file `composer-setup.php`
laravel@server$ curl https://getcomposer.org/installer -o composer-setup.php
We’ll then perform a cryptographic verification of the installer’s integrity (to ensure the installer hasn’t been corrupted or tampered with):
# Retrieve the latest Composer installer hash.
# You use this hash to verify the integrity of the Composer installer
laravel@server$ HASH=`curl -sS https://composer.github.io/installer.sig`
# Verify installer's hash before actually installing Composer.
laravel@server$ php -r "if (hash_file('SHA384', 'composer-setup.php') === '${HASH}') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Assuming the installer was verified, you’re safe to run the installer:
# Assuming the hash is verified, install Composer (in `/usr/local/bin` to avoid
# conflicting with packages managed by your package manager)
laravel@server$ sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
The last step installs Composer at /usr/local/bin/composer
.
You can safely delete the composer-setup.php
script after completing the installation.
Then change into the directory from which your app is served and use Composer to install your Laravel app’s PHP dependencies.
# Install Composer packages
laravel@server$ cd /srv/www/laravel-project
laravel@server$ composer install --optimize-autoloader --no-dev
This command looks in your Laravel project’s composer.json
file and installs the project’s PHP dependencies into a vendor
directory in your project’s root.
The options are recommended by Laravel; --optimize-autoloader
speeds up autoloading and --no-dev
ignores development dependencies that won’t be needed in production.
(Note that optimize-autoloader
is probably turned on by default in the config
section of your composer.json
file, but it can’t hurt to specify it explicitly.)
Next: The next article shows how to install your application’s JavaScript dependencies.
Finding this tutorial series useful? Consider saying thank you!
The original writing and media in this series is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.