Last modified: Jun 06, 2026
Install Sanic in Python Guide
Sanic is a fast Python web framework. It is built for async programming. You can use it to build high-performance web apps. This guide shows you how to install Sanic step by step.
First, you need Python 3.7 or newer. Sanic relies on async features. These features work best in newer Python versions. Check your Python version before you start.
Open your terminal or command prompt. Type python --version or python3 --version. If your version is below 3.7, upgrade Python first. You can download the latest version from python.org.
Step 1: Create a Virtual Environment
A virtual environment keeps your project clean. It prevents conflicts with other packages. This is a best practice for any Python project.
Navigate to your project folder. Then run this command:
python3 -m venv sanic_env
This creates a folder named sanic_env. Inside it, you have a separate Python installation. You can name the folder anything you like.
Now activate the virtual environment. On macOS and Linux, use:
source sanic_env/bin/activate
On Windows, use:
sanic_env\Scripts\activate
You should see the environment name in your terminal prompt. This means you are inside the virtual environment.
Step 2: Install Sanic with Pip
Pip is the standard package manager for Python. It downloads and installs packages from PyPI. Sanic is available on PyPI, so installation is simple.
Run this command in your activated environment:
pip install sanic
Pip will download Sanic and its dependencies. This includes uvloop and httptools. These libraries make Sanic fast. The process usually takes a few seconds.
If you want a specific version, use:
pip install sanic==23.12.0
Replace 23.12.0 with your desired version. Check the Sanic documentation for the latest stable release.
Step 3: Verify the Installation
After installation, verify that Sanic works. Open a Python interactive shell. Type python in your terminal.
Then import Sanic:
# Import Sanic to check installation
import sanic
print(sanic.__version__)
You should see the version number printed. For example:
23.12.0
If no error appears, Sanic is installed correctly. You can now start building your first app.
Step 4: Create a Simple Sanic App
Let’s test Sanic with a basic app. Create a new file named app.py. Write this code:
# app.py - A minimal Sanic application
from sanic import Sanic
from sanic.response import text
# Create a Sanic app instance
app = Sanic("MyFirstApp")
# Define a route for the root URL
@app.route("/")
async def hello_world(request):
# Return a simple text response
return text("Hello, Sanic!")
# Run the app if this file is executed directly
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)
This code creates a basic web server. It listens on port 8000. When you visit http://localhost:8000, you see "Hello, Sanic!".
Run the app with:
python app.py
You should see output like this:
[2023-12-01 10:00:00 +0000] [12345] [INFO] Goin' Fast @ http://0.0.0.0:8000
[2023-12-01 10:00:00 +0000] [12345] [INFO] Starting worker [12345]
Open your browser and go to http://localhost:8000. You will see the message. Press Ctrl+C to stop the server.
Step 5: Install Sanic Extras (Optional)
Sanic has optional extras. These add features like templating or database support. You can install them with pip.
For example, to use Jinja2 templates, run:
pip install sanic[jinja2]
Other extras include sanic[ext] for extensions and sanic[dev] for development tools. Check the Sanic documentation for a full list.
Troubleshooting Common Issues
Sometimes installation fails. Here are common problems and fixes.
Problem: Permission denied error. This happens when you install without a virtual environment. Solution: Always use a virtual environment. If you must install globally, add --user to the pip command.
Problem: Python version too old. Sanic needs Python 3.7+. Upgrade Python to the latest version. You can also use pyenv to manage multiple Python versions.
Problem: Missing C compiler. Some dependencies need compilation. On Linux, install build-essential. On macOS, install Xcode Command Line Tools.
Conclusion
Installing Sanic in Python is straightforward. You create a virtual environment, install Sanic with pip, and verify the installation. Then you can build fast async web apps.
Sanic is a powerful tool for modern web development. Its speed and simplicity make it a great choice. Start with the basic app and explore more features later.
Remember to always use a virtual environment. This keeps your projects organized. Happy coding with Sanic!