{"id":4639,"date":"2018-06-04T17:50:08","date_gmt":"2018-06-04T13:50:08","guid":{"rendered":"https:\/\/www.howtoforge.com\/tutorial\/how-to-install-django-on-ubuntu\/"},"modified":"2018-06-04T17:50:08","modified_gmt":"2018-06-04T13:50:08","slug":"how-to-install-django-on-ubuntu-18-04-lts","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/how-to-install-django-on-ubuntu-18-04-lts\/","title":{"rendered":"How to Install Django on Ubuntu 18.04 LTS"},"content":{"rendered":"<p>Django is a web application framework written in python that follows the MVC (Model-View-Controller) architecture, it is available for free and released under an open source license. It is fast and designed to help developers get their application online as quickly as possible. Django helps developers to avoid many common security mistakes like SQL Injection, XSS, CSRF and clickjacking.<\/p>\n<p>Django is maintained by the Django Software Foundation and used by many big technology companies, government, and other organizations. Some large websites like Pinterest, Mozilla, Instagram, Discuss, The Washington Post etc. are developed with Django.<\/p>\n<p>In this tutorial, we will install Django 2.0.5 stable version on an Ubuntu 18.04 LTS (Bionic Beaver) server. Django can be installed on a server in many ways, in this tutorial, I will show you 3 different ways to install Django:<\/p>\n<ol>\n<li>Django installation with pip.<\/li>\n<li>Install Django with virtualenv.<\/li>\n<li>Install Django from it&#8217;s github repository.<\/li>\n<\/ol>\n<p>When the Django installation is done, I will show you the first steps to start a new project with the Django web framework.<\/p>\n<h2 id=\"prerequisites\">Prerequisites<\/h2>\n<ul>\n<li>Ubuntu 18.04 &#8211; 64bit.<\/li>\n<li>Root privileges.<\/li>\n<\/ul>\n<h2 id=\"step-setup-python-on-ubuntu-lts\">Step 1 &#8211; Setup Python 3 on Ubuntu 18.04 LTS<\/h2>\n<p>We will configure python 3 before we start with the Django installation.<\/p>\n<p>On my Ubuntu machine, there are two versions of python available, python2.7 as default python version and python3.<\/p>\n<p>In this step, we will change the default python version to python 3.<\/p>\n<p>Check the python version:<\/p>\n<p class=\"command\">python &#8211;version<\/p>\n<p>So the default python is 2.7 at the moment.<\/p>\n<p>Next, change the default python to python version 3 with the &#8216;update-alternatives&#8217; command:<\/p>\n<p class=\"command\">update-alternatives &#8211;install \/usr\/bin\/python python \/usr\/bin\/python3 1<\/p>\n<p>Now check again the python version:<\/p>\n<p class=\"command\">python &#8211;version<\/p>\n<p>And you will get python 3.6 as a default python on the system.<\/p>\n<p><a class=\"fancybox\" id=\"img-1\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_django_on_ubuntu_1804\/big\/1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-django-on-ubuntu-18-04-lts.png\" alt=\"Setup Python 3 on Ubuntu\" width=\"500\" height=\"147\" title=\"\"><\/a><\/p>\n<p><strong>Note:<\/strong><\/p>\n<p>By default, ubuntu 18.04 has no &#8216;python&#8217; command, because it brings the &#8216;python3&#8217; command as default python.<\/p>\n<p>See the BionicBeaver <a href=\"https:\/\/wiki.ubuntu.com\/BionicBeaver\/ReleaseNotes#Other_base_system_changes_since_16.04_LTS\" target=\"_blank\" rel=\"noopener\">ReleaseNotes<\/a>.<\/p>\n<h2 id=\"step-install-django\">Step 2 &#8211; Install Django<\/h2>\n<p>In this step, I will show you 3 ways to install Django. Please follow either chapter 2.1, 2.2 or 2.3 to install Django but not all 3 options at the same time :).<\/p>\n<h3 id=\"-install-django-with-pip\">2.1. Install Django with Pip<\/h3>\n<p>Pip is a package management system for python. Python has a central package repository from which we can download the python package. It&#8217;s called Python Package Index (PyPI).<\/p>\n<p>In order to install Django with pip, we need to install &#8216;python3-pip&#8217; package to the ubuntu 18.04 system.<\/p>\n<p>Run command below to install pip for python 3.<\/p>\n<p class=\"command\">sudo apt install python3-pip -y<\/p>\n<p>The installation will add a new binary file called &#8216;pip3&#8217;. To make it easier to use pip, I will create a symlink for pip3 to pip:<\/p>\n<p class=\"command\">which pip3<br \/>ln -s \/usr\/bin\/pip3 \/usr\/bin\/pip<\/p>\n<p>Now check the version :<\/p>\n<p class=\"command\">pip &#8211;version<\/p>\n<p><a class=\"fancybox\" id=\"img-2\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_django_on_ubuntu_1804\/big\/2.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-django-on-ubuntu-18-04-lts-2.png\" alt=\"Install Django with pip\" width=\"500\" height=\"162\" title=\"\"><\/a><\/p>\n<p>The pip installation is done. Now we can use the pip command to install python packages.<\/p>\n<p>Let&#8217;s install Django 2.0.5 stable version to the server with pip command below:<\/p>\n<p class=\"command\">pip install Django==2.0.5<\/p>\n<p><strong>Note:<\/strong><\/p>\n<p>We set the Django==2.0.5 to get a specific version. If you want a different version, just change the number e.g. to django==1.10 etc.<\/p>\n<p><a class=\"fancybox\" id=\"img-3\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_django_on_ubuntu_1804\/big\/3.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-django-on-ubuntu-18-04-lts-4.png\" alt=\"Install specific Django version\" width=\"500\" height=\"172\" title=\"\"><\/a><\/p>\n<p>If you get an error about locale settings, run the following command to reconfigure the locale settings:<\/p>\n<p class=\"command\">export LANGUAGE=en_US.UTF-8<br \/>export LANG=en_US.UTF-8<br \/>export LC_ALL=en_US.UTF-8<br \/>locale-gen en_US.UTF-8<br \/>dpkg-reconfigure locales<\/p>\n<p>After the installation is complete, check the Django version with the command below:<\/p>\n<p class=\"command\">django-admin &#8211;version<\/p>\n<p>Alternatively, we can use the following command:<\/p>\n<p class=\"command\">python<br \/>import django<br \/>print(django.get_version())<\/p>\n<p>You will see that Django 2.0.5 stable has been installed on the system.<\/p>\n<p><a class=\"fancybox\" id=\"img-4\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_django_on_ubuntu_1804\/big\/4.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-django-on-ubuntu-18-04-lts-6.png\" alt=\"Django 2.0.5 stable has been installed\" width=\"500\" height=\"241\" title=\"\"><\/a><\/p>\n<p>Django 2.0.5 has been installed on the system with pip. Proceed with chapter 3.<\/p>\n<h3 id=\"-install-django-with-virtualenv\">2.2. Install Django with Virtualenv<\/h3>\n<p>Virtualenv is a python environment builder. It is used to create isolated python environments. We can choose the version of python that will be installed in the virtualenv environment. This is very useful for developers, they can run and develop an application with different python versions and different environments on one OS.<\/p>\n<p>Virtualenv is available on PyPI repository &#8211; we can install it with the pip command:<\/p>\n<p class=\"command\">pip install virtualenv<\/p>\n<p>Now we can use the virtualenv command to create a new environment with python3 as default python version. So let&#8217;s create a new environment &#8220;env01&#8221; with python3 as the python version and pip3 for the django installation.<\/p>\n<p class=\"command\">virtualenv &#8211;python=python3 env01<\/p>\n<p>The command will create a new directory called &#8216;env01&#8217; which contains the directories bin, include and lib for pyhton.<\/p>\n<p>The virtualenv has been created, now let&#8217;s log into the new environment with the following command:<\/p>\n<p class=\"command\">cd env01\/<br \/>source bin\/activate<\/p>\n<p>Next, install Django in the the virtual environment that we&#8217;ve created:<\/p>\n<p class=\"command\">pip install django==2.0.5<\/p>\n<p>When the installation finished, check the Django installation:<\/p>\n<p class=\"command\">django-admin &#8211;version<\/p>\n<p><a class=\"fancybox\" id=\"img-5\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_django_on_ubuntu_1804\/big\/5.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-django-on-ubuntu-18-04-lts-8.png\" alt=\"Install Django with Virtualenv\" width=\"500\" height=\"223\" title=\"\"><\/a><\/p>\n<p>Django 2.0.5 has been successfully installed in our virtual environment. Proceed with chapter 3.<\/p>\n<h3 id=\"-install-django-from-git-repository\">2.3. Install Django from Git Repository<\/h3>\n<p>In order to install Django from the Git Repository, we need to install git to the system.<\/p>\n<p>Install git using apt command below.<\/p>\n<p class=\"command\">sudo apt install git -y<\/p>\n<p>Next, create new virtual environment named &#8216;git01&#8217; using the virtualenv command.<\/p>\n<p class=\"command\">virtualenv &#8211;python=python git01<\/p>\n<p>Activate the &#8216;git01&#8217; virtual environment.<\/p>\n<p class=\"command\">cd git01\/<br \/>source bin\/activate<\/p>\n<p>Next, clone the django source code from github using git.<\/p>\n<p class=\"command\">git clone git:\/\/github.com\/django\/django django-dev<\/p>\n<p>Install django development version using pip command as shown below.<\/p>\n<p class=\"command\">pip install -e django-dev<\/p>\n<p><strong>Note:<\/strong><\/p>\n<p>-e =\u00a0 Install a package in editable mode or a local package. In this chapter, we install Django from the local code on the &#8216;django-dev&#8217; that we&#8217;ve cloned.<\/p>\n<p>When the installation process is done, let&#8217;s check the Django version on the server:<\/p>\n<p class=\"command\">django-admin &#8211;version<\/p>\n<p>You will get the django development version installed from the Github Repository.<\/p>\n<p><a class=\"fancybox\" id=\"img-6\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_django_on_ubuntu_1804\/big\/6.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-django-on-ubuntu-18-04-lts-10.png\" alt=\"Install Django from Git Repository\" width=\"500\" height=\"260\" title=\"\"><\/a><\/p>\n<h2 id=\"step-create-you-first-project-with-django\">Step 3 &#8211; Create you First Project with Django<\/h2>\n<p>In this step, we will install Django inside a virtual environment and then start our first project with Django. Make sure the &#8216;virtualenv&#8217; packages is installed on the system.<\/p>\n<p>Create new virtual environment named &#8216;django01&#8217;.<\/p>\n<p class=\"command\">virtualenv &#8211;python=python django01<\/p>\n<p>Go to the &#8216;django01&#8217; directory and activate the virtual environment.<\/p>\n<p class=\"command\">cd django01\/<br \/>source bin\/activate<\/p>\n<p>Now install Django 2.0.5 stable version using pip.<\/p>\n<p class=\"command\">pip install Django==2.0.5<\/p>\n<p><a class=\"fancybox\" id=\"img-7\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_django_on_ubuntu_1804\/big\/7.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-django-on-ubuntu-18-04-lts-12.png\" alt=\"Create you First Project with Django\" width=\"500\" height=\"284\" title=\"\"><\/a><\/p>\n<p>When the installation is complete, create a new project called &#8216;project01&#8217; with the django-admin command:<\/p>\n<p class=\"command\">django-admin startproject project01<\/p>\n<p>The command will create a new directory &#8216;project01&#8217; that contains Django files:<\/p>\n<p class=\"command\">cd project01\/<br \/>tree<\/p>\n<p><a class=\"fancybox\" id=\"img-8\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_django_on_ubuntu_1804\/big\/8.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-django-on-ubuntu-18-04-lts-14.png\" alt=\"django-admin\" width=\"500\" height=\"238\" title=\"\"><\/a><\/p>\n<p>Now edit the &#8216;settings.py&#8217; under the &#8216;project01&#8217; directory using vim command.<\/p>\n<p class=\"command\">vim project01\/settings.py<\/p>\n<p>Type the server IP address inside the &#8216;ALLOWED_HOSTS&#8217; line as shown below.<\/p>\n<pre>ALLOWED_HOSTS = ['192.168.10.100']<\/pre>\n<p>Save and exit.<\/p>\n<p>Now run the python django runserver command.<\/p>\n<p class=\"command\">python manage.py runserver 192.168.10.100:8000<\/p>\n<p>The command will run python django on the IP address &#8216;192.168.10.100&#8217;, on port &#8216;8000&#8217;.<\/p>\n<p><a class=\"fancybox\" id=\"img-9\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_django_on_ubuntu_1804\/big\/9.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-django-on-ubuntu-18-04-lts-16.png\" alt=\"Django project started\" width=\"500\" height=\"162\" title=\"\"><\/a><\/p>\n<p>Open your web browser and type the server IP address with port 8000.<\/p>\n<p><a href=\"http:\/\/192.168.10.100:8000\/\">http:\/\/192.168.10.100:8000\/<\/a><\/p>\n<p>And you will get the beautiful default django page as below.<\/p>\n<p><a class=\"fancybox\" id=\"img-10\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_django_on_ubuntu_1804\/big\/10.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-django-on-ubuntu-18-04-lts-18.png\" alt=\"Django project in Browser\" width=\"500\" height=\"288\" title=\"\"><\/a><\/p>\n<p><strong>Note:<\/strong><\/p>\n<p>Press &#8216;Ctrl+c&#8217; to exit from the django runserver.<\/p>\n<p>Next, we will configure the Django admin. Django will automatically generate the database for a superuser. Before we create the superuser, run the command below:<\/p>\n<p class=\"command\">python manage.py migrate<\/p>\n<p><strong>migrate<\/strong>: make adds the models (adding fields, deleting etc.) into the database scheme, the default database is sqlite3.<\/p>\n<p><a class=\"fancybox\" id=\"img-11\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_django_on_ubuntu_1804\/big\/11.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-django-on-ubuntu-18-04-lts-20.png\" alt=\"Python Migrate\" width=\"500\" height=\"327\" title=\"\"><\/a><\/p>\n<p>Now create the admin\/superuser:<\/p>\n<p class=\"command\">python manage.py createsuperuser<\/p>\n<p>Type your django admin user, email, and password.<\/p>\n<pre>Username (leave blank to use 'root'): hakase&#13;\nEmail address: <a href=\"https:\/\/www.howtoforge.com\/cdn-cgi\/l\/email-protection\" class=\"__cf_email__\" data-cfemail=\"d7b6b3babeb997bfb6bcb6a4b2fabbb6b5a4f9beb8\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a>&#13;\nPassword:&#13;\nPassword (again):&#13;\nSuperuser created successfully.<\/pre>\n<p><a class=\"fancybox\" id=\"img-12\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_django_on_ubuntu_1804\/big\/12.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-django-on-ubuntu-18-04-lts-23.png\" alt=\"Add Django admin user\" width=\"500\" height=\"131\" title=\"\"><\/a><\/p>\n<p>The Django superuser has been created, now you can execute the runserver command, then go to the browser and visit the django admin page:<\/p>\n<p class=\"command\">python manage.py runserver 192.168.10.100:8000<\/p>\n<p>Visit Django admin page: <a href=\"http:\/\/192.168.10.100:8000\/admin\/\">http:\/\/192.168.10.100:8000\/admin\/<\/a><\/p>\n<p><a class=\"fancybox\" id=\"img-13\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_django_on_ubuntu_1804\/big\/13.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-django-on-ubuntu-18-04-lts-25.png\" alt=\"Django admin login\" width=\"500\" height=\"207\" title=\"\"><\/a><\/p>\n<p>Login with username hakase and your password, you will see the admin page:<\/p>\n<p><a class=\"fancybox\" id=\"img-14\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_django_on_ubuntu_1804\/big\/14.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-django-on-ubuntu-18-04-lts-26.png\" alt=\"Django administration dashboard\" width=\"500\" height=\"159\" title=\"\"><\/a><\/p>\n<p>Django has been successfully installed inside a virtual environment and we&#8217;ve created a sample Django project named &#8216;project01&#8217; inside the &#8216;django01&#8217; directory.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Django is a web framework based on the Python programming language, it is released as free software under an open source license and maintained by the Django Software Foundation. Django is very fast and allows it to build web applications rapidly. Django is a web framework that uses the MVC (Model-View-Controller) paradigm. We can install Django on a server with the pip command, in a virtual environment with virtualenv, and directly from the Django git repository.<\/p>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Fhow-to-install-django-on-ubuntu%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-django-on-ubuntu-18-04-lts-27.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Fhow-to-install-django-on-ubuntu%2F&amp;text=How+to+Install+Django+on+Ubuntu+18.04+LTS&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-django-on-ubuntu-18-04-lts-28.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-django-on-ubuntu-18-04-lts-29.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Fhow-to-install-django-on-ubuntu%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-django-on-ubuntu-18-04-lts-30.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Django is a web application framework written in python that follows the MVC (Model-View-Controller) architecture, it is available for free and released under an open source license. It is fast and designed to help developers get their application online as quickly as possible. Django helps developers to avoid many common security mistakes like SQL Injection, &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-4639","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/4639","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=4639"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/4639\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=4639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=4639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=4639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}