How to Install Django Step by Step

How to Install Django Step by Step

Through this article, we will help you to install Django web framework on Ubuntu 14.04. It’s very easy so you won’t need much time to start exploring Django and be a pro!

First, we have to tell you that there are many options installing Django. For instance, global install from packages, global install through pip, installing through pip in a Virtualenv, and development version install through git. Each of those methods have advantages and disadvantages therefore you should pick one that suits you best.

1. Global Install from Packages

This method might be the simplest one. You just need to update your local package index with ‘apt’ then install the ‘python-django’ package right away using this command:

sudo apt-get update

sudo apt-get install python-django

It’s done! (we are not joking, it’s really that straight forward and simple). If you want to check whether the installation was successful or not, simply type:

django-admin –version

1.6.1

Even though this method is very simple, but it is not as flexible as other methods. Moreover, global install from packages has an older version of Django than the official versions available.

2. Global Install through pip

Pip is the Python package manager and it’s the best method for those who want to install the newest version of Django globally. First and foremost, you have to install the ‘pip’ package manager by refreshing your ‘apt’ package index:

sudo apt-get update

Now you can install ‘pip’ in your computer in instant, but before you installing you should take an attention on what version of Python you want to use. If you want to use Python version 2, use this command:

sudo apt-get install python-pip

Or if you want to use Python version 3, use this command:

sudo apt-get install python3-pip

Next, you can install Django as you please! If you’re using Python version 2, type:

sudo pip install django

Or if you’re using Python version 3, add pip3 in the command:

sudo pip3 install django

Last but not least, you can check whether the installation is successful or not by typing:

django-admin –version

1.7.5

Using this method will provide you with the latest release of Django, but it less flexible.

3. Install through pip in a Virtualenv

Let’s begin by installing ‘pip’ through Ubuntu repositories and don’t forget to refresh your local package index using this command:

sudo apt-get update

Note that if you use Python 2, use this command to install ‘pip’:

sudo apt-get install python-pip

Or you can use this command if you use Python 3:

sudo apt-get install python3-pip

After you install ‘pip’, you can start installing ‘virtualenv’ package. Again, whether you use Python 2 or Python 3 is a matter. If you use Python 2 ‘pip’, you can type:

sudo pip install virtualenv

And if you have installed Python 3 ‘pip’, type this command:

sudo pip3 install virtualenv

Almost done! Next, you need to create a virtual isolated environment to install packages. Starting to create and move into a new project directory:

mkdir ~/newproject

cd ~/newproject

Then create a virtual environment by typing:

virtualenv (the name of your virtual environment)

After that, you should activate the virtual environment you just created earlier using this command:

source newenv/bin/active

The change on your prompt indicates that your virtual environment has activated and you can start to use ‘pip’ to install Django whether you use Python 2 or Python 3.

(the name of your virtual environment))[email protected]:~/newproject$

Once you’re in type this command to install Django:

pip install django

And verify the installation using this command:

django-admin –version

1.7.5

When you want to leave your virtual environment, use command ‘deactivate’, and if you want to re-active it again, type:

cd ~/newproject

source newenv/bin/active

We recommend you to use this method because it allows you to customize and install Django without affecting other systems, particularly the greater one. Although it’s not globally accessible, it is more flexible to use and apply.

4. Development Version Install through git

In this method, you need to install ‘git’ on your system with ‘apt’ beforehand. First of all, refresh your local package index using this command:

sudo apt-get update

Note that you need to use this command of you’re using Python 2:

sudo apt-get install git python-pip

If you’re using Python 3:

sudo apt-get install git python3-pip

Next, you need to clone your repository in order to get more up-to-date features and bug fixes. Use this command:

git clone git://github.com/django/django ~/django-dev

At this stage, you can install Django using pip. We are going to use the -e option to install Django. If you’re using Python 2, simply type:

sudo pip install -e ~/django-dev

Or if you’re using Python 3:

sudo pip3 install -e ~/django-dev

It’s done! In order to make sure whether the installation is successful, type:

django-admin  –version

and if this appears:

1.9.dev20150305171756

then it’s ready to be used.

Now, you have installed Django, but don’t lean on your chair yet, because we still need to do one more step to test if the installation is working.

First of all, we need to name a project and we will name it ‘project name’ as an example. Use this command:

django-admin startproject (projectname)

cd projectname

Use this command to bootstrap the database:

python manage.py migrate

or if you’re using an older version of Django, there’s an alternative:

python manage.py syncdb

If there is an obligation to create an administrative user, you can select a username, email address, and password. After that, you can see your Django project’s appearance by running:

python manage.py runserver 0.0.0.0:8000

Please note that command above is for development purposes only. After that, see your server’s IP address, followed by ‘:8000’, and the command is:

server_ip_address:8000

The appearance more or less will look like this:

django

Next, add /admin at the end of your URL in order to get to the login page:

server_ip_address:8000/admin

It will look like this:

django

If you include the admin username and password earlier, you will be able to straight up to the admin section of the site:

django

We are finished! So, what are you waiting for? Let’s learn Django right away!

Also, let us know if you need a further question or something you want to share by commenting below.

Source: Digital Ocean

%d bloggers like this: