{"title":"From Scratch","description":"","section":"cookbook","version":"v1.5","path":"cookbook/from-scratch","canonical_url":"https://amberframework.org/docs/v1.5/cookbook/from-scratch","markdown_url":"https://amberframework.org/docs/v1.5/cookbook/from-scratch.md","inherited":true,"content_markdown":"# From Scratch\n\nYou can create an Amber project from scratch using `crystal init app`.\n\nAlso see [creating a project or library](https://crystal-lang.org/docs/using_the_compiler/#creating-a-project-or-library).\n\n```text\n$ crystal init app myapp\n      create  myapp/.gitignore\n      create  myapp/.editorconfig\n      create  myapp/LICENSE\n      create  myapp/README.md\n      create  myapp/.travis.yml\n      create  myapp/shard.yml\n      create  myapp/src/myapp.cr\n      create  myapp/src/myapp/version.cr\n      create  myapp/spec/spec_helper.cr\n      create  myapp/spec/myapp_spec.cr\nInitialized empty Git repository in /home/main/myapp/.git/\n```\n\n## Add Amber dependency\n\nFirst you require to add the `amber` shard dependency in your `shard.yml` file:\n\n```yaml\nname: myapp\nversion: 0.1.0\n\nauthors:\n  - Foo Bar <foo@bar.baz>\n\ntargets:\n  myapp:\n    main: src/myapp.cr\n\ncrystal: 0.24.2\n\nlicense: MIT\n\ndependencies:\n  amber:\n    github: amberframework/amber\n    version: 0.7.2\n```\n\nThen execute `shards install` :\n\n```text\n$ shards install\nFetching https://github.com/amberframework/amber.git\nFetching https://github.com/amberframework/router.git\nFetching https://github.com/amberframework/cli.git\nFetching https://github.com/mosop/optarg.git\nFetching https://github.com/mosop/callback.git\nFetching https://github.com/mosop/string_inflection.git\nFetching https://github.com/elorest/compiled_license.git\nFetching https://github.com/jeromegn/kilt.git\nFetching https://github.com/amberframework/micrate.git\nFetching https://github.com/crystal-lang/crystal-db.git\nFetching https://github.com/crystal-lang/crystal-mysql.git\nFetching https://github.com/will/crystal-pg.git\nFetching https://github.com/stefanwille/crystal-redis.git\nFetching https://github.com/jwaldrip/shell-table.cr.git\nFetching https://github.com/jeromegn/slang.git\nFetching https://github.com/crystal-lang/crystal-sqlite3.git\nFetching https://github.com/phoffer/inflector.cr.git\nFetching https://github.com/amberframework/teeplate.git\nInstalling amber (0.7.2)\nInstalling amber_router (0.0.3)\nInstalling cli (0.7.0)\nInstalling optarg (0.5.8)\nInstalling callback (0.6.3)\nInstalling string_inflection (0.2.1)\nInstalling compiled_license (0.1.3)\nInstalling kilt (0.4.0)\nInstalling micrate (0.3.1)\nInstalling db (0.5.0)\nInstalling mysql (0.4.0)\nInstalling pg (0.14.1)\nInstalling redis (1.9.0)\nInstalling shell-table (0.9.2)\nInstalling slang (1.7.1)\nInstalling sqlite3 (0.9.0)\nInstalling inflector (0.1.8)\nInstalling teeplate (0.5.0)\n```\n\n## Create project structure\n\nAll Amber's projects have a [directory structure](../guides/directory-structure.md). Basically you need a common file tree \\(unless you use a [Minimal Configuration](../examples/minimal-configuration.md)\\).\n\n```text\n.\n├── config\n│   ├── application.cr\n│   └── routes.cr\n└── src\n    ├── controllers\n    │   └── application_controller.cr\n    └── myapp.cr\n```\n\nFirst you need a `config/`folder with an `application.cr` file, also see [configuration](../guides/configuration.md).\n\n{% code-tabs %}\n{% code-tabs-item title=\"config/application.cr\" %}\n```crystal\nrequire \"amber\"\n\nrequire \"../src/controllers/**\"\n```\n{% endcode-tabs-item %}\n{% endcode-tabs %}\n\nSecondly a `routes.cr` file with pipelines and routes blocks, also see: [routing](../guides/routing/).\n\n{% code-tabs %}\n{% code-tabs-item title=\"config/routes.cr\" %}\n```crystal\nAmber::Server.configure do |app|\n  pipeline :web do\n    # plug PipeName.new\n  end\n\n  routes :web do\n    # get \"/\", SomeController, :some_action\n  end\nend\n```\n{% endcode-tabs-item %}\n{% endcode-tabs %}\n\nThen a basic setup \\(without views nor models\\) requires an `application_controller.cr` file inside `src/controllers` directory.\n\n{% code-tabs %}\n{% code-tabs-item title=\"src/controllers/application\\_controller.cr\" %}\n```crystal\nclass ApplicationController < Amber::Controller::Base\nend\n```\n{% endcode-tabs-item %}\n{% endcode-tabs %}\n\nFinally you need to call `Amber::Server` and all your project in your main `myapp.cr`file.\n\n{% code-tabs %}\n{% code-tabs-item title=\"src/myapp.cr\" %}\n```crystal\nrequire \"../config/*\"\n\nAmber::Server.start\n```\n{% endcode-tabs-item %}\n{% endcode-tabs %}\n\n## Build and run your project\n\nTo compile your project use `shards build myapp`and run the executable with `bin/myapp`.\n\n```text\n$ shards build myapp\nDependencies are satisfied\nBuilding: myapp\n$ bin/myapp \n06:52:37 Server     | (INFO) Amber 0.7.2 serving application \"Amber_app\" at http://localhost:3000\n06:52:37 Server     | (INFO) Server started in development.\n06:52:37 Server     | (INFO) Startup Time 00:00:00.000273000\n```\n\nThat's it!, now you're ready to create any basic project from scratch without Amber CLI.\n\n## Local CLI\n\nAs additional step you can compile an amber CLI inside your project using:\n\n```text\ncrystal build lib/amber/src/amber/cli.cr -o bin/amber -s\n```\n\nThen use `bin/amber` to do scaffolding, run migrations and [more](../cli/)."}