0Pricing
PHP Academy · Lesson

Installing and Initializing Composer

Install Composer globally and create a composer.json project file.

Installing and Initializing Composer is a free PHP Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is Composer?

Composer is PHP's dependency manager. It downloads packages from Packagist, resolves version conflicts, and generates an autoloader.

Installing Composer Globally

Download the installer and move the phar to a system path.

# Download installer
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer

Verifying the Installation

Run composer --version to confirm it is installed correctly.

$ composer --version
Composer version 2.7.0

composer init

composer init runs an interactive wizard that creates composer.json for a new project.

$ composer init
  Package name: vendor/my-app
  Minimum Stability: stable

composer.json Structure

A minimal composer.json:

{
  "name": "acme/my-app",
  "require": {
    "php": ">=8.1"
  },
  "autoload": {
    "psr-4": { "App\\": "src/" }
  }
}

The require Key

The require object lists runtime dependencies and their version constraints. Composer resolves the best compatible versions.

The require-dev Key

require-dev lists development-only dependencies excluded from production installs with --no-dev.

Version Constraints

Common operators: ^1.2 (compatible 1.x), ~1.2 (>=1.2, <2.0), >=2.0 (lower bound).

Running composer install

After creating composer.json, run composer install to download packages and generate vendor/autoload.php.

$ composer install
Installing dependencies from lock file...

The vendor/ Directory

All installed packages live in vendor/. Add it to .gitignore — commit only composer.json and composer.lock.

Packagist

Packagist (packagist.org) is the default package repository. Search for packages and copy the require line.

Summary

Composer automates dependency resolution and autoloading. Use composer init to bootstrap, composer install to download, and commit only composer.json and composer.lock.

Quick Check

Which file declares a project's Composer dependencies?

Frequently asked questions

Is the “Installing and Initializing Composer” lesson free?

Yes — the full text of “Installing and Initializing Composer” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Installing and Initializing Composer”?

Install Composer globally and create a composer.json project file. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Installing and Initializing Composer” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Installing and Initializing Composer
  2. Requiring and Updating Packages
  3. Understanding composer.lock
  4. Composer Scripts and Autoloading
← Back to PHP Academy