Insights from Our Experts

Blog image

Welcome to Fabric!

Author Image

Heera Hariharan,Software Engineer

Fabric is a tool/python library which can be used to automate routine shell commands over SSH. The main attraction of Fabric is to enable automatic deployment of our project using script files and can used as a remote build tool.
Fabric scripts are basic Python files. They run using the fab tool that is shipped with Fabric. All this does is include (i.e. import) your script (i.e. instructions to perform) and execute the provided procedure.


Installation of Fabric

 1. Python 2.5+ with the development headers
 2.Python-setuptools and pip (optional, but preferred) gcc
An easy way to install Fabric is by using the default operating system package  manageraptitude. In order to install Fabric using aptitude, run the following command:

sudo aptitude install fabric

# Alternatively, you can also use *pip*:
# pip install fabric



Python 2.5 and 2.7
For python 3.x this is in development stage


Executing commands on the local Linux machine

Fabric uses to execute tasks. Create a file in your current working directory.

def hello():
    print("Hello world!")

If we put a function like below in the fab file and run the command, we will get the result in our console

$ fab hello
Hello world!

We can also pass arguments to the tasks in the following way. Consider that our task is as follows 

def hello(name="world"):
    print("Hello %s!" % name)

We can run the task using following command

$ fab hello:name=Jeff
Hello Jeff!


Auto Deployment using fabric

Create a in the project

from fabric.api import *

# the user to use for the remote commands
env.user = 'appuser'
# the servers where the commands are executed
env.hosts = ['', '']

def pack():
# build the package
local('python sdist --formats=gztar', capture=False)

def deploy():
# figure out the package name and version
dist = local('python --fullname', capture=True).strip()
filename = '%s.tar.gz' % dist

# upload the package to the temporary folder on the server
put('dist/%s' % filename, '/tmp/%s' % filename)

# install the package in the application's virtualenv with pip
run('/var/www/yourapplication/env/bin/pip install /tmp/%s' % filename)

# remove the uploaded package
run('rm -r /tmp/%s' % filename)

# touch the .wsgi file to trigger a reload in mod_wsgi
run('touch /var/www/yourapplication.wsgi')

The run function will run the commands in our remote server. Writing the appropriate commands will do the auto deployment of our project. Although there are many functions, the most commonly used are:

  • run – which runs a shell command on a remote machine.
  • local – which runs command on the local machine.
  • sudo – which runs a shell command on a remote machine, with root privileges.
  • Get – which downloads one or more files from a remote machine.
  • Put – which uploads one or more files to a remote machine.



Fabric, on being used for multiple purposes, including deployment, restarting of servers, stopping and restarting processes. Task to be done can be written in Python and fabric will take care to execute in all machine languages we specify. We can reuse the “fab files” that is accumulated after being used a bunch of times. Basically, Fabric can be used whenever we need to log in to multiple servers and is simple, powerful and comes with a really good documentation.

I Need

Help for