Tags:

Local playground project with Lando for Drupal 8

<p><span class="drop-cap">T</span>oday we’ll set up a Drupal 8 local development environment to test things out (whatever is on your developing mind) with <a href="https://lando.dev/"><span><span><span><span><span><span><span><span>Lando</span></span></span></span></span></span></span></span></a> and Composer. </p> <p>So to start, you should have both Lando and Composer installed in your system. I’m on Mac, so most of my examples are going to be from the command line from the terminal.</p> <p>If you haven’t done it yet, install <a href="https://docs.lando.dev/basics/installation.html">Lando</a> and Composer in your system.</p> <h2>Create a project via Composer</h2> <p>Just <a href="https://www.drupal.org/docs/develop/using-composer/using-composer-to-install-drupal-and-manage-dependencies#s-create-a-project"><span><span><span><span><span><span><span><span>run this command</span></span></span></span></span></span></span></span></a><span><span><span><span><span><span> </span></span></span></span></span></span>on your Terminal to get started. This will create a Drupal 8 project the composer way.</p> <pre> <code class="language-bash">composer create-project drupal/recommended-project playground</code></pre> <p>You’ll notice I named my project playground. You can name yours however you feel like, it just makes sense to me, since I’ll use this to play around and find out new stuff I’ll probably use in other projects.</p> <p>After you have run the composer line above, you’ll see a message much like this on your terminal:</p> <p><img alt=" Congratulations, you’ve installed the Drupal codebase from the drupal/recommended-project template!" data-entity-type="file" data-entity-uuid="3dcb3b10-2610-4ba1-a79e-101992e9de9b" src="/sites/default/files/inline-images/Screen%20Shot%202020-09-12%20at%2002.05.05.png" width="1684" height="992" loading="lazy" /></p> <p> </p> <p>All right, we’re on the right track. Inside our project’s folder we should have these directories and files:</p> <h2><img alt="playground directory tree" data-entity-type="file" data-entity-uuid="3aefffc1-1507-49c0-8308-b270c18db57f" src="/sites/default/files/inline-images/Screen%20Shot%202020-09-12%20at%2002.09.06.png" width="1684" height="992" loading="lazy" />Now we’re moving on to Lando...</h2> <p>To set up Lando for your project (I’ll do it for my playground), you just have to run lando init in your project’s folder. The guys from Lando did an amazing job building up this tool (<a href="https://docs.lando.dev/contrib/contributing.html"><span><span><span><span><span><span><span><span>kudos and we love you!</span></span></span></span></span></span></span></span></a>), so if you choose, you can just answer each interactive question that Lando pops up to set up the project. If however, you’re looking for a fast-forward version of this, do it as I did, and just pass all the relevant parameters like this:</p> <pre> <code class="language-bash">lando init \ --name playground \ --recipe drupal8 \ --source cwd \ --webroot web</code></pre> <p><span><span><span><span><span><span>This will generate a .lando.yml file with the needed configurations:</span></span></span></span></span></span></p> <pre> <code class="language-bash">cat .lando.yml</code></pre> <p>We’re almost there, just a few more steps. If you run <em>lando start</em> now, Lando will download a bunch of Docker images, create containers and volumes and quite transparently to us, set up our database and some local URLs for us to access and use our site:</p> <pre> <code class="language-bash">lando start</code></pre> <p><em>So don’t worry if this takes a few minutes, it is worth the wait.</em></p> <p>Here’s a screenshot of my terminal, your’s might have a few more messages if you’re doing this for the first time.</p> <p><img alt="Lando start" data-entity-type="file" data-entity-uuid="18cc0ef1-fd85-4aad-af4e-f3f31d763cd7" src="/sites/default/files/inline-images/Screen%20Shot%202020-09-12%20at%2001.51.51.png" class="align-left" width="2820" height="1740" loading="lazy" /></p> <p>Nevermind my warnings, the important part are those green lines at the bottom. If you access any of those Appserver URLS you’ll see the Drupal’s site installation page. </p> <p><img alt="Drupal Installation Page" data-entity-type="file" data-entity-uuid="63aed25a-acdd-4f78-b9d0-aac01b3ff7ae" src="/sites/default/files/inline-images/Screen%20Shot%202020-09-12%20at%2002.03.41.png" width="2154" height="1484" loading="lazy" /></p> <p><span><span><span><span><span><span>How you complete most of these forms is entirely up to you. The only screen that needs our attention is the database screen below:</span></span></span></span></span></span></p> <p><img alt="Database Config" data-entity-type="file" data-entity-uuid="cb85fe6d-6985-4b09-a8b0-30d7b74841b6" src="/sites/default/files/inline-images/Screen%20Shot%202020-09-12%20at%2002.04.42_0.png" width="2154" height="1484" loading="lazy" /></p> <p>This information you must get from Lando at this point, because the actual database settings were set by the script we run earlier. It’s very easy to get this information, you just have to run lando info in your terminal.</p> <pre> <code class="language-bash"> lando info [ { service: 'appserver', urls: [ 'https://localhost:32784', 'http://localhost:32786', 'http://playground.lndo.site:8080/', 'https://playground.lndo.site/' ], type: 'php', healthy: true, via: 'apache', webroot: 'web', config: { php: '/Users/lucas/.lando/config/drupal8/php.ini' }, version: '7.3', meUser: 'www-data', hasCerts: true, hostnames: [ 'appserver.playground.internal' ] }, { service: 'database', urls: [], type: 'mysql', healthy: true, internal_connection: { host: 'database', port: '3306' }, external_connection: { host: '127.0.0.1', port: '32785' }, healthcheck: 'bash -c "[ -f /bitnami/mysql/.mysql_initialized ]"', creds: { database: 'drupal8', password: 'drupal8', user: 'drupal8' }, config: { database: '/Users/lucas/.lando/config/drupal8/mysql.cnf' }, version: '5.7', meUser: 'www-data', hasCerts: false, hostnames: [ 'database.playground.internal' ] } ]</code></pre> <p><span><span><span><span><span><span>As you can see in this example, my database is called ‘<em>drupal8</em>’, it’s password and user are also ‘<em>drupal8</em>’, the host is called ‘<em>database</em>’ and the port we’re using is the ‘<em>3306</em>’.</span></span></span></span></span></span></p> <p><span><span><span><span><span><span>Just fill the form with this information, and voilá, you’re ready to finish the installation process. </span></span></span></span></span></span></p> <p><span><span><span><span><span><span><img alt="Drupal 8 fresh installatiomn landing page" data-entity-type="file" data-entity-uuid="d1992dfe-3e2c-4e92-8d84-7d3bb649c37a" src="/sites/default/files/inline-images/Screen%20Shot%202020-09-12%20at%2002.10.39.png" width="2202" height="1366" loading="lazy" /></span></span></span></span></span></span></p> <h2>Here are some tips and tricks to go on:</h2> <ul> <li><span><span><span><span><span><span>Store the database in a file once the installation is complete. That way, every once in a while, you can drop the playground’s database and start over without having to do the installation process again. You can export the database very easily using the command</span></span></span></span></span></span> </li> </ul> <pre> <code class="language-bash">lando db-export filename.sql #You can import that database again, as many times as you want, by running lando db-import filename.sql.gz </code></pre> <ul> <li>If your team members wish to have their own playground project, just commit the files to a git repository (.lando.yml included) and have them run lando start on their own computers. For an extra treat, send them a copy of your newly-created database so that they can start using it pretty much out of the box.</li> </ul> <p><span><span><span><span><em><span>This tutorial is meant for you to start this project from scratch. If you were planning to use an existing database and need to set up credentials we’ll cover that in another post soon. Stay tuned!</span></em></span></span></span></span></p>

Related blog posts

Adding Compass to a local dev environment with Lando

One of my coworkers asked me to set up a Lando dev environment for him, with the possibility to run Compass commands from within the Lando containers.