Install Yarn 1.22.22
December 6, 2024 · AI Conversations
# Install system dependencies and PHP extensions
RUN apt-get update && apt-get install -y
zip
unzip
git
curl
libpng-dev
libjpeg-dev
libfreetype6-dev
libzip-dev
&& docker-php-ext-install pdo pdo_mysql zip gd mysqli
&& apt-get clean && rm -rf /var/lib/apt/lists/*
# Install Node.js (version 20.x)
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash – &&
apt-get install -y nodejs &&
apt-get clean && rm -rf /var/lib/apt/lists/*
# Install Yarn 4.3.1 using Corepack
RUN corepack enable && corepack prepare yarn@4.3.1 –activate
# Verify Yarn installation
RUN yarn –version
# Copy Composer (from official Composer image)
COPY –from=composer:2.7 /usr/bin/composer /usr/bin/composer
# Copy custom php.ini
COPY php.ini /usr/local/etc/php/php.ini
# Set the working directory
WORKDIR /var/www
# Expose PHP-FPM port
EXPOSE 9000
INstall yarn 1.22.22 rather than 4.3.1
Here's your updated Dockerfile with the change:
FROM php:8.2-fpm
# Install system dependencies and PHP extensions
RUN apt-get update && apt-get install -y
zip
unzip
git
curl
libpng-dev
libjpeg-dev
libfreetype6-dev
libzip-dev
&& docker-php-ext-install pdo pdo_mysql zip gd mysqli
&& apt-get clean && rm -rf /var/lib/apt/lists/*
# Install Node.js (version 20.x)
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - &&
apt-get install -y nodejs &&
apt-get clean && rm -rf /var/lib/apt/lists/*
# Install Yarn 1.22.22
RUN curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.22.22 &&
ln -sf /root/.yarn/bin/yarn /usr/local/bin/yarn &&
ln -sf /root/.yarn/bin/yarnpkg /usr/local/bin/yarnpkg
# Verify Yarn installation
RUN yarn --version
# Copy Composer (from official Composer image)
COPY --from=composer:2.7 /usr/bin/composer /usr/bin/composer
# Copy custom php.ini
COPY php.ini /usr/local/etc/php/php.ini
# Set the working directory
WORKDIR /var/www
# Expose PHP-FPM port
EXPOSE 9000
### Key Changes:
1. **Replaced Corepack Yarn installation**:
– Removed `corepack enable && corepack prepare yarn@4.3.1 –activate`.
– Added a direct download of the Yarn 1.22.22 installation script and updated the PATH.
2. **Symbolic Links**:
– Created symbolic links for `yarn` and `yarnpkg` to make them globally accessible.
Now, running `yarn –version` in this container should confirm Yarn 1.22.22 is installed.
### Option 1: Downgrade `packageManager` in `package.json`
Update your `package.json` to match the globally installed Yarn version (1.22.22):
"packageManager": "yarn@1.22.22"
After editing the `package.json`, rebuild your Docker image and the project should run without errors.
—
### Option 2: Use Yarn 4.3.1
If you want to stick to Yarn 4.3.1 as defined in your `package.json`, you need to update your Dockerfile to install Yarn 4.3.1. Here’s how you can update your Dockerfile:
#### Updated Dockerfile:
FROM php:8.2-fpm
# Install system dependencies and PHP extensions
RUN apt-get update && apt-get install -y
zip
unzip
git
curl
libpng-dev
libjpeg-dev
libfreetype6-dev
libzip-dev
&& docker-php-ext-install pdo pdo_mysql zip gd mysqli
&& apt-get clean && rm -rf /var/lib/apt/lists/*
# Install Node.js (version 20.x)
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - &&
apt-get install -y nodejs &&
apt-get clean && rm -rf /var/lib/apt/lists/*
# Install Yarn 4.3.1 using Corepack
RUN corepack enable && corepack prepare yarn@4.3.1 --activate
# Verify Yarn installation
RUN yarn --version
# Copy Composer (from official Composer image)
COPY --from=composer:2.7 /usr/bin/composer /usr/bin/composer
# Copy custom php.ini
COPY php.ini /usr/local/etc/php/php.ini
# Set the working directory
WORKDIR /var/www
# Expose PHP-FPM port
EXPOSE 9000
—
### Rebuild and Test
After making either of these changes, rebuild your Docker image:
docker build -t your-image-name .
Then run the container again. Choose the option based on whether you want to align with Yarn 1.22.22 or upgrade to Yarn 4.3.1.
"name": "sage",
"private": true,
"browserslist": [
"extends @roots/browserslist-config"
],
"engines": {
"node": ">=20.0.0"
},
"type": "module",
"scripts": {
"dev": "bud dev",
"build": "bud build",
"translate": "npm run translate:pot && npm run translate:update",
"translate:pot": "wp i18n make-pot . ./resources/lang/sage.pot –include="theme.json,patterns,app,resources"",
"translate:update": "for file in ./resources/lang/*.po; do wp i18n update-po ./resources/lang/sage.pot $file; done",
"translate:compile": "npm run translate:mo && npm run translate:js",
"translate:js": "wp i18n make-json ./resources/lang –pretty-print",
"translate:mo": "wp i18n make-mo ./resources/lang ./resources/lang"
},
"devDependencies": {
"@roots/bud": "6.20.0",
"@roots/bud-sass": "^6.20.0",
"@roots/bud-stylelint": "^6.20.0",
"@roots/sage": "6.20.0"
},
"dependencies": {
"@popperjs/core": "^2.11.8",
"bootstrap": "^5.3.3",
"mixitup": "^3.3.1"
},
"packageManager": "yarn@4.3.1+sha512.af78262d7d125afbfeed740602ace8c5e4405cd7f4735c08feb327286b2fdb2390fbca01589bfd1f50b1240548b74806767f5a063c94b67e431aabd0d86f7774"
}
To resolve this, you should align your environment with the specified Yarn version (4.3.1). Here’s how:
—
### Updated Dockerfile to Install Yarn 4.3.1
Modify your Dockerfile to install Yarn 4.3.1 as specified in the `package.json`:
FROM php:8.2-fpm
# Install system dependencies and PHP extensions
RUN apt-get update && apt-get install -y
zip
unzip
git
curl
libpng-dev
libjpeg-dev
libfreetype6-dev
libzip-dev
&& docker-php-ext-install pdo pdo_mysql zip gd mysqli
&& apt-get clean && rm -rf /var/lib/apt/lists/*
# Install Node.js (version 20.x)
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - &&
apt-get install -y nodejs &&
apt-get clean && rm -rf /var/lib/apt/lists/*
# Install Yarn 4.3.1 using Corepack
RUN corepack enable && corepack prepare yarn@4.3.1 --activate
# Verify Yarn installation
RUN yarn --version
# Copy Composer (from official Composer image)
COPY --from=composer:2.7 /usr/bin/composer /usr/bin/composer
# Copy custom php.ini
COPY php.ini /usr/local/etc/php/php.ini
# Set the working directory
WORKDIR /var/www
# Expose PHP-FPM port
EXPOSE 9000
—
### Steps to Apply Changes
1. **Build the Docker image:**
docker build -t your-image-name .
2. **Run the container:**
docker run -it your-image-name
3. **Verify Yarn version inside the container:**
yarn --version
Ensure the output matches `4.3.1`.
4. **Run your project commands:**
You can now use `yarn` commands (`yarn dev`, `yarn build`, etc.) without compatibility issues.
—
### If You Need to Stay on Yarn 1.22.22
If you must use Yarn 1.22.22, edit your `package.json` to:
"packageManager": "yarn@1.22.22"
However, this is not recommended if your project explicitly requires Yarn 4.3.1, as some functionality may break.
yarn run v1.22.22
error Command "install" not found.
### Steps to Install Dependencies
1. **Use the Correct Command:**
Instead of running:
yarn run install
Use:
yarn install
This command installs all the dependencies listed in your `package.json` file.
2. **Verify Your Scripts:**
You can check the available scripts in your `package.json` to confirm what commands can be run. Here’s a reminder of your `scripts` section:
"scripts": {
"dev": "bud dev",
"build": "bud build",
"translate": "npm run translate:pot && npm run translate:update",
"translate:pot": "wp i18n make-pot . ./resources/lang/sage.pot --include="theme.json,patterns,app,resources"",
"translate:update": "for file in ./resources/lang/*.po; do wp i18n update-po ./resources/lang/sage.pot $file; done",
"translate:compile": "npm run translate:mo && npm run translate:js",
"translate:js": "wp i18n make-json ./resources/lang --pretty-print",
"translate:mo": "wp i18n make-mo ./resources/lang ./resources/lang"
}
You can run any of the defined scripts using `yarn run `, for example:
yarn run dev
3. **Ensure Yarn is Installed Correctly:**
If you still encounter issues, you can verify your Yarn installation by checking the version:
yarn --version
4. **Check for Potential Issues:**
– If `yarn install` fails, it might be due to issues with your `package.json` or with specific dependencies.
– Make sure you are in the correct directory where your `package.json` is located when running the commands.
After running `yarn install`, all dependencies should be installed correctly, and you can proceed with your development tasks.