Skip to content

Instantly share code, notes, and snippets.

@farhad-taran
Last active March 2, 2023 21:08
Show Gist options
  • Save farhad-taran/41ea3c5f336a09285a9ac9b5c963d051 to your computer and use it in GitHub Desktop.
Save farhad-taran/41ea3c5f336a09285a9ac9b5c963d051 to your computer and use it in GitHub Desktop.
How to install Brew on WSL2

open ubuntu, this should give you a command prompt that looks like this:

username@DESKTOP-S0EN2QH:~$

WSL is mounted on the c drive and the system32 path, which is a system folder which does not permit users to have full access, because of this, it is important to make sure to install applications from your default users home path ~ as shown above, this is where this user has full access permissions and will ensure that all the applications and interactions will complete withouth any access permission errors.

even though you will have a default .bashrc and .profile file which get run automatically by ubunto everytime you open a terminal, it is best to run the following command to create a .bash_profile:

echo "source ~/.bashrc" >> ~/.bash_profile

this file will hold your custom shortcut commands called aliases or common environment variable. close the terminal and open another ubuntu terminal instance, this will load the new .bash_profile. if you start to get any other output before the initial terminal info username@DESKTOP-S0EN2QH:~$ it might be an indicator of a faulty .bash_profile. to debug and see what is wrong with your, .profile, .bash_profile or whatever else that is executed when you open up a terminal run the following command immediately after you open a terminal:

$ bash --login -x

the most common fault that can happen is when you edit these files in a windows environment and windows appends end of file and line feeds to the end of your commands makeing them invalid in a linux context, for example whats viewable in a windows notepad as export AWS_REGION='eu-west-1' is actually loaded and executed by linux as export AWS_REGION='eu-west-1\r'.

to fix the above issues you can run the following command on the faulty file to remove the unwanted line feeds:

sed -i 's/\r//g' .bash_profile.sh

Intsall brew by running the following command or follow the latest instructions:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

pay attention to the end of the logs as most of the time they suggest installing dependencies and other libraries that enhance brew and helper commands that can be appended to the end of your .bash_profile file.

sample .bash_profile:

source ~/.bashrc # loads colors and other settings for the terminal

eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv) # evalutes location of brew so it can be executed by simply calling brew
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment