How to Install Django
As the official docs note, if you are already familiar with the command line, have already installed the latest version of Python properly, and correctly configured a new dedicated virtual environment, then installing Django can be as simple as running
python -m pip install Django from the command line.
(.venv) $ python -m pip install Django
But even then there is some nuance involved and best practices you should know about such as
requirement.txt files, Git, and customizing a text editor for Python/Django work. This guide will explain all these concepts and provide step-by-step instructions so your computer is properly configured for Django development. In the future, creating or modifying Django projects should require only a few keystrokes.
The Command Line
The Command Line is a text-only interface for your computer. Most everyday users will never need it but software developers rely on it constant to install and update software, use tools like Git for version control, connect to servers in the cloud, and so on.
On Windows, the built-in options is called PowerShell. To access it, locate the taskbar on the bottom of the screen next to the Windows button and type in "powershell" to launch the app.
This will open a new window with a dark blue background and a blinking cursor after the
> prompt. Here is how it looks on my computer, where my current user is named
wsv. Your username will be different.
On macOS, you can access the Command Line through a built-in app called Terminal. It can be opened via Spotlight: press the
space bar keys at the same time and then type in "terminal."
Alternatively, open a new Finder window, navigate to the Applications directory, scroll down to open the Utilities directory, and double-click the application called Terminal.
Once open, the Terminal app consists of a white background by default and a blinking cursor after the
% prompt which shows the current user's name (mine is
Note: Going forward we will use the universal
$ Unix prompt for all commands rather than alternating between
> on Windows and
% on macOS.
The next step is to properly install the latest version of Python (3.11 as of this writing) on your computer.
On Windows, Microsoft hosts a community release of Python 3 in the Microsoft Store. In the search bar on the bottom of your screen type in "python" and click on the best match result. This will automatically launch Python 3.11 on the Microsoft Store. Click on the blue "Get" button to download it.
To confirm Python was installed correctly, open a new Terminal window with PowerShell and then type
$ python --version Python 3.11.3
python to open the Python interpreter from the command line.
$ python Python 3.11.3 (tags/v3.11.3:f3909b8, Apr 4 2023, 23:49:59) [MSC v.1934 64 bit (AMD64)] on win32 Type "help", "copyright", "credits", or "license" for more information. >>>
On macOS, the official installer on the Python website is the best approach. Go to the Python downloads page and click on the button underneath the text "Download the latest version for Mac OS X." As of this writing, that is Python 3.11. The package will be in your
Downloads directory. Double click on it which launches the Python Installer and follow through the prompts.
To confirm the download was successful, open up a new Terminal window and type
$ python3 --version Python 3.11.3
python3 to open the Python interpreter.
$ python3 Python 3.11.3 (v3.11.3:f3909b8bc8, Apr 4 2023, 20:12:10) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
To exit Python from the command line you can type either
exit() and the
Enter key or use
Ctrl + z on Windows or
Ctrl + d on macOS.
By default, Python and Django are installed globally on a computer meaning. If you went to your command line right now and typed
python -m pip install Django then Django would be installed on your computer. But what happens if you need Django 3.2 for one project and Django 4.2 for another? Not to mention, most projects rely on dozens of different software packages that all have different versions. It quickly becomes a mess.
Fortunately there is an easy solution: virtual environments. Virtual environments allow you to create and manage separate environments for each Python project on the same computer. There are many areas of software development that are hotly debated, but using virtual environments for Python development is not one. You should use a dedicated virtual environment for each new Python project.
There are several ways to implement virtual environments but the simplest is with the venv module already installed as part of the Python standard library. To try it out, open the command line. Then navigate to the Desktop directory on your computer with the
cd command, create a new directory with the
mkdir command called
tutorial, and then change directories,
cd, into it.
# Windows $ cd onedrive\desktop\ $ mkdir tutorial $ cd onedrive\desktop\tutorial # macOS $ cd ~/desktop/ $ mkdir tutorial $ cd ~/desktop/tutorial
To create a virtual environment within this new directory use the format
python -m venv <name_of_env> on Windows or
python3 -m venv <name_of_env> on macOS. It is up to the developer to choose a proper environment name but a common choice is to call it
# Windows $ python -m venv .venv # macOS $ python3 -m venv .venv
Once created, a virtual environment must be activated. On Windows, as a safety precaution, an Execution Policy must be set to enable running scripts. This is a one-time procedure that basically tells Windows, Yes I know what I'm doing here. The Python docs recommend allowing scripts for the
CurrentUser only, which is what we will do. On macOS there are no similar restrictions on scripts so it is possible to directly run
Here is what the full commands look like to create and activate a new virtual environment called
# Windows $ python -m venv .venv $ Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser $ .venv\Scripts\Activate.ps1 (.venv) $ # macOS $ python3 -m venv .venv $ source .venv/bin/activate (.venv) $
The shell prompt now has the environment name
(.venv) prefixed which indicates that the virtual environment is active. Any Python packages installed or updated within this location will be confined to the active virtual environment.
To deactivate and leave a virtual environment type
deactivate. Do that now.
(.venv) $ deactivate $
The shell prompt no longer has the virtual environment name prefixed which means the session is now back to normal.
Now that we are familiar with the command line, have installed the latest version of Python, and understand how to work with virtual environments we can finally install Django. Here is what the commands look like to install Django in a new directory.
First, from the command line navigate again to the Desktop, create a new directory called
success, and navigate into it.
# Windows $ cd onedrive\desktop\ $ mkdir success $ cd onedrive\desktop\success # macOS $ cd ~/desktop/ $ mkdir success $ cd ~/desktop/success
Second, create and activate a new virtual environment in the directory.
# Windows $ python -m venv .venv $ .venv\Scripts\Activate.ps1 (.venv) $ # macOS $ python3 -m venv .venv $ source .venv/bin/activate (.venv) $
And third, install Django itself now.
# Windows (.venv) $ python -m pip install django~=4.2.0 # macOS (.venv) $ python3 -m pip install django~=4.2.0
To ensure Django is working correctly, create a new project called
django_project and then type
python manage.py runserver to start the local Django web server.
(.venv) $ django-admin startproject django_project . (.venv) $ python manage.py runserver
In your web browser navigate to
http://127.0.0.1:8000/ and you should see the Django Welcome Page.
Congratulations! You've learned about the Command Line, installed Python, created virtual environments, and properly installed Django. You're well on your way to using Django to built powerful websites.
For even more tips and advice, check out the book Django for Beginners which walks you through building, testing, and deploying five increasingly complex projects with Django.