Make sure you have Python 3 installed: Python Download | python.org

Short Version

Using venv module

1
2
3
4
5
6
7
8
cd ~/MyProjects
mkdir venvs
python3 -m venv venvs/myproject02
source venvs/myproject02/bin/activate
pip install django
django-admin startproject myproject02
cd myproject02
./manage.py runserver

Using Virtualenvwrapper

1
2
3
4
5
6
7
8
9
10
pip install virtualenvwrapper
export WORKON_HOME=$HOME/.venvs
export PROJECT_HOME=$HOME/MyProjects
source /usr/local/bin/virtualenvwrapper.sh
mkvirtualenv --python=/usr/local/bin/python3 myproject03
pip install django
cd ~/MyProjects
django-admin startproject myproject03
cd myproject03
./manage.py runserver

Now you can create new virtual environments and Django projects like this:

1
2
3
mkvirtualenv --python=/usr/local/bin/python3 myproject04
cd ~/MyProjects
django-admin startproject myproject04

Deactivate virtual environment:

1
deactivate

Activate specific virtual environment:

1
workon myproject04

Long Version

Virtual Environments

With Virtual Environments you can manage projects and related packages separately without interfering with each other. For example you might have a virtual environment for project A that uses Python 2.7 and Django 1.9, and another virtual environment for project B that uses Python 3.6 and Django 1.10. If you update project B to Django 1.11, that doesn’t affect the Django version in project A.

Using venv module

venv module creates virtual environments. It was introduced in Python 3.3. Read more: venv | python.org

Create a virtual environment and activate it:

1
2
3
4
cd ~/MyProjects
mkdir venvs
python3 -m venv venvs/myproject02
source venvs/myproject02/bin/activate

You don’t have to create virtual environments in any particular location. In this case I created a folder venvs in the ~/MyProjects folder. You can also put the virtual environment folder inside the project folder and use .gitignore to ignore it from the version control.

You can see (myproject02) in the terminal to indicate that the virtual environment is active.

Install Django:

1
pip install django

Because the virtual environment is now active, Django package will be installed in ~/MyProjects/venvs/myproject02/lib/python3.6/site-packages/django.

Create a Django project:

1
django-admin startproject myproject02

Now you should have this kind of folder structure:

1
2
3
4
5
6
7
8
MyProjects
├── myproject02
│   ├── manage.py
│   └── myproject02
│       ├── __init__.py
│       ├── settings.py
│       ├── urls.py
│       └── wsgi.py

Run the server:

1
2
cd myproject02
./manage.py runserver

Access site in http://127.0.0.1:8000/

You can deactivate the virtual environment like this:

1
deactivate

Using Virtualenvwrapper

Install virtualenvwrapper:

1
pip install virtualenvwrapper

virtualenvwrapper makes it easier to manage virtual environments. It’s an extension to the virtualenv tool.

Edit your bash startup file (like .bashrc or .zshrc) and put these lines in it:

1
2
3
export WORKON_HOME=$HOME/.venvs
export PROJECT_HOME=$HOME/MyProjects
source /usr/local/bin/virtualenvwrapper.sh
WORKON_HOME The directory for all virtual environments. ~/.venvs in this case.
PROJECT_HOME The directory where all projects will be stored. ~/MyProjects in this case.
source .. virtualenvwrapper.sh Runs the script installed by the virtualenvwrapper project.

source the startup file for these to take effect:

1
source ~/.bashrc

source read and executes commands from the given file argument. Gnu Bourne Shell Builtins | gnu.org

Run mkvirtualenv:

You can use this command to identify python3 location: which python3

1
mkvirtualenv --python=/usr/local/bin/python3 myproject03

This creates a virtual environment in ~/.venvs/myproject03 and activates it.

You should see (myproject03) in the bash to signal that this particular virtual environment is activated.

Install Django:

1
pip install django

Create a Django project:

1
2
3
cd ~/MyProjects
django-admin startproject myproject03
cd myproject03

Now you should have this kind of folder structure:

1
2
3
4
5
6
7
8
MyProjects
├── myproject03
│   ├── manage.py
│   └── myproject03
│       ├── __init__.py
│       ├── settings.py
│       ├── urls.py
│       └── wsgi.py

Run development server:

1
./manage.py runserver

Access site in http://127.0.0.1:8000/

Virtualenvwrapper instructions

After you have added the lines above to your startup script (.bashrc), you can use these commands to add new virtual environments and projects:

1
2
3
mkvirtualenv --python=/usr/local/bin/python3 myproject04
cd ~/MyProjects
django-admin startproject myproject04

Use deactivate to deactivate the virtual environment:

1
deactivate

Use workon to switch to a specific virtual environment:

1
workon myproject04