Skip to content

Installing Django Rest Framework on macOS

Part 2 of 8 in the series Django REST Framework

In this post, we will see the steps to install Django Rest Framework(DRF) on  macOS.

We will install

  • Python3 using brew
  • Pip3
  • virtualenv
  • Django and Django Rest Framework inside our virtualenv

Step 1 : Install Python3 using brew

$ brew install python3

If installation goes smooth, then go to step 2. If you face any of below errors, try below solutions.

Error :

$ brew install python3
    Error: The following directories are not writable by your user:

    You should change the ownership of these directories to your user.
      sudo chown -R $(whoami) /usr/local/include /usr/local/lib

    And make sure that your user has write permission.
      chmod u+w /usr/local/include /usr/local/lib

Solution as given in error description  :

$ sudo chown -R $(whoami) /usr/local/include /usr/local/lib
$ chmod u+w /usr/local/include /usr/local/lib

Error :

  $ brew install python3

    Error: The following formula
      python cannot be installed as binary package and must be 
      built from source.
    Install the Command Line Tools:
      xcode-select --install

Again solution suggested in error description :

$ xcode-select --install


$  brew install python3    
The formula built, but is not symlinked into /usr/local     Permission denied @ dir_s_mkdir - /usr/local/Frameworks     
Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks

Solution :

$ sudo mkdir /usr/local/Frameworks
$ sudo chown -R $(whoami) /usr/local/Frameworks/
$ brew link python

Python3 should be installed now.

$ python3 -V
Python 3.7.4

Step 2 : Install Pip3 and Virtualenv

//To get pip3
$ brew postinstall python3

// Install virtualenv
$ sudo pip3 install virtualenv

Step 3 : Create a new virtual environment for our project.

Let’s call our virtual environment “venv”. You can use any name.

$ virtualenv venv -p python3
Running virtualenv with interpreter /usr/local/bin/python3
Already using interpreter /usr/local/opt/python/bin/python3.7
Using base prefix '/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7'
New python executable in /Users/hpatel/Slides/tut/repo/venv/bin/python3.7
Also creating executable in /Users/hpatel/Slides/tut/repo/venv/bin/python
Installing setuptools, pip, wheel...

Above step will create venv with python3 as default python interpreter in that environment.

We can now activate venv and install our required python libs inside that virtual environment which won’t affect default python installation on machine and any other virtual environments.

Step 4 : Install Django and Django-Rest-Framework (DRF

$ source venv/bin/activate
(venv) $ python -V
Python 3.7.4

(venv) $ pip list
Package    Version
---------- -------
pip        19.3.1 
setuptools 45.0.0 
wheel      0.33.6 
(venv) $ 
(venv) $ pip install Django
(venv) $ pip install djangorestframework
// Good to have libraries, we won't be using in tutorial.
(venv) $ pip install markdown
(venv) $ pip install django-filter
(venv) $ pip install pygments

Step 5 : Check installation

(venv) $ pip list
Package             Version
------------------- -------
asgiref             3.2.3  
Django              3.0.2  
django-filter       2.2.0  
djangorestframework 3.11.0 
Markdown            3.1.1  
pip                 19.3.1 
Pygments            2.5.2  
pytz                2019.3 
setuptools          45.0.0 
sqlparse            0.3.0  
wheel               0.33.6 
(venv) $ 

Library versions can be different.  

In next part, we will move to create our Django Project and App before creating our REST APIs in DRF.  
Note : Remember to deactivate the virtual environment by typing “deactivate” when you are not working on this project. Activate again when you start again.

Series Navigation<< Introduction : REST API with Django REST FrameworkCreating Django Project and App >>
Published inDjango

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: