How to install Django using virtualenv

Using virtualenv to install Django is recommended. This is because when you use virtualenv, you create an isolated environment with its own installation directories. This allows you to install a custom version of Python and its different packages which is not connected to the global installation on the server. This also solves the issue with permissions when installing software.

pip and virtualenv

pip and virtualenv are already installed on DreamHost servers by default. You'll use these to install Django. View the following links for further details, depending on which version of Python you're using:

Python 2

Python 3

Step 1 — Installing a custom version of Python for your virtualenv

If you're planning on using Django with a new virtual environment, chances are you want to use a specific version of Python. Review the following article (depending on your Python version) to install a custom version of Python and create a virtual environment using this custom version:

Step 2 — Create a virtual environment

View the following article (depending on your Python version) to create a virtual environment:

After you've created and activated a project in a virtual environment, you can then install Django within this isolated environment.

Step 3 — Setting up your Django site in virtualenv

To run the commands in this article, you must log into your server via SSH with your Shell user. View the following articles for more information:

Install a custom version of Django and any other required packages (e.g., mysqlclient if you're going to use a MySQL database) for your website using pip:

Using Python 2

(my_project) [server]$ pip install Django
(my_project) [server]$ pip install mysqlclient

Using Python 3

If you're installing in a Python3 environment, use the following instead:

(my_project) [server]$ pip3 install Django
(my_project) [server]$ pip3 install mysqlclient

Installing version 2+ of Django

If you want to install Django version 2.0.3, you must be running Python 3. If you attempt to install in a Python 2 environment, you'll see the following errors:

 Could not find a version that satisfies the requirement Django==2.0.3 
 (from versions: 1.1.3, 1.1.4, 1.2, 1.2.1, 1.2.2, 1.2.3, 1.2.4, 1.2.5, 1.2.6, 1.2.7, 1.3, 1.3.1, 
 1.3.2, 1.3.3, 1.3.4, 1.3.5, 1.3.6, 1.3.7, 1.4, 1.4.1, 1.4.2, 1.4.3, 1.4.4, 1.4.5, 1.4.6, 1.4.7, 1.4.8, 
 1.4.9, 1.4.10, 1.4.11, 1.4.12, 1.4.13, 1.4.14, 1.4.15, 1.4.16, 1.4.17, 1.4.18, 1.4.19, 1.4.20, 
 1.4.21, 1.4.22, 1.5, 1.5.1, 1.5.2, 1.5.3, 1.5.4, 1.5.5, 1.5.6, 1.5.7, 1.5.8, 1.5.9, 1.5.10, 1.5.11, 
 1.5.12, 1.6, 1.6.1, 1.6.2, 1.6.3, 1.6.4, 1.6.5, 1.6.6, 1.6.7, 1.6.8, 1.6.9, 1.6.10, 1.6.11, 1.7, 
 1.7.1, 1.7.2, 1.7.3, 1.7.4, 1.7.5, 1.7.6, 1.7.7, 1.7.8, 1.7.9, 1.7.10, 1.7.11, 1.8a1, 1.8b1, 1.8b2, 
 1.8rc1, 1.8, 1.8.1, 1.8.2, 1.8.3, 1.8.4, 1.8.5, 1.8.6, 1.8.7, 1.8.8, 1.8.9, 1.8.10, 1.8.11, 1.8.12, 
 1.8.13, 1.8.14, 1.8.15, 1.8.16, 1.8.17, 1.8.18, 1.8.19, 1.9a1, 1.9b1, 1.9rc1, 1.9rc2, 1.9, 1.9.1, 
 1.9.2, 1.9.3, 1.9.4, 1.9.5, 1.9.6, 1.9.7, 1.9.8, 1.9.9, 1.9.10, 1.9.11, 1.9.12, 1.9.13, 1.10a1, 
 1.10b1, 1.10rc1, 1.10, 1.10.1, 1.10.2, 1.10.3, 1.10.4, 1.10.5, 1.10.6, 1.10.7, 1.10.8, 1.11a1, 
 1.11b1, 1.11rc1, 1.11, 1.11.1, 1.11.2, 1.11.3, 1.11.4, 1.11.5, 1.11.6, 1.11.7, 1.11.8, 1.11.9, 
 1.11.10, 1.11.11)  
 No matching distribution found for Django==2.0.3

/home/username/projectname/lib/python2.7/site-packages/pip/_vendor/urllib3/util/ssl_.py:339: SNIMissingWarning: 
An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform.
This may cause the server to present an incorrect TLS certificate, which can cause validation failures.
You can upgrade to a newer version of Python to solve this. For more information,
see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings SNIMissingWarning /home/username/projectname/lib/python2.7/site-packages/pip/_vendor/urllib3/util/ssl_.py:137:
InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately
and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this.
For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecurePlatformWarning

View the following article to view which versions of Python are compatible with specific versions of Django:

View the following for instructions on how to create a Django 2 project using Python3:

Viewing the version of Django

Run the following command to view the version of Django you installed.

(my_project) [server]$ python -c "import django; print(django.get_version())"

Troubleshooting

When installing Django or mysqlclient, it's possible you may see the following error message:

Command "python setup.py egg_info" failed with error code 1

If you see this, try upgrading your version of pip within your virtual environment. For example:

(my_project) [server]$ python -m pip install --upgrade pip

This should fix your installation error.

See also

Did this article answer your questions?

Article last updated PST.