Depending on your needs, there are different ways to install Django. It can be mounted or installed using pip Python virtual environment system-wide.
The main purpose of Python virtual environment is to create a separate environment for different Python project. In this way, you can have on a number of different computer Django environment, and specific versions of modules installed on a per project basis, without having to fear that it will affect the rest of your Django installation. If you install Django to the global environment, you can install only one version of Django on your computer.
Django installation on Ubuntu 18.04
The following sections provide step by step instructions on how to install Django Python virtual environment on Ubuntu 18.04's.
1, mounted and Python 3 venv
By default, Ubuntu 18.04 comes with Python 3.6. You can verify this by typing the following on whether the system is installed Python 3:
The output should be as follows:
Python 3.6.9
Starting Python 3.6, creating a virtual environment recommended method is to use venv module. To install modules provide venv python3-venv package, run the following command:
As shown below:
Once the module is installed, we can think Django application creates a virtual environment.
2, create a virtual environment
First, go to the directory you want to store Python 3 virtual environment. It could be any other directory of your home directory or your users have read-write access.
Create a new directory for the Django application and navigate to it:
After entering the directory, run the following command to create a new virtual environment:
The above command creates a directory named venv of which contains a copy of Python binaries, Pip package manager, Python standard libraries, and other support files. You can use any name for the virtual environment.
To start using this virtual environment, you need to activate to activate it by running the script:
When activated, bin directory virtual environment will be added to the beginning of the $ PATH variable. In addition, your shell prompt will change, it will display the name of the virtual environment you are currently using. In our case venv:
3, install Django
Now the virtual environment is activated, you can use the Python Package Manager pip to install Django:
In a virtual environment, you can use the command pip instead pip3 and python instead python3.
To verify the installation, use the following command to print version of Django:
As of this writing (December 6, 2019), the latest official Django version 3.0
Your Django version may differ from the version shown here.
4. Create a Django project
To create a new django project called mydjangoapp, use the django-admin command-line utility:
1 directory, 6 files
In this directory, you will find the main script and another directory named manage.py for managing the project, including database configuration, settings and django-specific applications.
Let us migrate the database and create an administrative user.
Run the following command to migrate the database:
The output is shown below:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying sessions.0001_initial... OK
After migrating the database, create an administrative user so that you can use the Django admin interface:
This command will prompt you to enter an administrative user's user name, email address and password.
5, test development server
Use manage.py runserver option to start the script followed by the development of the Web server:
You will see the same output as the following figures:
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
If you have installed on the virtual machine and want to access Django Django development server, you need to edit the settings.py file, add the server IP address in ALLOWED_HOSTS list.
Http://127.0.0.1:8000 open in a Web browser, you'll see the default Django login page:
You can add at the end of the URL / admin / to access the Django admin interface (http://127.0.0.1:8000/admin/). This will take you to administrator login screen:
Enter your user name and password, you will be redirected to the Django admin site:
To stop the development server, type CTRL-C in the terminal.
6, disable the virtual environment
After the completion of the work, by typing deactivate to deactivate environment, you will return to the normal shell.
deactivate
to sum up
You've learned how to create a virtual Python environment on your computer and install Ubuntu 18.04 Django. To create additional Django development environment, repeat the steps outlined in this tutorial.
If you are new to Django, visit the Django documentation page, learn how to develop your first Django application.
If you have any questions, please feel free to comment.