How to install Django

In this Python Django tutorial, we will learn How to install Django, we will also understand how to install Django in Windows, Linux, or Mac. The list of topics covered in the article is given below.

  • How to install Django in Windows
  • How to install Django in Linux
  • How to install Django in Mac
  • How to check Django version
  • How to install Django in pycharm
  • How to install Django in visual studio code
  • How to install Django rest framework

How to install Django

As Django is a python based web framework, so the first requirement of installing Django is installing python. So, irrespective of any operating system, first we need to install python.

Now, installing python is very easy, you can easily download python from the following official website.

Or you can follow the following article “Python download and Installation steps“. All the steps of installing python in the different operating systems are given in this article.

After installing python, we can easily install the Django framework using PIP. The PIP stands for “Preferred Installer Program” or “Pip Installs Packages” is a recursive acronym for it.

It is a package manager for python, and it is mainly used to install, update, uninstall packages. And it comes by default with python version 3.4 or later.

But before using PIP to install Django, it is recommended as a best practice to provide a separate environment to each of your Django projects. And we can easily manage the environment using the “venv” module in python.

The “venv” module is used to create a lightweight virtual environment with its own site directories. Now, these directories will be separate from the system site directory.

A virtual environment is a separate independent environment with its own Python binary. So, we can use it to install python packages independently in its site directory.

Next, let’s understand how we can create a virtual environment and install Django in the different operating systems. Let’s starts with the windows operating system.

Read Python Django length filter

How to install Django in Windows

In this section, we will learn how to create a virtual environment in windows and use it to install Django.

Now, let’s first create a virtual environment using the python venv module. And for this implementation, first, we open a command prompt, and then we need to navigate to the location where we want to create our project.

Now, to create a virtual environment, we can follow the given syntax.

python -m venv environment_name

In the syntax, we need to provide the name of the environment in place of environment_name. This command will set up the virtual environment and create a directory in the location with the name that we will assign. It is recommended to provide a proper project name for your environment. Here is an example of executing this command.

python -m venv mysite-env

This will create a directory named “mysite-env” in the given location. We can also check for the directory using the “dir” command in the command prompt. This implementation is shown in the following screenshot.

How to create virtual environment for Django
Creating a virtual environment for Django

Next, we need to activate the virtual environment by running the activate.bat file which is in the Scripts directory. And for this, we need to follow the following syntax.

environment_name\Scripts\activate.bat

Let’s use this syntax to activate our “mysite-env” environment.

mysite-env\Scripts\activate.bat

This will activate the virtual environment, and we can easily identify this as the environment name will be visible next to the command prompt. The screenshot of this is given below.

How to activate virtual environment for Django
Activating the virtual environment

Now, we are ready to install Django, and for this, we will use the PIP. Here is the command to install Django using PIP.

python -m pip install Django

After executing the above command, the latest Django release will be downloaded and installed. In our case, Django-3.2.6 will be installed.

How to install Django in Windows
Installing Django in Windows

Read: Python Django vs Flask

How to install Django in Linux / Mac

Now, the steps to install Django in Linux and Mac are the same as there in Windows. The minor difference is in the command for activating the virtual environment. So, in this section, we will understand how we can create a virtual environment and use it to install Django in Linux and Mac operating systems.

Now, to create a virtual environment in Linux or Mac, we can follow the same syntax.

python3 -m venv environment_name

After creating the virtual environment, we need to active it. And for this, we have to use the same command for both Linux and Mac.

source environment_name/bin/activate

By activating the virtual environment, the shell prompt will show the name of the environment which we have activated. Also, it will shift from the system’s python version to virtual environment python.

After this, we can easily use the same pip module to install Django for both Linux and Mac.

python3 -m pip install Django

This will successfully download and install the latest Django release.

Read: ModuleNotFoundError: No module named Django

How to check the Django version

After installing Django in our virtual environment, we can easily check its version. For this, we have to use the following command.

python -m django --version

If we specify Django without its version, then python will automatically download and install the latest Django release. And the latest Django version is 3.2.6.

If we execute the above command, we will get the result in the following way.

How to check the Django version
Output

Read: Python Tkinter

How to install Django in visual studio code

We can also use visual studio code to install the Django module, for this implementation, we have to follow the following given steps.

  • First, we need to install python in our system.
  • Next, start visual studio code, click on the extensions option and install the python extension.
Installing python extension in vs code
Installing python extension in vs code
  • Next, in your file system, create a folder to store the virtual environment and the project files.
  • Now, move back to visual studio code, and from the menu bar either select “Terminal“>”New Terminal” or use Ctrl+Shift+` shortcut to open a terminal in VS Code.
  • After this, use the terminal to move to the project location. And then execute either of the following commands to create a virtual environment.
# Linux | macOS
python3 -m venv env_name

# Windows
python -m venv env_name
creating virtual environment in vs code for django
Example
  • Next, open the project folder in VS Code by selecting the “File“>”Open Folder” option from the menu bar. Or, we can also use Ctrl+O shortchut.
  • Now, select “View“>”Command Palette” option from the manu bar to open command palette. Or use Ctrl+Shift+P shortcut. And then, select the “Python: Select Interpreter” command.
Selecting python interpreter in vs code
Python interpreter option in vs code
  • It will open a list of python interpreters that a visual studio can locate. From the list, select the interpreter which is located in the virtual environment which you have created.
python interpreter in vs code for django
Selecting python interpreter
  • Now, if we open the terminal in VS code then the virtual environment will be automatically activated.

Note: – if you are using Windows then, you have to change your default terminal from Powershell to command prompt in VS Code. And then start the terminal in VS Code. For this, look for “Terminal>Integrated>Default Profile” in the settings and set it to “Command Prompt“.

  • In the end, simply run the following command in the terminal to install the Django module in your virtual environment.
python -m pip install django
How to install Django in visual studio code
Installing Django using visual studio code

Read: Python Tkinter label

How to install Django in pycharm

Similar to visual studio code, we can also use pycharm to install the Django module and create a Django project.

Now, if we are using Pycharm Professional, then we don’t need to install Django separately. A Django project can be created directly in the Pycharm Professional edition. So, for this article, we will discuss how we can install Django in the Pycharm Community edition. For this, we have to follow the following steps.

  • First, start the Pycharm Community edition and create a new project, and select the “New environment using” option as “Virtualenv“. And click on “Create“.
Creating new django project in pycharm
Creating a new project in pycharm
  • Now, if you are using Windows then you have to change the terminal from PowerShell to the command prompt in pycharm. For this, go to “Files“>”Settings” and look for Terminal settings under Tools. Now, select shell path from powershell.exe to cmd.exe.
terminal settings in pycharm
Terminal settings
  • Now, simply open the terminal in pycharm and run the following command to install Django.
python -m pip install django
  • It will automatically download and install the latest Django release in the virtual environment.
How to install Django in pycharm
Installing Django in Pycharm

Read: How to setup Django project

How to install Django rest framework

A Django rest framework is a flexible and powerful library used in Django to build web APIs. So, before installing the Django rest framework, we have to install python and Django.

After successfully installing python and Django, it is super easy to install the Django rest framework. We can easily install it using PIP in python. And this step will be the same for all the operating systems. So, here is the pip command that we can use to install the Django rest framework.

pip install djangorestframework

After executing the command, the Dango rest framework library will be automatically downloaded and installed in the system.

How to install Django rest framework
Example

You may also like reading the following list of articles.

So in this tutorial, we have discussed How to install Django, how to install Django in Windows, Linux, or Mac. And we have also covered the following topics.

  • How to install Django in Windows
  • How to install Django in Linux
  • How to install Django in Mac
  • How to check Django version
  • How to install Django in pycharm
  • How to install Django in visual studio code
  • How to install Django rest framework