<p>For vlucas/phpdotenv for 3.4.0, this index.php template should work:</p>
<pre class="lang-php prettyprint-override"><code>&lt;?php
/**
 * Craft web bootstrap file
 */

// Set path constants
define('CRAFT_BASE_PATH', dirname(__DIR__));
define('CRAFT_VENDOR_PATH', CRAFT_BASE_PATH.'/vendor');

// Load Composer's autoloader
require_once CRAFT_VENDOR_PATH.'/autoload.php';

// Load dotenv?
if (class_exists('Dotenv\Dotenv') &amp;&amp; file_exists(CRAFT_BASE_PATH.'/.env')) {
    Dotenv\Dotenv::create(CRAFT_BASE_PATH)-&gt;load();
}

// Load and run Craft
define('CRAFT_ENVIRONMENT', getenv('ENVIRONMENT') ?: 'production');
$app = require CRAFT_VENDOR_PATH.'/craftcms/cms/bootstrap/web.php';
$app-&gt;run();
</code></pre>
<p>For console:</p>
<pre class="lang-php prettyprint-override"><code>#!/usr/bin/env php
&lt;?php
/**
 * Craft console bootstrap file
 */

// Set path constants
define('CRAFT_BASE_PATH', __DIR__);
define('CRAFT_VENDOR_PATH', CRAFT_BASE_PATH.'/vendor');

// Load Composer's autoloader
require_once CRAFT_VENDOR_PATH.'/autoload.php';

// Load dotenv?
if (class_exists('Dotenv\Dotenv') &amp;&amp; file_exists(CRAFT_BASE_PATH.'/.env')) {
    Dotenv\Dotenv::create(CRAFT_BASE_PATH)-&gt;load();
}

// Load and run Craft
define('CRAFT_ENVIRONMENT', getenv('ENVIRONMENT') ?: 'production');
$app = require CRAFT_VENDOR_PATH.'/craftcms/cms/bootstrap/console.php';
$exitCode = $app-&gt;run();
exit($exitCode);
</code></pre>
<p>It is the <code>Dotenv\Dotenv::create(CRAFT_BASE_PATH)-&gt;load();</code> line that allows it to work. Repeat for your craft console file.</p>
<p>You can use the 5.x.x version of vlucas/phpdotenv, but it does require changes to how it is loaded compared to 3.4.x</p>
<p>The Craft CMS starter project provides a 5.x.x example, however this is using a bootstrap include method across both web and console, so you'd have to refactor slightly, but worth it as you don't have to duplicate the same logic in the your console file anymore.</p>
<p><a href="https://github.com/craftcms/craft/blob/main/bootstrap.php" rel="nofollow noreferrer">https://github.com/craftcms/craft/blob/main/bootstrap.php</a></p>

CC BY-SA 4.0
2022-06-27T19:34:03.063Z
There's a separate console file???
joomkit
CC BY-SA 4.0
2022-06-27T21:03:50.093Z
Dotenv is loaded on both the web/index.php and craft file at the root, so you'll need to update both to the necessary Dotenv loading syntax relative to the version in use. If you look at the Craft starter project, they use a bootstrap include file across both so you don't have to duplicate it.

https://github.com/craftcms/craft
James White
CC BY-SA 4.0
2022-06-29T14:39:53.963Z
Thanks James. I still don't get what the issue is. I have upgraded another site on local dev with no issues - they have the same craft  and phpdotenv versions in their respective composer.json. Both load frontend fine. But one will not run craft commands on command line. Last ditch attempt i swapped the craft shell file from the working to the broken - hey presto. I realised thats what you call the console file? #still-learning thx
joomkit
CC BY-SA 4.0
2022-06-29T21:40:48.940Z
Yes the craft file at the root is the PHP wrapper file for calling Craft console commands with ./craft. This was likely using the wrong dotenv syntax for loading based on the version.
James White
CC BY-SA 4.0
2022-06-30T12:35:43.690Z
Yes that was it
joomkit
CC BY-SA 4.0
2023-06-08T23:33:01.073Z
Note: I just updated from Craft 3.7 to 4.x, and I found it easiest to leave phpdotenv version as 2.4.0, no changes to other files were required.
4midori
CC BY-SA 4.0
2022-06-27T18:02:08.930Z
2022-07-01T07:02:36.010Z
2022-07-01T07:02:36.010Z
James White
<p>Ran upgrade on my local dev environment.
Seemed to go fine.
Front end site broke and command line updates for craft also error.</p>
<p>My index.php looks like</p>
<pre><code>// Set path constants
define('CRAFT_BASE_PATH', dirname(__DIR__));
define('CRAFT_VENDOR_PATH', CRAFT_BASE_PATH.'/vendor');

// Load Composer's autoloader
require_once CRAFT_VENDOR_PATH.'/autoload.php';

// Load dotenv?
if (file_exists(CRAFT_BASE_PATH.'/.env')) {
    (new Dotenv\Dotenv(CRAFT_BASE_PATH))-&gt;load();
}

// Load and run Craft
define('CRAFT_ENVIRONMENT', getenv('ENVIRONMENT') ?: 'production');
$app = require CRAFT_VENDOR_PATH.'/craftcms/cms/bootstrap/web.php';
$app-&gt;run();
</code></pre>
<p>CLI craft update gives me:</p>
<pre><code> Uncaught TypeError: Argument 1 passed to Dotenv\Dotenv::__construct() 
...vendor/vlucas/phpdotenv/src/Dotenv.php:60
</code></pre>
<p>I tried downgrading vulcas DotEnv package with no luck.
I then set craft requirement in compose to 3.4.0 but still getting issues loading and updating on CLI, all centered around the Dotenv package.</p>
<p>I did a vanilla install via CLI to check index.php content which are different - AND i noticed composer.json states craft requires php8! Not what the docs say but thats another post.</p>
<p>Any help appreciated</p>

<upgrades><cli>
Broken site after upgrade via composer from craft 3.37.38
# Broken site after upgrade via composer from craft 3.37.38

(Asked by: username_0 on 2022-06-27)

Ran upgrade on my local dev environment.
Seemed to go fine.
Front end site broke and command line updates for craft also error.
My index.php looks like
// Set path constants
define('CRAFT_BASE_PATH', dirname(__DIR__));
define('CRAFT_VENDOR_PATH', CRAFT_BASE_PATH.'/vendor');

// Load Composer's autoloader
require_once CRAFT_VENDOR_PATH.'/autoload.php';

// Load dotenv?
if (file_exists(CRAFT_BASE_PATH.'/.env')) {
    (new Dotenv\Dotenv(CRAFT_BASE_PATH))->load();
}

// Load and run Craft
define('CRAFT_ENVIRONMENT', getenv('ENVIRONMENT') ?: 'production');
$app = require CRAFT_VENDOR_PATH.'/craftcms/cms/bootstrap/web.php';
$app->run();

CLI craft update gives me:
 Uncaught TypeError: Argument 1 passed to Dotenv\Dotenv::__construct() 
...vendor/vlucas/phpdotenv/src/Dotenv.php:60

I tried downgrading vulcas DotEnv package with no luck.
I then set craft requirement in compose to 3.4.0 but still getting issues loading and updating on CLI, all centered around the Dotenv package.
I did a vanilla install via CLI to check index.php content which are different - AND i noticed composer.json states craft requires php8! Not what the docs say but thats another post.
Any help appreciated

**Tags:** upgrades, cli

---

## 1 answers

### Answer by username_1 on 2022-06-27. Score: 4 | Accepted answer

For vlucas/phpdotenv for 3.4.0, this index.php template should work:
load();
}

// Load and run Craft
define('CRAFT_ENVIRONMENT', getenv('ENVIRONMENT') ?: 'production');
$app = require CRAFT_VENDOR_PATH.'/craftcms/cms/bootstrap/web.php';
$app->run();

For console:
#!/usr/bin/env php
load();
}

// Load and run Craft
define('CRAFT_ENVIRONMENT', getenv('ENVIRONMENT') ?: 'production');
$app = require CRAFT_VENDOR_PATH.'/craftcms/cms/bootstrap/console.php';
$exitCode = $app->run();
exit($exitCode);

It is the Dotenv\Dotenv::create(CRAFT_BASE_PATH)->load(); line that allows it to work. Repeat for your craft console file.
You can use the 5.x.x version of vlucas/phpdotenv, but it does require changes to how it is loaded compared to 3.4.x
The Craft CMS starter project provides a 5.x.x example, however this is using a bootstrap include method across both web and console, so you'd have to refactor slightly, but worth it as you don't have to duplicate the same logic in the your console file anymore.
https://github.com/craftcms/craft/blob/main/bootstrap.php

> **Comments:**
> **username_0**: There's a separate console file???  
> **username_1**: Dotenv is loaded on both the web/index.php and craft file at the root, so you'll need to update both to the necessary Dotenv loading syntax relative to the version in use. If you look at the Craft starter project, they use a bootstrap include file across both so you don't have to duplicate it.  https://github.com/craftcms/craft  
> **username_0**: Thanks James. I still don't get what the issue is. I have upgraded another site on local dev with no issues - they have the same craft  and phpdotenv versions in their respective composer.json. Both load frontend fine. But one will not run craft commands on command line. Last ditch attempt i swapped the craft shell file from the working to the broken - hey presto. I realised thats what you call the console file? #still-learning thx  
> **username_1**: Yes the craft file at the root is the PHP wrapper file for calling Craft console commands with ./craft. This was likely using the wrong dotenv syntax for loading based on the version.  
> **username_0**: Yes that was it  
> **username_2**: Note: I just updated from Craft 3.7 to 4.x, and I found it easiest to leave phpdotenv version as 2.4.0, no changes to other files were required.  

---

