Last modified: Oct 04, 2026

Deploy a Django App with Fabric

Deploying a Django app by hand is slow and error prone. You type the same commands again and again.

Fabric fixes this. It is a Python library that runs shell commands on remote servers.

In this guide, you will learn how to deploy a Django app with Fabric. We will cover setup, code, and common tasks.

What Is Fabric?

Fabric is a tool for running commands over SSH. You write tasks in Python. Then you run them from your terminal.

It is perfect for deployment. You can pull code, install packages, run migrations, and restart services.

Fabric is simple. It uses plain Python functions. This makes it easy to read and change.

Why Use Fabric for Django?

Django projects have many moving parts. You have code, static files, a database, and a web server.

Doing all of this manually takes time. You might forget a step. Fabric keeps everything in one place.

It also works well with any server. You just need SSH access.

Fabric is idempotent friendly. You can run the same task many times without breaking things.

Prerequisites

Before we start, make sure you have these items ready.

  • A Django project in a Git repository.
  • A remote server with SSH access.
  • Python installed on your local machine.
  • Basic knowledge of the command line.

You should also know how to connect to your server. Test it with a simple SSH command.


ssh [email protected]

If that works, you are ready to go.

Install Fabric

Fabric is a Python package. Install it with pip.


pip install fabric

This installs the latest version. Fabric 2 and above use a new API. We will use that API here.

You can check the version with this command.


fab --version

You should see something like Fabric 3.2.2. The exact number may differ.

Create a fabfile.py

Fabric looks for a file named fabfile.py. This file holds your tasks.

Create it in the root of your Django project.


# fabfile.py
from fabric import task

@task
def hello(c):
    """Say hello on the remote server."""
    c.run("echo Hello from Fabric")

Run this task with the fab command.


fab -H [email protected] hello

The output should be simple.


Hello from Fabric

Great. You just ran your first Fabric task.

Set Up Connection Details

Typing the host every time is boring. You can set defaults in the fabfile.

Use a Connection object to store the host and user.


# fabfile.py
from fabric import task, Connection

HOST = "[email protected]"
PROJECT_PATH = "/var/www/myproject"

@task
def deploy(c):
    """Deploy the Django app."""
    with Connection(HOST) as conn:
        conn.run(f"cd {PROJECT_PATH} && git pull")

Now you can run fab deploy without extra flags.

This keeps your commands short and clean.

Write the Deployment Task

A full deployment has several steps. Let us break them down.

First, pull the latest code. Then install requirements. Next, run migrations. Finally, restart the web server.

Here is a complete task.


# fabfile.py
from fabric import task, Connection

HOST = "[email protected]"
PROJECT_PATH = "/var/www/myproject"
VENV_PATH = f"{PROJECT_PATH}/venv"

@task
def deploy(c):
    """Deploy the Django app to the server."""
    with Connection(HOST) as conn:
        # Pull the latest code
        conn.run(f"cd {PROJECT_PATH} && git pull origin main")

        # Install Python dependencies
        conn.run(f"{VENV_PATH}/bin/pip install -r {PROJECT_PATH}/requirements.txt")

        # Run database migrations
        conn.run(f"cd {PROJECT_PATH} && {VENV_PATH}/bin/python manage.py migrate")

        # Collect static files
        conn.run(f"cd {PROJECT_PATH} && {VENV_PATH}/bin/python manage.py collectstatic --noinput")

        # Restart the web server
        conn.sudo("systemctl restart gunicorn")

Each line does one job. This makes the task easy to read.

The sudo method runs a command with admin rights. You may need to enter a password.

You can also use run for normal commands. Use sudo only when needed.

Handle Errors Gracefully

Sometimes a command fails. Maybe the code has a bug. Maybe the server is down.

Fabric stops on error by default. This is good. You do not want a broken deploy.

But you can control this. Use the warn parameter to continue anyway.


# Continue even if the command fails
conn.run("some-command", warn=True)

Use this with care. It can hide real problems.

For deployment, it is better to fail fast. Fix the issue and run again.

Add a Rollback Task

Things go wrong. A rollback task saves the day.

You can reset the code to the last commit. Then restart the server.


@task
def rollback(c):
    """Roll back to the previous commit."""
    with Connection(HOST) as conn:
        conn.run(f"cd {PROJECT_PATH} && git reset --hard HEAD~1")
        conn.sudo("systemctl restart gunicorn")

This is a simple rollback. For real projects, you might use tags or releases.

Still, it shows the power of Fabric. You can automate anything.

Run Tasks on Multiple Servers

Some apps run on more than one server. Fabric can handle this too.

Use a list of hosts. Then loop over them.


HOSTS = ["[email protected]", "[email protected]"]

@task
def deploy_all(c):
    """Deploy to all servers."""
    for host in HOSTS:
        with Connection(host) as conn:
            conn.run(f"cd {PROJECT_PATH} && git pull")
            conn.sudo("systemctl restart gunicorn")

This runs the same steps on each server. It is great for small clusters.

For larger setups, you might use a tool like Ansible. But Fabric works well for many cases.

Keep Secrets Safe

Your fabfile may contain server names and paths. Do not put passwords in it.

Use SSH keys instead. They are safer and easier.

You can also use environment variables. Fabric reads them with os.environ.


import os
HOST = os.environ.get("DEPLOY_HOST")

This keeps your secrets out of the code. It is a good habit.

Test Your Tasks Locally

You can test Fabric on your own machine. Just use localhost as the host.

This helps you catch errors early. You do not need a real server to start.


fab -H localhost deploy

Make sure your local setup matches the server. Then the real deploy will be smooth.

Common Mistakes to Avoid

Beginners often make the same mistakes. Here are a few to watch out for.

  • Forgetting to activate the virtual environment.
  • Running commands in the wrong directory.
  • Not restarting the web server after a deploy.
  • Hardcoding passwords in the fabfile.

Check each step. Use cd before every command that needs it.

Also, log the output. Fabric prints command output by default. This helps you debug.

Conclusion

Fabric makes Django deployment simple and repeatable. You write tasks in Python. Then you run them with one command.

We covered setup, a full deploy task, rollback, and multi-server support. You also learned how to keep secrets safe.

Start small. Write one task. Test it. Then add more.

Soon you will deploy your Django app with Fabric in seconds. No more manual steps. No more mistakes.

Happy deploying!