DEV Community

Joseph Mancuso for Masonite

Posted on • Updated on

Masonite Framework Tutorial Series Part 1 - Installation

Introduction

I'm Joe Mancuso, the creator of Masonite and I'll be coming out with a few of these tutorial articles which I'll eventually use as a transcript to create a video series.

If you want to stay up to date with the series and all future articles of Masonite then be sure to give a follow! I'll release a new tutorial part every few days so be sure to stay tuned!

This series will be more of an in depth look at each concept where we will give a practical example and a more in depth view at each part. These tutorials will be similar to the official documentation but we will have room to go more in depth here than we can in the documentation.

We will not be building something specific but starting "fresh" at each part. You can use this conceptual knowledge to build awesome a badass applications.

This article will talk about installing Masonite.

What Masonite Is

Masonite is a modern and developer centric Python web framework. Masonite takes a much different approach to building Python applications than many of Python frameworks. The architecture of Masonite is much more similar to the Laravel framework and borrows a lot of the file structure and design concepts such as an IOC container, auto resolving dependency injection of controller methods, helper functions, manager pattern and many of design patterns that Laravel uses.

Masonite is a completely batteries included Python framework that has a plethora of features too long to list. For a more exhaustive list of features you can check out the Official Documentation here. Or you can follow along this tutorial series and eventually uncover detailed explanations of each feature.

Installation

Masonite comes with a CLI tool called Craft. Craft commands will become your best friend and you will use them ALL the time. Craft commands are akin to Artisan commands for Laravel or the Rails command line for Ruby on Rails; if you are coming from that world.

Installing Craft

We can install craft simply by installing the Masonite CLI tool:

$ pip install masonite-cli
Enter fullscreen mode Exit fullscreen mode

Craft can be ran on Python 2.7+ and Python 3.4+ so it doesn't require installing from pip3 (which would install under the Python 3 installation).


\* * * *

If you are getting an error that looks like an SSL certificate error, then you likely have an out of date OpenSSL or LibreSSL version (like OpenSSL 0.9). You're machine most likely already has this requirement but you'll need to have OpenSSL 1.0+ or LibreSSL 2.2.7+ installed. We can check this by running:

$ openssl version
Enter fullscreen mode Exit fullscreen mode

If you have an OpenSSL version lower than the recommended version's above then you'll need to upgrade them.

\* * * *

Once that installs we should have access to several craft commands. We can check these commands by running:

$ craft
Enter fullscreen mode Exit fullscreen mode

And should see an output like:

Craft Version: 2.0.10

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  help     Displays help for a command
  install  Installs all of Masonite's dependencies
  list     Lists commands
  new      Creates a new Masonite project
  package  Creates a new package
Enter fullscreen mode Exit fullscreen mode

You'll see we have a command call new so let's run that command to get our project up and running:

$ craft new project_name
Enter fullscreen mode Exit fullscreen mode

What this command will do is download a zip of the repository located at the MasoniteFramework/masonite GitHub repository, unzip the file and rename the directory it just downloaded to your project name.

We can now cd into our project:

$ cd project_name
Enter fullscreen mode Exit fullscreen mode

Activating Virtual Environment (optional)

You don't have to do this step but it's good to activate virtual environments for each application you are building so you don't confuse and combine different dependencies of your applications. Whether you're building Masonite, Django or Flask applications, it's good to have isolated virtual environments for each.

Python3 comes with a built in virtual environment module called venv. On Mac and Windows we can create one by running:

$ python3 -m venv venv
Enter fullscreen mode Exit fullscreen mode

That last parameter can be whatever you wan't your virtual environment folder to be called. I always call mine venv. Now we just need to activate our virtual environment:

On Mac we can run:

$ source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

On Windows we can run:

$ ./venv/Scripts/activate
Enter fullscreen mode Exit fullscreen mode

We should now be in a virtual environment and we will know if we succeeded because we should now see our virtual environment name next to our terminal command line:

(venv) My-MBP:project_name username$ 
Enter fullscreen mode Exit fullscreen mode

Installing Dependencies

Normally we would now run something like pip install -r requirements.txt but there is an additional step we need to take if we do that approach and that is to generate a secret key for our application. Craft has combined these two steps and we can just run a single command:

$ craft install
Enter fullscreen mode Exit fullscreen mode

This command will do 3 things for us. The first thing it will do is copy all the data in the .env-example file that was created when we created the Masonite application and it will run 2 commands:

$ pip install -r requirements.txt
$ craft key --store
Enter fullscreen mode Exit fullscreen mode

What this is designed to do is install all of the requirements and then generate a secret key for our application and put it into our .env file. The craft key command comes from our application itself.

Each Masonite application can have it's own set of commands. If the craft command is ran inside of a Masonite application, it will grab all commands of those application in addition to it's own commands.

Serving Masonite

We can now run our Masonite application! We can do so simply by running the serve command:

$ craft serve -r
Enter fullscreen mode Exit fullscreen mode

Notice this -r flag. What this will do is run the WSGI server with Waitress and set's the server to run with auto-reloading. Whenever you make a change to your application and save the file, it will refresh the WSGI server so you don't have to stop and rerun the server every time you make a change.

This command will run the server on http://localhost:8000. We can change this by using 2 options:

$ craft serve -r --host 127.0.0.1 --port 8080
Enter fullscreen mode Exit fullscreen mode

That's it for Part 1! You now have a fully functional Masonite application! If you don't want to wait for the next part of the series then you can checkout the Official Documentation here.

Make sure you follow so you don't miss Part 2!

Top comments (0)