Back in 2015, I wrote a very similar article: Setup Django on Mac. The main difference is that I'm now using Pyenv to have projects using Python 2.7, 3.5 & 3.8. The other change that happened since 2015 is that MacOS can now change colours depending on the time of the day so you don't really need F.lux anymore.
What we need
To run a Django project, all you technically need is: Python, Django and one database platform (Postgres, MySQL, etc.). You could install all of it directly and start coding your first project.
The problem is that you will likely work on projects which requires different versions of Django and different versions of Python. To handle different versions of Python, we'll use PyEnv. To handle different versions of Django (within the right version of Python), we need to setup Python virtual environments. We will use pyenv-virtualenv for that.
In short, with PyEnv, we can create a virtual environment with a specific version of Python and a specific version of Django.
A virtual environment is basically a folder for a project which contains the Python libraries you need for that project. Django is one of these libraries.
Pyenv & pyenv-virutalenv makes it easy to create a virtual environment for a specific version of Python.
1.Setup Python via Pyenv
To install PyEnv on MacOS, we'll use Homebrew (brew) so we'll install brew and then PyEnv.
Open up your terminal (in the Finder it's located under: Applications/Utilities/Terminal) and type the following commands one by one.
The two first commands will take a while. For the third one, check the output of the PyEnv installation; you might get a different path (depending on the most recent version of openssl):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" brew install pyenv echo 'export PATH="/email@example.com/bin:$PATH"' >> ~/.zshrc
Now that PyEnv is installed; you can check all the available versions of Python via the pyenv install -l command. At the moment (June 2020), the latest version is Python 3.8.3. We'll install it and set it up as the default version globally on our machine:
pyenv install 3.8.2 pyenv global 3.8.2
For PyEnv to work properly, we also need to make sure it always starts with our terminal. To do so, simply type:
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.zshrc
The latest version of Python should now be set via PyEnv. To double check that it's all setup properly, open a new window and have a look at the following executables paths: python itself and pip (its package manager). Both PIP and Python must be correct (3.8.2); if these were different, nothing would work as expected.
pip -V > pip 19.2.3 from /Users/francois/.pyenv/versions/3.8.2/lib/python3.8/site-packages/pip (python 3.8) python -V > Python 3.8.2
At this stage, you've got Python properly setup. You could install other versions easily via pyenv install (pyenv install 2.7.0 for example). This is enough to start one Django project for each Python version. To run multiple projects, we'll add pyenv-virtualenv
2.Install & setup virtual environments
pyenv-virtualenv is also installed via brew:
brew install pyenv-virtualenv echo -e 'if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi' >> ~/.zshrc
You can finally create a virtual-environment for your project with the following command:
pyenv virtualenv 3.8.2 myblog
When you want to you work on your project, you must type the following command to "activate" the virtual-environment:
pyenv activate myblog
Within myblog virtual-environment, you can now install Django:
pip install Django
3.Other tools to install
Git & gitk
gitk is a simple graphic tool for Git. I use it to review my local changes before committing.
brew install git brew install git-gui
NPM, LESS & SASS
brew install npm npm install less -global sudo gem install sass
For Postgres on Mac, PostgresApp is a no-brainer. First download it and then add it to your path so Django can find it.
The path to to postgres binary files might be different. Simply use ls to double check it.
# find the bin path to add ls /Applications/Postgres.app/Contents/Versions/ ls /Applications/Postgres.app/Contents/Versions/12/ # add it echo 'export PATH="/Applications/Postgres.app/Contents/Versions/12/bin:$PATH"' >> ~/.zshrc
You are now ready to go. With that configuration, you will be able to run any Django project on your Mac.