Creating the Development Environment for A Django Project on Ubuntu

Setting Up a Django Development Environment on Ubuntu: A Step-by-Step Guide
August 11, 2024 by
Creating the Development Environment for A Django Project on Ubuntu
Hamed Mohammadi
| No comments yet

Introduction

Django, a high-level Python web framework, offers rapid development and clean design. To effectively work with Django, setting up a dedicated development environment is crucial. This post will guide you through creating a virtual environment using venv and installing necessary packages with apt and pip on Ubuntu.



Step 1: Installing Python 3 and Essential Packages on Ubuntu

We use pip to add python packages for our Django project, but we need core Python packages for development install on our Ubuntu system. This is usually done by default, but if not it is pretty simple. Here's the apt command to install Python 3, venv, and other commonly used development packages.

It's always a good practice to update your package lists before installing new software:

$ sudo apt update

And then:

$ sudo apt install python3 python3-venv python3-pip build-essential


Breakdown of the packages:

  • python3: Installs the Python 3 interpreter.

  • python3-venv: Provides the venv module for creating virtual environments.

  • python3-pip: Installs pip, the Python package installer.

  • build-essential: Essential development tools for building software from source.



Step 2: Create a Virtual Environment

A virtual environment isolates project dependencies. Create a new directory for your project and navigate to it:

$ mkdir myproject 
$ cd myproject 

Then, create a virtual environment:

$ python3 -m venv .venv

Activate the virtual environment:

$ source .venv/bin/activate

Your terminal prompt should now indicate that you're in the virtual environment.


Step 3: Install Django

Install Django using pip:

(.venv)$ pip install django

This will install Django and all other dependencies.

Step 4: Create a Django Project

Create a new Django project:

(.venv)$ django-admin startproject myproject .

The dot(.) at the end of the command is important. Because you are already in your Django project folder and don’t want Django create another folder inside your current folder and put Django's files there.


Step 5: Run the Development Server

If you are not already there, navigate to the project directory/ Start the development server:

(.venv)$ python manage.py runserver

You should now be able to access your Django project at http://127.0.0.1:8000/.


Additional Tips

  • Keep your virtual environments organized: Create separate virtual environments for different projects to avoid dependency conflicts.

  • Use requirements.txt: List project dependencies in a requirements.txt file for easy installation in other environments. You can use pip freeze > requirements.txt to record the required packages.

  • Consider using a package manager like poetry: While venv is sufficient for basic projects, poetry offers additional features like dependency management and environment isolation.


Conclusion

Your development environment is ready for Django development. You can add your apps, write models, views, urls, and template. You've successfully set up a Django development environment on Ubuntu. This foundation allows you to start building your web applications without affecting the main desktop environment. Remember to explore Django's rich features and leverage the power of virtual environments to manage your projects effectively.


Creating the Development Environment for A Django Project on Ubuntu
Hamed Mohammadi August 11, 2024
Share this post
Archive

Please visit our blog at:

https://zehabsd.com/blog

A platform for Flash Stories:

https://readflashy.com

A platform for Persian Literature Lovers:

https://sarayesokhan.com

Sign in to leave a comment