How to install Django in a virtual environment

Share your love

In every tutorial you may use on the internet, most of the Django installation is done in a virtual environment. There are alternatives, such as installing Django in a system-wide environment. What method should you choose to install Django?

Should I install Django in a virtual environment?

You should install Django in a virtual environment to avoid conflicting dependencies between Django, its dependencies, and other packages installed in the system-wide environment. Besides, installing Django in a virtual environment is recommended as it provides an isolated environment, which helps control versions used across different Django projects.

A virtual environment helps keeps dependencies required by different projects separated.

Types of environments you can use to install Django?

There are two ways of installing Django in an environment:

  1. Python virtual environment or
  2. System-wide environment.

Installing Django in a system-wide environment

A system-wide environment is the environment where all your applications and utilities are installed on your Linux system. 

Django is installed in the same directory as other applications, libraries, and packages in a system-wide environment.

That means that Django and other applications use the same Python version installed on your system. 

What potential problems could arise from installing Django in a system-wide environment?

1. Dependency issues across packages installed globally

Dependency issues may arise when a particular application requires a specific library or package version.

Where another package in the same system-wide environment requires a particular version of a library to operate and the version changes, there will likely be some issues with dependencies.

Installing Django in a system-wide environment may end up updating a particular package to the latest version, which another application in the same environment may not support.

Therefore, before you install Django on the system-wide environment, be ready to face dependency issues at some point.

2. Locked out on using only one Django version across all your projects

Another problem that may arise is that when you have multiple projects on your system, all the projects will be using the same version of Django. You are limited to using the latest Django version across all your projects.

Thus, one project cannot have a different Django version installed when you’re using a system-wide environment.

A better approach is to install Django in a virtual environment.

Installing Django in a Python virtual environment

Installing Django in a virtual environment allows you to separate system-wide applications and all your Django projects.

A virtual environment is an isolated environment that helps manage dependencies while isolating requirements for different Python projects.

Therefore, installing Django in a virtual environment prevents you from messing with the system libraries and dependencies.

Moreover, different Django projects on your Linux system can use different Django versions. Thus, a virtual environment gives you more freedom and control when installing Django for different projects.

So, how do you install Django in a virtual environment?

How to install Django in a virtual environment

Follows these steps to install Django in a virtual environment:

Step 1: Install or upgrade Python to the latest version

Open the Terminal and type the following to install the latest version of Python

sudo apt update && sudo apt install python3

sudo apt install python3-pip virtualenv

Confirm that you have  installed Python successfully by executing the following:

python3 –version

The command above should display the Python version installed on your system. That confirms that Python is installed on your Linux system. These commands work on Debian operating systems that use apt as the package manager, such as Ubuntu, Kali Linux, etc.

Find more information in this article: How to install Python in Kali Linux.

Step 2: Create a separate directory for your Python virtual environments

After installing Python, create a separate directory on your home page to store all your Python virtual environments.

You should define where you want to create a virtual environment.

In this case, we will create a virtual environment in the home {~/) folder of the Linux system.

To do that, open the Terminal and type the following:

mkdir ~/.virtualenvs

The command above creates a new directory called .virtualenvs in your home (~) directory.

You will use the directory to store different virtual environments for your Django projects.

Step 3: Create a new virtual environment using venv

After creating the designated directory, create a new virtual environment by typing and executing the following:

python -m venv ~/.virtualenvs/newDjangoEnv

The command above creates a new virtual environment called newDjangoEnv inside the ~/.virtualenvs directory.

If you navigate inside the folder and list the contents, you should see your newly created virtual environment folder.

cd ~/.virtualenvs


Step 4: Activate the newly created Python virtual environment

After creating the virtual environment, you need to activate it to install Django.

To do so, open the Terminal and execute the following command:

source ~/.virtualenvs/newDjangoEnv/bin/activate

The command above is used to activate the virtual environment that we created earlier.

Step 5: Install Django in your new virtual environment

After activating your newly created Python virtual environment, you may install Django by typing:

pip install django==4.0                   

The command above installs Django version 4.0 in the virtual environment that we activated. To specify a specific version of Django that you want to install, use the double equal sign (==) after the keyword django.

To install the latest Django version available, use the following command:

pip install django

You have successfully installed Django in a virtual environment.


If you want to create another Django project with another Django version, create another virtual environment in the same ~/.virtualenvs directory, activate it, and perform the installation.

To do that, open a new Terminal and execute the following:

python -m venv ~/.virtualenvs/DjangoEnv2 

You are free to name the virtual environment any descriptive name according to your project. Provided you will remember the virtual environment name, give it any name you want.

Activate the new environment

source ~/.virtualenvs/DjangoEnv2/bin/activate

Install Django while specifying the version

pip install django==3.0

You have successfully created an isolated directory to keep all your virtual environments, created a new virtual environment, and installed Django on your virtual environment.

After installing Django, start experimenting with new things, such as learning new concepts and installing helpful packages to use along with Django. Find more information on packages you should be using along with Django in this article: Packages and Libraries you should be using along with Django.

That’s it basically for this article. I hope to see you next time.

Share your love

Badi here, creator of— A website dedicated to providing helpful information and how-to's of web development and hosting. Inspired by a fascination to write the most efficient code to make a computer laugh, & humans, Steve has a passion for organizing characters to create code and informative content.
What makes me happy?
Well, nothing like the feeling of finally figuring out that one pesky bug that's been driving me crazy.

Leave a Reply

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