Skip to content

Instantly share code, notes, and snippets.

@devinci-it
Last active August 2, 2024 17:55
Show Gist options
  • Select an option

  • Save devinci-it/61ec58bf5e85e1cb6186d07d66ce387b to your computer and use it in GitHub Desktop.

Select an option

Save devinci-it/61ec58bf5e85e1cb6186d07d66ce387b to your computer and use it in GitHub Desktop.
Installation guide for `thefuck` using `pyenv`

Guide to Installing thefuck with pyenv

Overview

This guide will help you install and configure thefuck using pyenv in the /opt/thefuck directory. By following these steps, you'll set up pyenv, install Python 3.11, create and manage a virtual environment, and ensure the correct executable for thefuck.

Table of Contents

  1. Install pyenv and Python 3.11

  2. Set Up the Project Directory

  3. Create and Activate a Virtual Environment

  4. Install and Manage thefuck

  5. Ensure Correct thefuck Executable


1. Install pyenv and Python 3.11

Install pyenv

Run the following command to install pyenv:

curl https://pyenv.run | bash

Configure Your Shell for pyenv

Add the following lines to your shell profile (~/.bashrc, ~/.zshrc, etc.):

# Add to ~/.bashrc or ~/.zshrc
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Apply the changes:

source ~/.bashrc

Install Python 3.11

pyenv install 3.11.4

2. Set Up the Project Directory

Create the Directory

Use sudo to create the directory:

sudo mkdir -p /opt/thefuck

Change Directory Ownership

Replace USERNAME with your actual username:

sudo chown USERNAME:USERNAME /opt/thefuck

Navigate to the Directory

cd /opt/thefuck

3. Create and Activate a Virtual Environment

Set Python 3.11 as the Local Version

pyenv local 3.11.4

Create a Virtual Environment

pyenv virtualenv 3.11.4 thefuck-venv

Activate the Virtual Environment

pyenv activate thefuck-venv

4. Install and Manage thefuck

Update pip

pip install --upgrade pip

Install thefuck

pip install thefuck

Upgrade thefuck

pip install --upgrade thefuck

5. Ensure Correct thefuck Executable

Edit Your Shell Profile

Open your shell profile file:

nano ~/.bashrc

Add a Custom Function

Add the following function to your profile:

# Custom function to ensure the correct `thefuck` executable is used
thefuck() {
    # Save the current PythonIOEncoding
    TF_PYTHONIOENCODING=$PYTHONIOENCODING

    # Set environment variables for `thefuck`
    export TF_SHELL=bash
    export TF_ALIAS=fuck
    export TF_SHELL_ALIASES=$(alias)
    export TF_HISTORY=$(fc -ln -10)
    export PYTHONIOENCODING=utf-8

    # Run `thefuck` from the specific virtual environment with arguments
    TF_CMD=$(
        ~/.pyenv/versions/3.11.4/envs/thefuck-venv/bin/thefuck "$@"
    ) && eval "$TF_CMD"

    # Clean up environment variables
    unset TF_HISTORY
    export PYTHONIOENCODING=$TF_PYTHONIOENCODING

    # Add the command to history
    history -s "$TF_CMD"
}

Reload Your Shell Profile

source ~/.bashrc

Summary

  1. Install pyenv and Python 3.11.
  2. Create the directory /opt/thefuck, set ownership, and navigate to it.
  3. Create and activate a virtual environment named thefuck-venv.
  4. Update pip, install thefuck, and upgrade if needed.
  5. Add a custom function to your shell profile to ensure the correct thefuck executable is used.
#!/bin/bash
# Variables
PYENV_DIR="$HOME/.pyenv"
PYTHON_VERSION="3.11.4"
VENV_NAME="thefuck-3.11-venv"
PROJECT_DIR="/opt/thefuck"
USERNAME=$(whoami)
# Function to check for command success
check_command_success() {
if [ $? -ne 0 ]; then
echo "Error: $1 failed. Exiting."
exit 1
fi
}
# Install pyenv
echo "Installing pyenv..."
curl https://pyenv.run | bash
check_command_success "pyenv installation"
# Configure shell for pyenv
echo "Configuring shell for pyenv..."
{
echo 'export PATH="$HOME/.pyenv/bin:$PATH"'
echo 'eval "$(pyenv init --path)"'
echo 'eval "$(pyenv init -)"'
echo 'eval "$(pyenv virtualenv-init -)"'
} >> ~/.bashrc
source ~/.bashrc
# Install Python 3.11
echo "Installing Python $PYTHON_VERSION..."
pyenv install $PYTHON_VERSION
check_command_success "Python $PYTHON_VERSION installation"
# Set up project directory
echo "Setting up project directory..."
sudo mkdir -p $PROJECT_DIR
sudo chown $USERNAME:$USERNAME $PROJECT_DIR
cd $PROJECT_DIR
# Create and activate virtual environment
echo "Creating and activating virtual environment..."
pyenv local $PYTHON_VERSION
pyenv virtualenv $PYTHON_VERSION $VENV_NAME
pyenv activate $VENV_NAME
# Install and upgrade thefuck
echo "Installing and upgrading thefuck..."
pip install --upgrade pip
pip install thefuck
pip install --upgrade thefuck
# Add custom function to shell profile
echo "Adding custom function to shell profile..."
{
echo ''
echo '# Custom function to ensure the correct `thefuck` executable is used'
echo 'thefuck() {'
echo ' /opt/thefuck/.pyenv/versions/3.11.4/envs/thefuck-venv/bin/thefuck "$@";'
echo '}'
} >> ~/.bashrc
source ~/.bashrc
echo "Installation and setup complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment