How To Install Django for Beginners

If you are new to Django, good news! You’ve found the most complete and comprehensive guide on how to install Django on your computer. I will start with some software and tools needed before you can install Django, then into virtual environments, and ultimately into how to install Django step by step. Hopefully, by the end of this post, you’ll have learned how to install the web development framework on your computer and you’d be ready to start building your first Django project. Make sure to always come back to this post for your future Django projects. It always will be there for you.

This is a guide for Django beginners, it assumes that you are installing Django for learning purposes and not for production purposes. Therefore third-party web servers and databases are beyond the scope of this guide.

Now let’s get into it.

Table of Contents

Steps To Install Django.

Before you can use Django, you have to install Python on your computer. That’s the first step to get started with Django.

Another thing to keep in mind is that you need to install Django every time you start a new Django project. That’s because you’ll be using a virtual environment for each project. You could install Django globally for practice, but why not do it the right way from the get-go?

Step 1: Create a folder with the name “[Project Name]”

Okay, so the first thing you need to do is make a folder where you’re going to put your project and your virtual environment. For example, if you’re making a website for to-do lists, you could call the folder something like todoapp or todosite or djangotodo. Just pick a name that makes sense for your project and is short and sweet.

I like to keep my Django projects in this folder:

Documents>> SoftDev>> Projects>> Django

So what I would do is go to the Django folder and make a new folder with my project name. But you don’t have to be as neat as me🙃. You can just make yours on the Desktop if you want.

Some people like to do this on the command line like below:

$ cd documents\softdev\projects\django
$ mkdir todoapp

If you used the command line, don’t close the terminal or switch it, because you’ll need it for the next step.

Did you make a folder where you want it and give it a good name? Awesome! Now let’s get (cd) into that folder and set up a virtual environment.

Step 2: Open your terminal and Cd (Change directory) into your project folder

Now that you have a project folder, let’s make a virtual environment inside it. A virtual environment is a way to isolate your Python packages and dependencies from the rest of your system. This way, you can have different versions of Python and libraries for different projects. To create a virtual environment, you need to use the command line. Don’t worry, it’s not that hard!

First, you need to go to your project folder using the cd command. cd stands for change directory, and it lets you move around your file system. For example, if you are already in the parent folder of your project folder, and you haven’t closed the terminal, you can just type:

$ cd todoapp

And hit enter. This will take you to the todoapp folder. If you are somewhere else, you need to type the full path of your project folder. For example:

$ cd documents\softdev\projects\django\todoapp

You can also copy and paste the path from your file explorer. Just go to your project folder, click on the address bar, and copy the text. Then paste it next to cd in the terminal.

Steps 3: Create a virtual environment, Activate It and Install Django.

This is where the steps divert. With virtualenv, you start by creating the virtual environment itself and then activate it. It’s only after then that you can install Django. On the other hand, with pipenv, Django is installed during the creation of the virtual environment. Then you can activate it. Other than that, there

Option 1: Creating A Virtual Environment With Virtualenv.

Below are the steps to creating a virtual environment with virtualenv.

➊Make sure, on your terminal, you’re in the folder you want to create the virtual environment. The project folder. Refer to the previous step to learn how to do it.

➋In that folder, run the following command.

$ virtualenv env

The env at the end is the name of the virtual environment. Three names are usually used for virtual environments namely env, venv and .env. You can use one that suits you best. Now after the above command runs, you should see a folder inside your project folder with the name you’ve given to the virtual environment. That’s your virtual environment.

Double click the folder to open it. There should be some other folders inside. Double click the Scripts folder. Inside there, you’ll find a file called activate.bat. That file is the one that contains the code to activate the virtual environment. Now, all we have to do is to open the file via the terminal in order to activate the virtual environment.

➌Run the following command to activate the virtual environment:

$ .\env\scripts\activate.bat

The . represents ‘current folder’, in this case, it’s the project folder. env is the name of your virtual environment.

Once that command has run. There should be some brackets at the start of your terminal with the name of the virtual environment to indicate that the virtual environment is now active. Something like below.

(env) $

That’s the one thing I prefer virtualenv over pipenv. Unlike pipenv, it shows you when the virtual environment is activated. Pipenv, on the other hand, does not on the Windows operating system. This can really be problematic since it has the potential to bring mistakes.

For Mac and Linux users, pipenv is perfectly fine. But for Windows, I don’t recommend it for that reason. Anyway, I’ll show you how to use it in case you’re interested.

➍Now it is time to install Django in the activated virtual environment.

Run the following command:

$ pip install django

That’s it. After this command finishes running, Django will be installed in the virtual environment and ready to start its use. Inside env\libraries\ is where you’ll find Django and its dependencies. Take a look at what’s installed. This will install the latest Django version. Check out how to install a specific version if you want to do so.

This concludes our section on how to install Django on Windows using virtualenv. Lets get into an alternate universe of pipenv.

Option 2: Creating A Virtual Environment With Pipenv.

Although there is that given drawback about pipenv, it’s easier to use than virtualenv as you only need two commands to create, activate it, and install Django.

Follow the steps below to install Django using pipenv.

➊Make sure, on your terminal, you’re in the folder you want to create the virtual environment. The project folder. Refer to the previous step to learn how to do it.

➋Run the following command:

$ pipenv install django

This is the killer command that creates both the virtual environment and installs Django in it. If you look into the project folder there are two files, a Pipfile and a Pipfile.lock. Pipfile, among other dependencies, is where Django is installed. This will install the latest Django version. Check out how to install a specific version if you want to do so.

➌Activate the virtual environment using the command below.

$ pipenv shell

That does the job. Unfortunately, you won’t be able to verify if it is active or not if you’re on a Windows computer.

Next Steps After Installing Django…

After installing Django on your computer, it is now time to start building your first Django project. You may need to find some guides on how to start a Django project or some best books to learn Django. Even some best Python books to brush up your python skills before you get deep into Django.

Let me know what you think about this post down in the comments section below. If you found it helpful, please share it with others that you think may need it. I’ll see you in other Coding Gear guides. Peace.

Stephen Mclin
Stephen Mclin

Hey, I'm Steve; I write about Python and Django as if I'm teaching myself. CodingGear is sort of like my learning notes, but for all of us. Hope you'll love the content!

Articles: 125

Leave a Reply

Your email address will not be published. Required fields are marked *