How to setup a Django Development Environment on Mac from scratch.

Xcode, Homebrew, Git, Virtualenv, Python3, PostgreSQL, Redis.

Rio Weber
riow

--

Step 1: Install Xcode

Just follow this link: itunes.apple.com/us/app/xcode

1. Open Xcode

2. Agree to the conditions.

3. Download all options.

Download and install Command Line Tools

4. Open Terminal.

5. Execute the following command.

xcode-select --install

6. Click Install.

7. Click Agree.

8. Click Done.

Install Homebrew

Homebrew allows you to easily install hundreds of open-source tools.

9. Go to the Homebrew website.

10. Execute the top command under “Install Homebrew” in Terminal.

11. Press “Return”

12. Enter Password.

13. Check the install.

brew doctor

14. Update brew.

brew update

Install Git

15. Execute the following command in Terminal.

brew install git

16. Verify the install.

git --version

Make sure the version is 2.13.0or later.

17. Rerun brew doctor to make sure everything is working.

brew doctor

Install Virtualenv

Virtualenv is a tool to create isolated Python environments.

18. Install virtualenv to manage different versions of Python.

brew update
brew install pyenv
brew install pyenv-virtualenv

Check pyenv install: pyenv -v should be greater than 1.0.10

19. Enable auto-activation of virtualenvs on your profile by adding the following lines to your shell profile “.bash_profile” file.

/Users/rio/.bash_profile

20. To do this, open “.bash_profile” with nano.

nano ~.bash_profile

21. Past in the following line.

eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

> control+X, Y, Enter.

22. Restart the shell to enable pyenv-virtualenv

exec "$SHELL"

Install Python 3

23. Use Pyenv (from Virtualenv) to install Python 3.

pyenv install 3.6.1

24. Specify what version you want to use in your environment.

pyenv local 3.6.1

To do it globaly: pyenv global 3.6.1.

25. Set the Locale (More on locale).

A locale consists of a number of categories for which country-dependent formatting or other specifications exist.

26. To do this, add the following lines to “.bash_profile” with nano.

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

*MANY SECTIONS THAT FOLLOW ARE OPTIONAL*

Sections “Create the Python Virtual Environment” and some of “Setup PostgreSQL” are relevant to all Django environments.

Create the Python Virtual Environment

Using python3, create the environment you will be working in. This one will be named “ENV_NAME” while specifying the version you want to use.

30. Create virtualenv

pyenv virtualenv 3.4.5 ENV_NAME

To delete an existing virtualenv: pyenv uninstall ENV_NAME

31. Execute:

eval “$(pyenv init -)”
eval “$(pyenv virtualenv-init -)”

32. Activate your new environment.

pyenv activate ENV_NAME

You should now be working on your specified pyenv. Your (ENV_NAME) should be in parenthesis.

Almost done…

Only thing left is to clone the project.

NOTE: If you haven’t added yourself to the project and made/added your SSH key to your profile online now is the time to do that.
For a project through GitHub follow this tutorial starting at “Generating a new SSH key.”
https://medium.com/riow/deploy-to-production-server-with-git-using-php-ab69b13f78ad#6bc1

For a project through GitLab look here:
https://docs.gitlab.com/ee/gitlab-basics/create-your-ssh-keys.html

Clone EVERYTHING!

Make Dev Folder

mkdir Repo_Name

Check Git Info

git config --global --list

Create SSH Key.

ssh-keygen -t rsa -C "your.email@example.com" -b 4096

Get SSH Public Key.

cat ~/.ssh/id_rsa.pub
pbcopy < ~/.ssh/id_rsa.pub

Paste key into repo host.

Add Remote Origin.

git remote add origin git@github.com:USERNAME/REPOSITORY.git

Clone It!

git clone git@github.com:USERNAME/REPOSITORY.git

For Basic Git Commands:
https://docs.gitlab.com/ee/gitlab-basics/start-using-git.html

Populate and migrate the database:

sudo python ./src/manage.py syncdb --migrate

Make sure your in the repo.

pwd
/Users/rio/Repo_Name/Repo

Run thedevelopment server

python manage.py runserver

Install Python packages.

34. Execute:

sudo -H pip3 install gitsome
easy_install -U setuptools
brew install postgresql
sudo -H pip3 install psycopg2==2.6.2

Setup PostgreSQL

Postgres is an object-relational database management system that stores and returns data securely in response to requests from other software applications.

35. In a new terminal window run PostgreSQL:

postgres -D /usr/local/var/postgres

Note: For system-wide (start at login): brew services start postgresql.
-D
is to the switch to pass the location of the server configuration file.

36. In original terminal window:

$ createdb `DATABASE_NAME`

Install Redis

Redis is an in-memory database that persists on disk. It can be used for Caching in Django.

37. Install Redis.

brew install redis

You can execute brew services start redis to launch Redis and enable it to start at login.
BUT, we just want a background service, so…
38. In a New terminal window execute:

redis-server /usr/local/etc/redis.conf

You should now have 3 total terminal windows open.

./END

--

--