How to Install Uvicorn: A Simple Setup Guide for Python Developers

Learn how to quickly and easily set up Uvicorn, a lightning-fast web server for your modern Python applications.

gunicorn

Getting Started with Modern Python Web Servers

UNIX

What Exactly is Uvicorn?

Uvicorn is a super-fast web server designed specifically for Python. When you build a web application or an API using modern Python frameworks like FastAPI or Starlette, you need a program to actually serve it to the internet. Uvicorn acts as the bridge between your Python code and the users who are trying to access your website from their web browsers.

What makes Uvicorn special is that it uses a modern standard called "ASGI" (Asynchronous Server Gateway Interface). In simple terms, this means Uvicorn can handle many different tasks at the exact same time without getting stuck waiting for one task to finish. Because of this, it is incredibly fast and can easily manage a huge number of users visiting your application all at once.

Before Uvicorn and ASGI existed, older Python web servers usually had to handle requests one by one in a line. Uvicorn changed the game by bringing the power of "asynchronous" (non-blocking) programming to Python web servers. Today, it is the standard and most popular choice for developers who want to run high-performance Python web applications easily.

Prerequisites

  • Internet Connection: An active connection to download the required packages.
  • Operating System: A computer running Windows, macOS, or Linux.
  • Python Installed: Python version 3.10 or newer installed on your system.
  • Command Line: Basic knowledge of how to open and use a command-line interface (Terminal on Mac/Linux, or Command Prompt/PowerShell on Windows).
  • Package Manager: pip (Python's default package installer) available on your computer.

Step-by-Step Installation

1

Verify Python Installation

First, open your terminal or command prompt and check that Python is installed and running a supported version (3.10+).
BASH
python --version
2

Create and Activate a Virtual Environment

It is highly recommended to install Python packages inside a virtual environment to keep your project files organized and prevent conflicts with other projects.
Create the environment:
BASH
python -m venv .venv
Activate the environment (macOS/Linux):
BASH
source .venv/bin/activate
Activate the environment (Windows):
BASH
.venv\Scripts\activate
3

Install Uvicorn

There are two ways to install Uvicorn. We recommend the "Standard" installation for the best speed and performance.
Option A: The Standard Installation (Recommended)
This installs Uvicorn along with extra tools that make it run much faster. This is the best choice for almost all developers.
BASH
pip install 'uvicorn[standard]'
Option B: The Minimal Installation
If you want a very small footprint or are working on a system that cannot install extra C-based tools, you can install the pure Python version.
BASH
pip install uvicorn
4

Verify the Installation

Check that Uvicorn was installed successfully by asking for its version number.
Bash
uvicorn --version
5

Test the Server with a Basic App

To prove your new server works, create a simple Python file and run it.
1. Create a file named example.py and paste this code inside:
Python
async def app(scope, receive, send):
    assert scope['type'] == 'http'
    
    await send({
        'type': 'http.response.start',
        'status': 200,
        'headers': [
            (b'content-type', b'text/plain'),
            (b'content-length', b'13'),
        ],
    })
    
    await send({
        'type': 'http.response.body',
        'body': b'Hello, world!',
    })
2. Run the server from your terminal:
Bash
uvicorn example:app --reload
Congratulations! You can now open your web browser and go to http://127.0.0.1:8000 to see your running Uvicorn server display "Hello, world!".

Discover CTCservers Dedicated Server Locations

CTCservers servers are available around the world, providing diverse options for hosting websites. Each region offers unique advantages, making it easier to choose a location that best suits your specific hosting needs.